Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Last active August 29, 2015 14:04
Show Gist options
  • Save zachwlewis/fc43715b1173e8b72a12 to your computer and use it in GitHub Desktop.
Save zachwlewis/fc43715b1173e8b72a12 to your computer and use it in GitHub Desktop.
Drawing a DisplayObject to a BitmapData
/**
* Create a BitmapData image of a DisplayObject.
* This will draw the entire DisplayObject onto a BitmapData, even if
* the DisplayObject's registration point isn't the top-left extent of
* the DisplayObject.
* @param do The DisplayObject to create a BitmapData from.
* @return A BitmapData with the DisplayObject drawn onto it.
*/
public function bmdFromDo(do:DisplayObject):BitmapData
{
// Save the bounds of the DisplayObject in case its registration point isn't the top-left extent.
var doBounds:Rectangle = do.getBounds(do);
// Create a new BitmapData rounding to the nearest unit that will contain the DisplayObject.
var bmd:BitmapData = new BitmapData(uint(doBounds.width + 0.5), uint(doBounds.height + 0.5), true, 0);
// Create a Matrix to transform the DisplayObject to the BitmapData in case its registration point isn't the top-left extent.
var doMatrix:Matrix = new Matrix(1, 0, 0, 1, -doBounds.x, -doBounds.y);
// Draw the DisplayObject onto the BitmapData using the transform matrix.
bmd.draw(do, doMatrix);
// Return the freshly-minted BitmapData.
return bmd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment