Posted By

adrianparr on 04/19/10


Tagged

draw rectangle movieclip bitmapData as3 bitmap DisplayObject matrix bounds getrealbounds


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

socialflasher
Nikolai
tspitzr
burnandbass


AS3 bitmapData.draw() with Masked DisplayObject


 / Published in: ActionScript 3
 

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

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.

  1. // This code assumes there is a MovieClip on stage with the instance
  2. // name of 'pattern_mc'. Inside 'pattern_mc' there could be a mask which
  3. // is masking all the other layers.
  4.  
  5. var bounds:Rectangle = getRealBounds(pattern_mc);
  6. var bmpd:BitmapData = new BitmapData(bounds.width, bounds.height);
  7. var matrix:Matrix = new Matrix();
  8. matrix.translate(-bounds.x, -bounds.y);
  9. bmpd.draw(pattern_mc, matrix, null, null, null, true);
  10. var bmp:Bitmap = new Bitmap(bmpd);
  11.  
  12. bmp.x = 20;
  13. bmp.y = 20;
  14. addChild(bmp);
  15.  
  16.  
  17.  
  18. function getRealBounds(displayObject:DisplayObject):Rectangle
  19. {
  20. var bounds:Rectangle;
  21. var boundsDispO:Rectangle = displayObject.getBounds( displayObject );
  22.  
  23. var bitmapData:BitmapData = new BitmapData( int( boundsDispO.width + 0.5 ), int( boundsDispO.height + 0.5 ), true, 0 );
  24.  
  25. var matrix:Matrix = new Matrix();
  26. matrix.translate( -boundsDispO.x, -boundsDispO.y);
  27.  
  28. bitmapData.draw( displayObject, matrix, new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );
  29.  
  30. bounds = bitmapData.getColorBoundsRect( 0xFF000000, 0xFF000000 );
  31. bounds.x += boundsDispO.x;
  32. bounds.y += boundsDispO.y;
  33.  
  34. bitmapData.dispose();
  35. return bounds;
  36. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: burnandbass on November 29, 2012

Very helpful ! Thank you, just get the work done!

You need to login to post a comment.