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 | 1 Comment » | Tags: , , , ,

Related Posts

One comment | Add One

  1. Scott Roberts - 01/8/2009 at 12:45 am

    I’ve played with a similar loop and found that the time to show the bitmap was up to 3 seconds, although I was testing from within the Flex Builder ….

    Were you able to write out a bitmap the size of the stage, let’s say 2000 x 2000, in a faster time with that?

    Kind Regards
    -SR

Leave a Comment

Name:

E-Mail :

Website :

Comments :

Search Posts

Sponsor Links