Erik Liddell is an interactive designer based out of Boston MA. He graduated from The School of The Museum of Fine Arts Boston where he focused in Experimental Film and Multi-Media Design. PhotoShop™ style “navigator” using AS 3.0 – Lesson 1 of 2
I have broken this tutorial into two lessons in an effort to make it more accessible and less daunting. In this tutorial we will set up a Photoshop™ style image navigator. Lesson 1 will focus on the slider bar that allows our users to simply zoom in and out of the image by dragging it back and forth. In Lesson 2 we will finish out “navigator” by building the functionality which allows our users to change their viewing area or focus point of the image they are zooming in and out of.
Lesson 1 covers (event listeners, scaling, and dragging)
Lesson 2 covers (enter frame loops, event listeners, scaling, and dragging)
In order to make things easier and keep this lesson focused on Action Script 3.0 rather than flash its’ self, I have attached an
Ok, let us start by quickly taking a look at how this file is set up and familiarize ourselves with the symbols associated with it.

On the main time line you will notice 4 layers labeled “image”, “border”, “tool”, and “actions”.
On our first layer we have our image. I have made it a Movie Symbol and given it the instance name “image.”
It is IMPORTANT to note here, that when creating this symbol we make its registration point centered as shown below. We will be zooming in and out on this image during run time and we want to be sure that it remains center focused.

On our second layer I have used the rectangle tool to create a red border that is the same size as the image and turned it into a Movie Symbol with the instance name: “viewArea” and again made its’ registration point centered.
On our third layer, we have a Movie Clip with an instance name: “zTool.” This is our zoom tool. Let’s go ahead and double click on our zoom tool and take a quick peek at its guts.

On the zTool timeline we have 5 layers named: “frame”, “image”, “targetArea”, “slider Line”, and “slider”
Our frame layer is simply for graphical purposes so we are going to focus on the remaining 4.
On our image layer, I have placed the image symbol from our main timeline and scaled it down to 20% its’ original size, again giving it the instance name, “image.”
In the target area layer, I have created another border using the rectangle tool. Again it is the same exact size as our image symbol. This time however, I have filled the center of the rectangle instead of leaving it empty, selected the center color and turned that into a button symbol, then selected the whole thing and made it a Movie Symbol with a centered registration point and the instance name, “targetArea.” It is IMPORTANT to note here that the border line scale should be set to 'None' in the property inspector as shown below. This is so when our red box gets bigger or smaller the line stroke does not change.
On the slider line layer, we have a symbol with the instance name “sliderLine.” Its registration point is actually left centered, and it will be serving both a graphical purpose and as a reference to our script.
Finally, we have the slider layer. On the slider layer we have a rectangle movie clip with the instance name, “slider” I threw in an invisible button with in this symbol just so it naturally induces a hand cursor when our users scroll over it, but its not necessary.
Ok GREAT! Now that we know our way around the shell, Let us move on to the code!
First thing we are going to do is create the variable "sliderDrag" and set it to false. We will use this variable to keep track as to whether our user is dragging our slider or not. Its a Boolean, not because we will be making soup with it, but because its value will only ever be true or false
[as] var sliderDrag:Boolean=false;[/as]
Now we are going to create our Functions. There will be three functions in all; "slide" - which will allow our user merely to slide the slider bar back and forth on the slider line, "zoom" - which will change the size of the red box in our zoom tool and the image on or main stage proportionately, and "endDrag"
Lets' start with the slide Function:
[as]
function slide(event) {
var sliderLimits:Rectangle = new Rectangle(zTool.sliderLine.x,zTool.slider.y,zTool.sliderLine.width,0);
zTool.slider.startDrag(false, sliderLimits);
sliderDrag=true;
}
zTool.slider.addEventListener(MouseEvent.MOUSE_DOWN, slide);
[/as]
The first line of this code creates a function called "slide" and designates it as an event triggered function.
The second line is setting a variable that when called upon will draw a rectangle called sliderLimits whose x position is the same as our slider line, y is the same as our slider, width is 95, and height is 0. This rectangle will be used to set limits on how far our user can drag the slider. Those variable translate to this -(starting x limit or maximum left position , starting y limit or top limit, maximum right limit, and maximum bottom limit) Basically we don't want the user to drag our slider up or down (so its y or top and bottom limits are set to the sliders initial y position with "0" pixels up or down-and we don't want them to be able to drag it further left or right than the edges of our slider line so we set its limits from the x position of the slider line to the end of the slider line or its width.
The next line merely starts the drag of our slider (zTool.slider) and set its limits to the rectangle mentioned above. (in AS 2 we could have just put the limits in directly here but AS 3 doesn't like that - too many variables it complains, so we drew a rectangle and just called that single variable. Then we set our sliderDrag Variable to true - we will reference that later.
The Final line in this code adds an event listener to out slider so that when the user clicks on it, it calls this function.
Ok now we have to let flash no when to end this drag:
[as]
function endDrag(event) {
zTool.slider.stopDrag();
sliderDrag=false;
}
zTool.slider.addEventListener(MouseEvent.CLICK, endDrag);
zTool.slider.addEventListener(MouseEvent.ROLL_OUT, endDrag);
[/as]
Notice we that we included this function on both "CLICK" - which is basically onRelease from AS 2, and ROLL_OUT. This is just so if our user drags too far before releasing it will still call the stopDrag(); instead of having our slider slide back and forth until they click on it again.
Go ahead and test your movie. You will notice that our slider now slides back and forth but nothing else.
Ok now let’s get it to actually do something!
We are basically going to tell flash to measure the distance our user leaves the slider from the starting point, and turn that number in to a variable called redBoxScale which we will use to resize our little red box in the zoom tool. We want it so that the farther along the sliderLine our user drags, the smaller our box becomes, indicating that they are zooming in on out main image. There is one small problem with this though. Our sliderLine is 100 pixels long and if we use 100 pixels as that scale factor, well, our box will be scaled down to zero and thus invisible. On the other hand if we zoom back out to 0 we might actually see the edges of our images and that would just be ugly!!! So we are going to set some conditions, grounding our redBoxScale to a number between 1 and 95 instead of 0-100.
Finally we are going to use the new size of our red box in proportion to the image in our zoom tool to create a new variable called scaleFactor. We will use this variable to resize our image on the main stage.
Let’s do it!
[as]
function zoom(event) {
if (sliderDrag==true) {
var redBoxScale = (zTool.slider.x-zTool.sliderLine.x);
if (redBoxScale>95) {
redBoxScale=95;
}
if (redBoxScale<1) {
redBoxScale=1;
}
this.zTool.targetArea.scaleX=(redBoxScale-100)/100;
this.zTool.targetArea.scaleY=(redBoxScale-100)/100;
var scaleFactor = zTool.image.width/zTool.targetArea.width;
image.width = viewArea.width* scaleFactor;
image.height = viewArea.height* scaleFactor;
}
}
zTool.slider.addEventListener(MouseEvent.MOUSE_MOVE, zoom);
[/as]
That’s it! our scale portion of the navigator tool is now built.
Stay tuned for lesson 2 where we ad the functionality that allows our use to navigate around the image by dragging the small red box.