Popular Pages


LorenzGames, All about Flash Games!

Follow me on Twitter! Follow Me on Twitter

BitmapData: draw VS copyPixels

May 14th, 2011 - Category: ActionScript 3 , Developers
BitmapData is a very powerful class which, if used properly, can lead to huge performance increase.

Mostly an image is drawn using 2 methods, draw or copyPixels, but what is the real difference of those?

The draw method contains more parameters than the copyPixels, for example with copyPixels it is not possible to dynamically transform the image ( scale, move, rotate, etc...).

draw(source:IBitmapDrawable, matrix:Matrix = null, colorTransform:ColorTransform = null, blendMode:String = null, clipRect:Rectangle = null, smoothing:Boolean = false):void

copyPixels(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, alphaBitmapData:BitmapData = null, alphaPoint:Point = null, mergeAlpha:Boolean = false):void

Also copyPixels can only draw another BitmapData, instead of any given IBitmapDrawable.

Using copyPixels, though, can be multiple times faster than using a draw method.

Here there is a benchmark of the draw and copyPixels method.

- draw 24 ms.
- copyPixels 1 ms.

This happens because the draw method goes inside the DisplayObject and reads each single vector inside it then renders it on the BitmapData.

If we keep the same size but we remove some of the MovieClips in the main container you can notice a huge speed increase.

- draw 6 ms.
- copyPixels 1 ms.

Of course it will not always be possible to use copyPixels, but it's safe to say that if possible it should always be used instead of draw.

Download the source code


Article Comments

Rasmus Wriedt Larsen - May 18th, 2011
You could just store the vector art in a BitmapData (using draw function once), and then use copy pixels.
Of course if you want to do some dynamic scale, you need to figure out if it's worth storing lots of bitmapDatas for all possible solutions, or you should use draw for the extra functionality (the same for color transform)

chaussures femmes - September 5th, 2011
Your essay is good, I like it very much. Here I would like to share with you some things
Replica Handbags: http://www.replicabagsell.com ---meicai

Write a Comment: