/ Published in: ActionScript 3
DisplayUtils.as and Alignment.as
These two classes by Justin Windle (aka Soulwire) can be used to scale a DisplayObject to fit within (fill) a rectangle, without distorting the image.
Example Usage
Fit a DisplayObject into a Rectangle
var area:Rectangle = new Rectangle( 0, 0, 200, 100 );
DisplayUtils.fitIntoRect( myDisplayObject, area, true, Alignment.MIDDLE );
Create a fullscreen background image
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
function onStageResized( event:Event = null ):void
{
var area:Rectangle = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
DisplayUtils.fitIntoRect( myDisplayObject, area, true, Alignment.MIDDLE );
}
stage.addEventListener( Event.RESIZE, onStageResized );
onStageResized();
Create a thumbnail
var thumb:Bitmap = DisplayUtils.createThumb( myBitmap.bitmapData, 100, 100, Alignment.MIDDLE, true );
addChild( thumb );
These two classes by Justin Windle (aka Soulwire) can be used to scale a DisplayObject to fit within (fill) a rectangle, without distorting the image.
Example Usage
Fit a DisplayObject into a Rectangle
var area:Rectangle = new Rectangle( 0, 0, 200, 100 );
DisplayUtils.fitIntoRect( myDisplayObject, area, true, Alignment.MIDDLE );
Create a fullscreen background image
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
function onStageResized( event:Event = null ):void
{
var area:Rectangle = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
DisplayUtils.fitIntoRect( myDisplayObject, area, true, Alignment.MIDDLE );
}
stage.addEventListener( Event.RESIZE, onStageResized );
onStageResized();
Create a thumbnail
var thumb:Bitmap = DisplayUtils.createThumb( myBitmap.bitmapData, 100, 100, Alignment.MIDDLE, true );
addChild( thumb );
URL: http://blog.soulwire.co.uk/code/actionscript-3/fit-a-displayobject-into-a-rectangle