ActionScript 3 lacks functionality for visible bounds of DisplayObject
Posted on 3rd May 2010 in ActionScript | No Comments »
The following has already been listed as a feature request at Adobe Bug System and is only to illustrate the situation which is basically the lack of badly needed functionality in ActionScript 3.
The problem relates to flash.geom.PerspectiveProjection introduced with the load of native 3D in Flash 10.
The situation
- The object is added to the stage with the defined x, y, z, rotationX, rotationY and rotationZ properties.
- For a better 3D impression, perspective projection is applied.
- After all transformations on-screen size of the object is required.
The code
package { import flash.display.Sprite; import flash.display.Graphics; import flash.geom.Rectangle; import flash.geom.PerspectiveProjection; import flash.display.BitmapData; public class PPPlane extends Sprite { public function PPPlane():void { var Plane:Sprite = drawPlane(); Plane.x = stage.stageWidth / 2 - Plane.width/2; Plane.y = stage.stageHeight / 2 - Plane.height/2; Plane.z = 100; Plane.rotationY = 70; Plane.rotationX = 20; Plane.rotationZ = 30; addPerspective(); addChild(Plane); trace("Plane size: "+Plane.width+"x"+Plane.height); var bounds:Rectangle = Plane.getBounds(stage); trace("Plane bounds: "+bounds); var visibleBounds:Rectangle = visibleBitmapBounds(); trace("Visible, on-screen bounds of the Plane: "+visibleBounds); } private function addPerspective():void { var pp:PerspectiveProjection = new PerspectiveProjection(); pp.fieldOfView = 120; this.transform.perspectiveProjection = pp; } private function drawPlane():Sprite { var Plane:Sprite = new Sprite(); Plane.graphics.beginFill(0x0000FF); Plane.graphics.drawRect(0, 0, 200, 200); Plane.graphics.endFill(); return Plane; } private function visibleBitmapBounds():Rectangle { var w:int = stage.stageWidth; var h:int = stage.stageHeight; var bitmapData:BitmapData = new BitmapData(w, h, true, 0); bitmapData.draw(this) var bounds:Rectangle = bitmapData.getColorBoundsRect(0xFF000000, 0x00000000, false); bitmapData.dispose(); return bounds; } } }
The code is illustrative and self-explanatory: addPerspective() method adds perspective projection and visibleBitmapBounds() returns a Rectangle with visible bounds of the object.
The result and the problem
Depending on the stage size, the result is the blue 3D-rotated plane in the perspective alongside the traceback:
Plane size: 274x365 Plane bounds: (x=256, y=209, w=274, h=365) Visible, on-screen bounds of the Plane: (x=258, y=215, w=171, h=287)
Plane size and bounds are correct to what it is without perspective transformations, but they don’t help with the size of an object on screen.
So what is currently missing from ActionScript 3 entirely, is the means to get visible bounds of the DisplayObject that has perspective projection. It is especially painful if the object can’t be output to stage for measuring (visibleBitmapBounds()).
If any math geniuses have a better way to get visible bounds of the object with perspective projection, you’re most welcome to share it!
Related resources:
- Vote for FP-741 at Adobe Bug System!
- flash.geom.PerspectiveProjection at the ActionScript 3.0 Reference for the Adobe Flash Platform
- Perspective Projection in Flash by Bjørn Gunnar Staal
- Flash Platform is at a crossroads

