Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Proportionate scaling of MovieClips

leave a comment »

There have been few instances where I had to resize a MovieClip maintaining the ascpect ratio. And every now and then I had to make temporary codes as was unable to locate where I wrote the last time. So guys I have now shared it here so that I atleast do not get lost:

static public function scaleMaintainAspectRatio( object:DisplayObject, targetWidth:Number, targetHeight:Number ):void
{
var scale:Number = 0;
var objectWidth:Number = object.width;
var objectHeight:Number = object.height;

if( targetWidth == 1 || targetHeight == 1 ){
if( targetWidth == 1 && targetHeight == 1) {
//targetWidth = 1; targetHeight = 1
//do nothing

} else if( targetWidth == 1 ){
//targetWidth = 1; targetHeight > 1
object.height = targetHeight;
scale = objectWidth / objectHeight;
object.width = targetHeight * scale;

} else if( targetHeight == 1 ) {
//targetWidth > 1; targetHeight = 1
object.width = targetWidth;
scale = objectHeight / objectWidth;
object.height = targetWidth * scale;
}

} else {

if( objectHeight > targetHeight || objectWidth > targetWidth){
if( objectWidth > objectHeight ){
scale = targetWidth / objectWidth;
object.width = targetWidth;
object.height = objectHeight * scale;

if( object.height > targetHeight ){
scale = targetHeight / objectHeight;
object.width = objectWidth * scale;
object.height = targetHeight;
}
} else {
scale = targetHeight / objectHeight;
object.width = objectWidth * scale;
object.height = targetHeight;

if( object.width > targetWidth ){
scale = targetWidth / objectWidth;
object.width = targetWidth;
object.height = objectHeight * scale;
}
}
}
}
Advertisements

Written by Deepanjan Das

November 30, 2011 at 12:18 PM

Posted in ActionScript3.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: