Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created October 20, 2010 22:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelrinaldi/637497 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/637497 to your computer and use it in GitHub Desktop.
Useful to get the real bounds of an object.
package rinaldi.display
{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Rectangle;
/**
*
* This method returns a Rectangle with the real visual DisplayObject bounds.
*
* @param p_source Source to get real visual bounds.
*
* @return The real visual DisplayObject bounds.
*
* @example
* <pre>
* import flash.text.TextField;
* import flash.geom.Rectangle;
* import flash.display.Shape;
* import rinaldi.display.getVisualBounds;
*
* var label : TextField;
* var visualBounds : Rectangle;
* var rect : Shape;
*
* label = new TextField();
* label.text = "Lorem Ipsum";
* label.width = 200;
* label.height = 50;
* addChild(label);
*
* visualBounds = getVisualBounds(label);
* rect = new Shape();
* with(rect.graphics) beginFill(0xcc0000), drawRect(visualBounds.x, visualBounds.y, visualBounds.width, visualBounds.height);
* addChild(rect);
*
* trace("The visual 'label' dimensions:", visualBounds.width, visualBounds.height);
* </pre>
*
* @see rinaldi.display
*
* @author Rafael Rinaldi, Arthur Debert and Gabriel Laet.
*
* */
public function getVisualBounds( p_source : DisplayObject ) : Rectangle
{
var rect : Rectangle;
var data : BitmapData;
const CANVAS_WIDTH : int = 2000;
const CANVAS_HEIGHT : int = 2000;
data = new BitmapData(CANVAS_WIDTH, CANVAS_HEIGHT, true, 0x00000000);
data.draw(p_source);
rect = data.getColorBoundsRect(0xff000000, 0x00000000, false);
if(rect == null) rect = new Rectangle;
data.dispose();
data = null;
return rect;
}
}
@hankpillow
Copy link

Hey mate, dont get me wrong, but you've wasted your time :S
this method only works for objects with register point at 0,0 and it's a bit "hard" to process large displays.
the actionscript api has a native and fast solution for it ;)
check it out!

        var ball  : Sprite = new Sprite
        ball.graphics.beginFill(0xff00)
        ball.graphics.drawCircle( 0,0, 20 );
        ball.graphics.endFill( );
        ball.x += ball.y += 20

        addChild(ball)

        trace(ball.width, ball.height)// 40,40
        trace(getVisualBounds(ball))// (x=0, y=0, w=20, h=20)
        trace(ball.transform.pixelBounds) //(x=0, y=0, w=40, h=40)

happy coding mate!
bota!

@rafaelrinaldi
Copy link
Author

The first idea was to get the "real" bounds from a TextField and it works pretty well to solve that problem. If you just use "getBounds" from AS3 API, I don't know why, but the data comes wrong.

I never used "pixelBounds", I'll try it on the next time.

Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment