/ Published in: ActionScript 3
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// 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; }
URL: http://blog.open-design.be/2010/01/26/getbounds-on-displayobject-not-functioning-properly/