We will discuss several other advantages of Javascript animations in the following tutorial. To follow along, download the chrome1.zip file and open the chrome.psd file. |
1. The chrome.psd Photoshop file has been designed so that you can easily customize the layers to
create your own chrome animation. To customize the "Top Texture" layer to your specifications or needs fill the "Top Texture" layer with the desired color or texture. Be sure to have the Preserve Transparency option on to preserve the unique shape of the layer. To customize the "Bubble" and "Bubble Texture" layers (there are three "Bubble Texture" layers--"Bubble Texture 6," "Bubble Texture 7," and "Bubble Texture 8"), use the Hue/Saturation feature with the Colorize option on. Be sure to have the Preserve Transparency option on to preserve the unique shape of the layer. You can also Colorize all of the the "Chrome" layers. After you have customized the animation file to suit your design, save the Photoshop file with the new name "ChromeNew," so that you can come back to original file if things get out of hand.
2.Now we are ready to generate the images for the animation. In this tutorial we will assume that you will be making JPEG files for the animations. If you would like to use the chrome.psd file to generate GIF animations, please refer to the other tutorials for insight on how generate GIF animations with the layered photoshop files in AniPack 1.
Continue doing this until you get to the layers named "Chrome 6-8" layers. These layers have "H&S," "Texture," and "Back" layers associated with them. You will need to turn these layers on when generating the images for these frames. For example, go back to the "ChromeNew" document, make the "Chrome 6," "Bubble 6 Back," "Bubble 6 Texture," and "Bubble 6 H&S" layers visible. Click on the "Chrome 6" layer to select it. Choose Select All from the Select menu. Now choose Copy Merged from the Edit menu. Choose New from the File menu and create a new RGB document (the document's size will default to the size of frame 1's contents which is in the clipboard. Past the clipboard contents into the new document and merge to the background. Save and name this file as a JPEG image and name it "chrome6.jpg". Your "chrome6.jpg" image should look something like this:
2. Now let's talk about how to create javascript animations. First let's look at a simple example. Click Here to view an example of a simple Javascript animation. This example is a simple javascript animation is a loop. Images 1 through 5 are displayed sequentially over and over again. There is no interactivity. Now let's look at the code. Here is the all of the HTML and Javascript code for the first example:
Let's quickly run through how this code works. First there is a variable that defines "frame" as 1. This essentially starts the animation off on frame 1. Next we have the "cycle()" function:
The cycle function starts off with an if statement. This if statement is basically a browser check. It checks to see if the browser supports "document.images". In essence, this if-statment checks to see if the browser supports the javascript necessary to power the animation. The exclaimation point in front of the "document.images" stands for "not". so this if statement essentially says "if the browser does not support document.images, then ignore everything else in this function." If the browser does support document.images, the the code within the if-statement is executed.
The remaining code is simply used to actually define the images. We will forgo explaining how it works since there is nothing you need to do with it. Just remember that this code cannot be removed. Now let's move on to a slightly more complicated version of this script. Click here to view the second example. If you rollover the chrome animation with your mouse cursor you will see that this version of the code is interactive. Here the entire code for the second example:
Like the first example, the javascript starts off by defining the frame variable as 1. Next another variable called "onbutton" is defined as false. We will talk about the "onbutton" variable more in a moment. What follows these two variable is a somewhat familiar "cycle" function. The first two lines of the cycle function are exactly the same as in the first example. However, this version of the cycle function begins to deviate in the third line: "if !onbutton". This if-statement basically says, "if the onbutton variable is not true (false)." The onbutton variable is initially defined as false, so the next line is invoked. The remaining lines within the cycle function are a bit hard to explain, so let's first describe what each line says and then we'll describe how it works. We'll skip the setTimeout line at the bottom since it is the same as in the previous example. Here are the lines under the "if !onbutton" if-statement within the cycle funtion:
"if (frame == 5) frame = 1;" -- This line says "if the frame variable is equal to 5 than redifine the frame variable as 1." "else if (frame > 5)" -- This line says "if the frame variable is not equal to five than check to see if the frame variable is greater than 5." "frame--;" -- This line says "if the frame variable is greater than 5, then decrease the frame variable by 1." "else frame++;" -- This line says "if the frame variable is not equal to 5 and not greater than 5, then increase the frame variable by 1." "if (frame > 8) frame = 8;" -- This line says "if the frame variable is greater than 8, then redefine the variable as 8." So, let's run through the cycle function. When the onbutton variable is false (as it is by default) the cycle function checks to see if the frame variable is equal to 5 and if it is, it is reset to 1. Note that this is exactly like the animation loop in the first example. If the frame variable is not equal to 5 then the cycle function checks to see if the frame variable is greater than 5. If the frame is greater than 5, then the frame variable is subtracted by 1. If the frame variable is not greater than five than the frame variable is increased by 1. Going back up a few lines, if the onbutton variable is true, then the frame variable is increased by one and continues to be increased by 1 until it reaches 8 and then it stays at eight. In real terms the cycle function works like this: initially the animation loops from the "chrome1.jpg" image through to the "chrome5.jpg" image. When the mouse rolls over the area defined by the image map, the images climb from whatever image is displayed at the time up to the "chrome8.jpg" image and then the "chrome8.jpg" image is repeatedly displayed as long as the mouse is over the area defined by the image map. So, if, for example, the cursor rolls over the animation will the "chrome3.jpg" image is being displayed, then the animation will proceed by displaying the chrome images from 4 to 8. When the cursor rolls off of the image, the image from 8 to 6 are displayed and then the animation begins to loop again. Notice that the way this script works demonstrates another benefit of using javascript for animations rather than GIF Animations. The smooth transitions that this javascript generates would be possible using GIF animations, but it would be excessivly complex and the download requirements for the range of required GIF animations would be outrageously prohibitive. |