Return to Snippet

Revision: 26164
at April 19, 2010 13:16 by adrianparr


Updated Code
// This code assumes there is a MovieClip on stage with the instance 
// name of 'pattern_mc'. Inside 'pattern_mc' there could be a mask which 
// is masking all the other layers.

var bounds:Rectangle = getRealBounds(pattern_mc);
var bmpd:BitmapData = new BitmapData(bounds.width, bounds.height);
var matrix:Matrix = new Matrix();
matrix.translate(-bounds.x, -bounds.y);
bmpd.draw(pattern_mc, matrix, null, null, null, true);
var bmp:Bitmap = new Bitmap(bmpd);

bmp.x = 20;
bmp.y = 20;
addChild(bmp);



function getRealBounds(displayObject:DisplayObject):Rectangle
{
	var bounds:Rectangle;
	var boundsDispO:Rectangle = displayObject.getBounds( displayObject );
	
	var bitmapData:BitmapData = new BitmapData( int( boundsDispO.width + 0.5 ), int( boundsDispO.height + 0.5 ), true, 0 );
	
	var matrix:Matrix = new Matrix();
	matrix.translate( -boundsDispO.x, -boundsDispO.y);
	
	bitmapData.draw( displayObject, matrix, new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
	
	bounds = bitmapData.getColorBoundsRect( 0xFF000000, 0xFF000000 );
	bounds.x += boundsDispO.x;
	bounds.y += boundsDispO.y;
	
	bitmapData.dispose();
	return bounds;
}

Revision: 26163
at April 19, 2010 13:12 by adrianparr


Initial Code
var bounds:Rectangle = getRealBounds(pattern_mc);
var bmpd:BitmapData = new BitmapData(bounds.width, bounds.height);
var matrix:Matrix = new Matrix();
matrix.translate(-bounds.x, -bounds.y);
bmpd.draw(pattern_mc, matrix, null, null, null, true);
var bmp:Bitmap = new Bitmap(bmpd);

bmp.x = 20;
bmp.y = 20;
addChild(bmp);



function getRealBounds(displayObject:DisplayObject):Rectangle
{
	var bounds:Rectangle;
	var boundsDispO:Rectangle = displayObject.getBounds( displayObject );
	
	var bitmapData:BitmapData = new BitmapData( int( boundsDispO.width + 0.5 ), int( boundsDispO.height + 0.5 ), true, 0 );
	
	var matrix:Matrix = new Matrix();
	matrix.translate( -boundsDispO.x, -boundsDispO.y);
	
	bitmapData.draw( displayObject, matrix, new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
	
	bounds = bitmapData.getColorBoundsRect( 0xFF000000, 0xFF000000 );
	bounds.x += boundsDispO.x;
	bounds.y += boundsDispO.y;
	
	bitmapData.dispose();
	return bounds;
}

Initial URL
http://blog.open-design.be/2010/01/26/getbounds-on-displayobject-not-functioning-properly/

Initial Description
If you need to use bitmapData.draw() method on a DisplayObject that has a mask as a child then it can be awkward getting it to work properly. Using Thomas John's getRealBounds method is a great solution.

Initial Title
AS3 bitmapData.draw() with Masked DisplayObject

Initial Tags

                                

Initial Language
ActionScript 3