August 11, 2005
Maximizing BitmapData.setPixel performance
Flash 8's ability to use bitmap / pixel data in the player is an awesome new feature addition. When you're doing a lot of pixel manipulation, here's a little trick to improve performance: Use an off-screen bitmap for setPixel and then copy the pixels into an on-screen bitmap.
Consider the following code snippet:
import flash.geom.Point; import flash.display.BitmapData; import flash.display.Bitmap; var width:Number = 100; var height:Number = 100; var offScreen:BitmapData = new BitmapData(width, height); var onScreen:BitmapData = new BitmapData(width, height); var zeroPoint:Point = new Point(0, 0); var clip:MovieClip = createEmptyMovieClip("clip", 1); clip._x = 10; clip._y = 10; clip.attachBitmap(onScreen, 1); // wire the bitmap to the screen onEnterFrame = function() { // do all of the pixel manipulation in the offScreen BitmapData for (var y:Number = 0; y < height; y++) { for (var x:Number = 0; x < width; x++) { offScreen.setPixel(x, y, x*y+getTimer()); } } // as a final step, copy all of the offScreen pixels to the onScreen BitmapData onScreen.copyPixels(offScreen, offScreen.rectangle, zeroPoint); }
The difference may not be huge, but it does make a difference. This technique, drawing off screen and then copying on screen, is called Double Buffering.
Already making great use of BitmapData, have you see Andre's 8ball lab yet?

Comments
For what it does, copyPixels is insanely fast!
Posted by: JesterXL at August 11, 2005 11:40 AM
never thought of that!
Posted by: Tink at August 11, 2005 05:12 PM