AS3 | Flex | Programming

Scaling a DisplayObject to fit to a container’s dimensions

I’ve often run into the problem of proportionally scaling an image (or other DisplayObject) to the dimensions of its parent container. I always end up having to scavenge around in my code bank for the line of code to do this—which really is a simple bit of code—but I prefer to copy-and-paste it than rewrite it. SO I am posting it here so I never have to search for it again:

var scale:Number = ((container.height/container.width) > (image.height/image.width)) ? (container.width/image.width) : (container.height/image.height);
image.scaleX = scale;
image.scaleY = scale;

Download an example here