ActionScript 3 Drawing Bitmaps Draw a display object into bitmap data

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

A helper function to create a bitmap copy of an object. This can be used to convert vector objects, text or complex nested Sprite's to a flattened bitmap.

function makeBitmapCopy(displayObj:IBitmapDrawable, transparent:Boolean = false, bgColor:uint = 0x00000000, smooth:Boolean = true):Bitmap {

    //create an empty bitmap data that matches the width and height of the object you wish to draw
    var bmd:BitmapData = new BitmapData(displayObj.width, displayObj.height, transparent, bgColor);
            
    //draw the object to the bitmap data
    bmd.draw(displayObj, null, null, null, null, smooth);
            
    //assign that bitmap data to a bitmap object
    var bmp:Bitmap = new Bitmap(bmd, "auto", smooth);
            
    return bmp;
}

Usage:

var txt:TextField = new TextField();
txt.text = "Hello There";

var bitmap:Bitmap = makeBitmapCopy(txt, true); //second param true to keep transparency
addChild(bitmap);


Got any ActionScript 3 Question?