The Fastest Way To Draw Pixels In ActionScript 3

By admin | May 9, 2008

ActionScript 3 can display a dynamically generated bitmap, in which the RGB values of each pixel are computed by code.It can be help us to render 3D scenes, process video and images, and create other effects.In order to draw a dynamically animated bitmap, it may be necessary to write to every pixel in the BitmapData each frame.We can call setPixel once for every pixel or build an appropriatedly sized ByteArray to do it.But which is the fastest?

The following is the fastest: Calls setPixels once per pixel, uses y in the outer loop and x in the inner loop, and locks and unlocks the BitmapData before and after writing to it. The main loop of code looks like this:

Download: main.mxml
  1. outputBitmapData.lock();
  2. for(y = 0; y < STAGE_HEIGHT; ++y)
  3. {
  4.   for(x = 0; x < STAGE_WIDTH; ++x)
  5.   {
  6.     r = (t*100 + 255 * x / STAGE_WIDTH)%255;
  7.     g = 180;
  8.     b = 180;
  9.  
  10.     outputBitmapData.setPixel(x, y, (r<<16) + (g<<8) + b);
  11.   }
  12. }
  13. outputBitmapData.unlock();

Topics: Adobe-Flex | | Tags: , , , ,

Search Posts