Quick Tip: How to Automatically Resize an Image to Fit the Screen
This Quick Tip explains how to use an image as a website background and scale it proportionally to the stage size. It's great for when you're using a SWF that's full-height and full-width inside a webpage.
iMac icon by bobbyperux.
Final Result Preview
Take a look at what we'll be working on. Resize your browser window and click the SWF to auto-resize the image.
Step 1: Create a New File
Let's start by creating a new Actionscript 3.0 File. Save it as resize.fla wherever you want - I'll assume C:\resize\resize.fla

Step 2: Configure the Folders
Let's create the folders we need for this tutorial. If you are familiar with classpaths and you have your own classpath, create the folder \org\display\ inside it (assuming you don't already have one with the same name).
Otherwise, create the folder \org\display in the same folder as the resize.fla document (for example: C:\resize\org\display).
Step 3: Import an Image File
Import an image to your FLA's library so that we can use it to test the effect. Click File > Import > "Import to library".


In the dialog that opens, find the file you'd like to use. The sky image I picked is available in the Source download at the top of the page.
Step 4: Configure the Image for Export
Next we'll export our image for ActionScript, so that we can refer to it in our code.
First open your library (if you can't see it, click Window > Library), right-click the image, and click Properties.


Change its name to "Sky."


If the advanced options are not visible, click Advanced in the bottom-right corner of the Properties window. Check the options, "Export for ActionScript" and "Export in Frame 1", using the name Sky for the class and flash.display.BitmapData for the base class. Also make sure the option "Allow smoothing" is checked; this option will ensure that we maintain good image quality when resizing the image.


Click OK to close the Properties panel. So, now we have configured everything in our FLA, you can save it.
Step 5: Custom Class OffsetResize
Now let's create a new ActionScript document and save it inside our \org\display\ folder as OffsetResize.as (for example: C:\resize\org\display\OffsetResize.as), or inside the \org\display\ folder in your classpath.

Next: code. Check out the comments.
1 |
|
2 |
package org.display{ |
3 |
|
4 |
//Import the needed classes for this object
|
5 |
import flash.display.Sprite; |
6 |
import flash.display.DisplayObject; |
7 |
import flash.events.Event; |
8 |
|
9 |
|
10 |
//Creating a new class extending the Sprite class
|
11 |
public class OffsetResize extends Sprite{ |
12 |
|
13 |
//Create the static constants for maximum and minimum
|
14 |
//We will use this for the offsetType property
|
15 |
public static const MAX:String="max"; |
16 |
public static const MIN:String="min"; |
17 |
|
18 |
//The kind of the resize -- whether the image is bigger or smaller than the stage
|
19 |
private var _offsetType:String; |
20 |
|
21 |
//The constructor function
|
22 |
public function OffsetResize($child:DisplayObject,$offsetType:String="max"):void{ |
23 |
|
24 |
//The offsetType; if no value is set the "max" value will be automatically assumed
|
25 |
_offsetType=$offsetType; |
26 |
|
27 |
//Add the child here, any kind of DisplayObject
|
28 |
addChild($child); |
29 |
|
30 |
//Check if this object is on stage. if so, call the init() function
|
31 |
if(stage) init(); |
32 |
|
33 |
//If it's not on stage it will listen for when it's added to the stage and then call the init() function
|
34 |
else addEventListener(Event.ADDED_TO_STAGE,init); |
35 |
|
36 |
//This will check when this object is removed from the stage and call the end() function
|
37 |
addEventListener(Event.REMOVED_FROM_STAGE,end); |
38 |
}
|
39 |
|
40 |
|
41 |
//The init() function (called when the object is in the stage)
|
42 |
//The Event=null parameter is because we used the init() without any parameter in the constructor
|
43 |
// and because it's also used as an event listener (ADDED_TO_STAGE)
|
44 |
private function init(e:Event=null):void{ |
45 |
|
46 |
//Detect when the stage is resized and call the stageResize() function
|
47 |
stage.addEventListener(Event.RESIZE,stageResize); |
48 |
|
49 |
//Call the stageResize() function now, too
|
50 |
stageResize(); |
51 |
}
|
52 |
|
53 |
//The stageResize() function will be called every time the stage is resized
|
54 |
//The e:Event=null parameter is because we have called the stageResize() function without a parameter
|
55 |
private function stageResize(e:Event=null):void{ |
56 |
|
57 |
//Calculate the width ratio by dividing the stage's width by the object's width
|
58 |
var px:Number=stage.stageWidth/width; |
59 |
|
60 |
//Calculate the height ratio by dividing the stage's height by the object's height
|
61 |
var py:Number=stage.stageHeight/height; |
62 |
|
63 |
/*
|
64 |
This is the ternary operator; in one line it checks if _offsetType is "max".
|
65 |
If so, it sets the variable div as the maximum value between the width's ratio and the height's ratio.
|
66 |
If not, it sets the variable div as the minimum value between the width's ratio and the height's ratio.
|
67 |
So, this line is responsible for whether the image is bigger or smaller than the stage.
|
68 |
*/
|
69 |
var div:Number=_offsetType=="max" ? Math.max(px,py) : Math.min(px,py); |
70 |
|
71 |
//These two lines resize this object according to the division ratio.
|
72 |
//If we use scaleX or scaleY here it wont work as we need it to.
|
73 |
width*=div; |
74 |
height*=div; |
75 |
|
76 |
//These two lines are responsible for centering this object on the stage.
|
77 |
x=(stage.stageWidth/2)-(width/2); |
78 |
y=(stage.stageHeight/2)-(height/2); |
79 |
}
|
80 |
|
81 |
//This function is called when this object is removed from stage, as we don't need the stageResize() function any more
|
82 |
private function end(e:Event):void{ |
83 |
//Remove the RESIZE listener from the stage
|
84 |
stage.removeEventListener(Event.RESIZE,stageResize); |
85 |
}
|
86 |
|
87 |
/*
|
88 |
Here we create the offsetType parameter, so we can change how the object
|
89 |
resizes dynamically
|
90 |
*/
|
91 |
public function set offsetType(type:String):void{ |
92 |
_offsetType=type; |
93 |
|
94 |
//After changing the type we call stageResize function again to update
|
95 |
if(stage) stageResize(); |
96 |
}
|
97 |
|
98 |
//Just for if we want to know what the offsetType is
|
99 |
public function get offsetType():String{ return _offsetType; } |
100 |
}
|
101 |
}
|
Now you can save the OffsetResize.as file. You can close it if you want; from now on, we won't be editing it any more, just using it inside other classes.
Step 6: Configure the Document Class
Now switch back to the FLA and let's assign a document class to it. (Not familiar with document classes? Read this quick introduction.)
Open the Properties panel of the FLA by clicking in any blank space on the stage (with no objects selected), then clicking Window > Properties.
In the panel that opens, type "Main" for the Class (or Document Class, in Flash CS3).


Save the FLA again but do not close it.
Step 7: Code the Document Class
We could write our code directly in the timeline, but that's not a good habit; the purpose of the document class is to eliminate timeline programming.
So, create a new ActionScript File and save it as "Main.as" in the same folder as your FLA (for example: C:\resize\Main.as).

Let's code it now (see the comments in the code):
1 |
|
2 |
package{ |
3 |
|
4 |
//Let's import the needed classes
|
5 |
import org.display.OffsetResize; |
6 |
import flash.display.Sprite; |
7 |
import flash.display.Bitmap; |
8 |
import flash.display.StageAlign; |
9 |
import flash.display.StageScaleMode; |
10 |
import flash.events.Event; |
11 |
import flash.events.MouseEvent; |
12 |
|
13 |
//We are extending the Sprite class, rather than MovieClip, since we won't use any timeline animation here
|
14 |
public class Main extends Sprite{ |
15 |
|
16 |
//This will be the instance of our custom object OffsetResize
|
17 |
private var _bg:OffsetResize; |
18 |
|
19 |
//The constructor
|
20 |
public function Main():void{ |
21 |
|
22 |
/*
|
23 |
We could use the stage property directly here, but I'm assuming that a lot of people
|
24 |
create a document like "base.swf" which loads the "main.swf" file -- in which case our
|
25 |
main.swf wouldn't have the stage property.
|
26 |
|
27 |
But it checks if there is a stage property. We can use this swf we will create with
|
28 |
or without the base.swf; in this case we will use it without the base.swf, but it's
|
29 |
prepared to use with the latter if we want.
|
30 |
|
31 |
So this line will check if our swf is on the stage, and, if so, call init().
|
32 |
*/
|
33 |
if(stage) init(); |
34 |
|
35 |
//If not on the stage it will call the init() function only when it's added to the stage
|
36 |
else addEventListener(Event.ADDED_TO_STAGE,init); |
37 |
}
|
38 |
|
39 |
//The init function is called only when the object is in the stage
|
40 |
//It was explained before whe we are using this in the constructor function
|
41 |
private function init(e:Event=null):void{ |
42 |
|
43 |
//Setting how the stage will scale (it will not scale) and its alignment (top-left corner)
|
44 |
stage.scaleMode=StageScaleMode.NO_SCALE; |
45 |
stage.align=StageAlign.TOP_LEFT; |
46 |
|
47 |
//Remember the picture in the library? Well, this is why we exported it for AS.
|
48 |
//Let's create a Bitmap object with that image:
|
49 |
var picture:Bitmap=new Bitmap(new Sky(0,0)); |
50 |
|
51 |
//Now we create an instance of the OffsetResize class we coded before.
|
52 |
//The second parameter is optional and can be left blank; its value can be OffsetResize.MAX or OffsetResize.MIN.
|
53 |
_bg=new OffsetResize(picture,OffsetResize.MIN); |
54 |
|
55 |
//add the instance of the OffsetResize to the stage at child index 0, so it will be behind everything (it's a background)
|
56 |
stage.addChildAt(_bg,0); |
57 |
|
58 |
//The code so far is enough to make this work, but I will show how to change the offsetType dynamically.
|
59 |
//So let's add a MOUSE_DOWN event, which will call the mouseDown() function when we click on the stage.
|
60 |
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown); |
61 |
}
|
62 |
|
63 |
//This function is called every time that we click the stage
|
64 |
private function mouseDown(e:MouseEvent):void{ |
65 |
/*
|
66 |
This is the ternary operator, it's a compact version of this long if statement:
|
67 |
if(_bg.offsetType==OffsetResize.MAX){
|
68 |
_bg.offsetType=OffsetResize.MIN;
|
69 |
}
|
70 |
else{
|
71 |
_bg.offsetType=OffsetResize.MAX;
|
72 |
}
|
73 |
*/
|
74 |
_bg.offsetType = _bg.offsetType==OffsetResize.MAX ? OffsetResize.MIN : OffsetResize.MAX; |
75 |
}
|
76 |
}
|
77 |
}
|
Step 8: Test It Out
Check if it's alright.
- We need to have an image in the Library called "Sky" which is exported for ActionScript with a class name of Sky and a base class of flash.display.BitmapData.
- We must also have the document class set to "Main"
- the Main.as file in the same folder as the FLA
- and the OffsetResize.as file inside \org\display (which is either inside the same folder as the .fla file or is inside the classpath).
If all these requirements are met, you can test it now!
Conclusion
We created a custom class called "OffsetResize", which resizes a specified object according to the stage's size. If you created it in your classpath then you can use it wherever you want: all you need to do is import the OffsetResize object and use it; it's extendable and reusable. But remember it's to use with scalable content, for example a SWF in HTML which takes up 100% of the width and height.
Thanks for reading! Post any questions in the comments.



