RSS

How to get a DisplayObject’s BitmapData

2 Comments | This entry was posted on Jun 17 2008

There are many uses to using a DispalyObject’s (MovieClip, Sprite, Loader, etc…) BitmapData. The main reason I find myself using it for is for scaling an image I loaded smoothly. I load the image from an external source, then pull it’s BitmapData and set it’s smoothing to true. Then I can scale the new image without it getting too grainy or pixelated. This is the same effect as selecting the “Allow Smoothing” checkbox if you are using a Bitmap from the library.

In this example, we will get the BitmapData from our MovieClip (myMC) and create a new Bitmap (b), then add it to the stage:
 

var bd:BitmapData = new BitmapData(myMC.width, myMC.height, true,0xFFFFFFFF);
bd.draw(myMC);
var b:Bitmap = new Bitmap(bd);
addChild(b);

I used the BitmapData.draw() method to pull the movie clip’s (myMC) pixel information. I can then use that data when creating a new Bitmap. All that is left is to add the new Bitmap (b) to the display tree (addChild).

Jagged Bitmap Movement

1 Comment | This entry was posted on Jun 05 2008

Recently I was working with floating text that had filters applied to it and couldn’t figure out why it was moving on whole pixels. It caused the slower movement to look really jagged and not smooth at all. It turns out that the cacheAsBitmap attribute when set to true (which is the case when filters are applied) causes flash player to render the DisplayObject on whole pixels only. A quick hack that seemed to work was to alter the width and/or the height of said DisplayObject on each frame. This causes flash player to constantly re-render and cache the bitmap data.

Example:

var myClip:MovieClip = new MovieClip();
addChild(myClip);
myClip.filters = [new BlurFilter(10,10,3)]

myClip.addEventListener(Event.ENTER_FRAME, update);

function update(e:Event) {
     e.target.width += Math.random()/100;
}