7 July 2008

Proportional Scaling Techniques in AS3

The ability to proportionally scale an object to fit within, or fill a region is a powerful technique with a variety of applications. We used this approach on Logobama to allow users to upload any size photo and automatically scale it to fit within the available space, before saving out an image from Flash.

The source code for this post includes a ScalingImage class that loads an image and responds to any stage resize event by proportionally scaling the image to fill the stage. Check out the demo and try resizing your browser.

The code behind a proportionally scaled full screen image is fairly straightforward:

// set image dimensions to match stage;
image.width = stage.stageWidth;
image.height = stage.stageHeight;

// choose the larger scale property and match the other to it;
( image.scaleX < image.scaleY ) ? image.scaleY = image.scaleX : image.scaleX = image.scaleY;

Since scaleX and scaleY are percentages, it’s just a matter of figuring out which property is larger and making the two equal.

By swapping the scale comparison from “greater than” to “less than”, the object will scale proportionally to fit the larger of the two dimensions given. This kind of calculation is handy when you need to fit items of variable dimensions into a fixed space, such as the flickr photos on the Authenticity Book Axiom Gallery.

// choose the smaller scale property and match the other to it;
( image.scaleX > image.scaleY ) ? image.scaleY = image.scaleX : image.scaleX = image.scaleY;

There are many applications for this technique, from creating image thumbnails to building a liquid grid layout. We’d love to see more examples of this approach in action, so feel free to leave a link or two in the comments.

posted by Shaun Tinney

thinking about… AS3, Flash

4 Comments…

  1. Trikke said…

    Lovely formula, you sir, just saved me a headache!

    9:04 am / July 9th, 2008

  2. Hello Shaun. How can I use this for a slideshow component with the event resize? When I use this without the event resize the object goes to the space and when I use this with event resize I can’t get the proportional scale.

    Can you help me?

    3:29 pm / August 8th, 2008

  3. Lissa said…

    I am a newbie so please excuse the question but I am having trouble with the source files. When I open the scaling-image.fla file and try to preview the html, it is always blank. Therefore when I publish it I get a blank screen. I really need to be able to develop the site in flash with this code to resize the background image, but I can not seem to get it to publish. The swf works just fine. Any advice would be helpful

    Thanks!

    10:04 am / October 21st, 2008

  4. Wes said…

    Hey great classes!!! I’m making an honest effort to stop using AS2. I know that I could maybe Mickey Mouse my problem by drawing in the a scalable AS2 swf into into a AS3 swf. The question is how do I set the image as the bottom root layer, so the image doesn’t sit on top of all the other layers? Thanks ~ be5iK.inK

    8:28 pm / November 18th, 2008

Have a comment?