BitmapData Set Each Pixel To Random Color


/ Published in: ActionScript 3
Save to your folder(s)

Using lock() and unLock() saves about 20% when doing bulk pixel updates.


Copy this code and paste it in your HTML
  1. // Create the bitmap
  2. var imgData:BitmapData = new BitmapData(500, 500, true, 0x00000000);
  3. var bmp:Bitmap = new Bitmap(imgData);
  4. // Invoke lock( )
  5. imgData.lock( );
  6. // Set pixel color-values
  7. var color:uint;
  8. for (var i:int = 0; i < imgData.height ; i++) {
  9. for (var j:int = 0; j < imgData.width; j++) {
  10. color = Math.floor(Math.random( )*0xFFFFFFFF);
  11. imgData.setPixel32(j, i, color);
  12. }
  13. }
  14. // Invoke unlock( )
  15. imgData.lock( );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.