radistao
radistao

Reputation: 15504

How to define rectangle of visible children in Flash/AS3 ("real" element size)

I use BitmapData.draw to make a snapshot of a graph canvas. On the canvas located some children in random positions (including negative positions). I should to define in some way what rectangle bounds to shoot.

SO, for example, i have 3 nodes in the view (canvas):

In this case bounds should be (-5; -3) (left top) to (70; 70) (right bottom).

Is there a way to define "real" element size, based on its children layout?

May be some methods from Flex framework?

Upvotes: 0

Views: 255

Answers (2)

radistao
radistao

Reputation: 15504

Looks like in the most cases getBounds() and getRect() should work, but some times they may have problem calculating right-bottom corner, for instance, when component explicitly resized.

For my component I found already implemented method (this solution also ignores invisible children!):

public function calcDisplayObjecBoundingBox():Rectangle
  {
    var numChildren:int = this.numChildren;

    var result:Rectangle = new Rectangle( 0, 0, 0, 0 );
    var i:int = 0;
    var child:DisplayObject;

    if( numChildren > 0 )
    {

      //find first visible child and initialize rectangle bounds
      for( i = 0; i < numChildren; i++ )
      {
        child = this.getChildAt( i );

        if( child.visible )
        {
          result.left = child.x;
          result.top = child.y;
          result.right = child.x + child.width;
          result.bottom = child.y + child.height;
          i++;
          break;
        }
      }

      for( ; i < numChildren; i++ )
      {
        child = this.getChildAt( i );

        if( child.visible )
        {
          result.left = Math.min( result.left, child.x );
          result.right = Math.max( result.right, child.x + child.width );
          result.top = Math.min( result.top, child.y )
          result.bottom = Math.max( result.bottom, child.y + child.height );
        }
      }
    }

Probably, it works pretty much the same as for getBounds() and getRect(), but calculates right-bottom point by children, not the component itself.

Upvotes: 1

null
null

Reputation: 5255

DisplayObject.getBounds() and DisplayObject.getRect() are the methods that you are looking for.

Both methods

[Return] a rectangle that defines the boundary of the display object, based on the coordinate system defined by the targetCoordinateSpace parameter

The difference between them is:

The getBounds() method is similar to the getRect() method; however, the Rectangle returned by the getBounds() method includes any strokes on shapes, whereas the Rectangle returned by the getRect() method does not.

If you use strokes, you probably want to use getBounds()

Upvotes: 1

Related Questions