jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of

02.06.2013 Views

Download from Wow! eBook }, timer = setInterval(function() { removeFrame() }, 50); for(var x = 75; x--;) { $("", { src: srcStr1 + counter + srcStr2 }).css("zIndex", x).appendTo(frames); counter++; } frames.appendTo("#cartoon"); When we run the page, the animation should proceed as we expect, much like the type of sketch we perhaps may have idly created in a notepad in our youth and "watched" by flicking through the pages quickly. The following screenshot shows a single frame of the stickman animation: Clearly, the best way to view the animation is in a browser. [ 203 ] Chapter 7 What just happened? We start out by initializing some variables. We set a counter variable and a series of strings representing the common strings that we'll need to use repeatedly. These will be used inside a for loop so we don't want to define them within the loop as JavaScript will create the same string objects repeatedly, whereas by defining them outside of the loop will ensure they only get created once. We also create a new container which we'll append each of the new frames to, and assign a function to the setInterval JavaScript function.

Full Page Animations Next we define the removeFrame() function which will be executed by setInterval. All this function does is check whether there is more than one element within the frames container and if so, remove the first one. Every 50 milliseconds, the top image will be removed, which is fast enough for the repeated showing of still images to be perceived as an animation. If there is only one image left, we clear the timeout as the animation has completed. Next we define the for loop, specifying the maximum number of frames, and decrementing on each iteration of the loop. We don't need to specify a comparison condition in this form of loop however, because the loop will naturally end when x = 0 (because 0 is a falsey value). Using decrementing for loops is a proven strategy for faster JavaScript. On each iteration of the loop we create a new element and set its src to point to the correct image file using a combination of the strings we created earlier and the counter variable. We set the z-index of each image as it is created using the css() method and the x variable used to control the loop. On each iteration, x will decrease, so each image added to the page will be lower down in stacking order than the previous one, which is exactly the order we require. We then append the image to our new container . At the end of each iteration, we increment the counter variable by 1. After the loop has completed, we append our container element, which now contains all of the necessary images, to the container hardcoded into the page. This will overlay the loading spinner. In a full implementation, we'd probably remove the spinner at this point. Pop quiz – implementing stop-motion animation with jQuery 1. In this example, we used a decrementing for loop, why? a. We need to in order to set a descending z-index on the images. b. The decrementing format of the loop is required when creating inline elements with jQuery. c. Because the code is easier to read. d. For performance reasons. Because the loop isn't checking a condition on every iteration. It's simply removing one from the value of x, so it runs faster. Have a go hero – extending stop-motion animation Simple two dimensional stickmen aren't the only images that can be used to create a stop-motion animation. Pretty much any series of sequential images can be used, so experiment with color images or photographs. Time-lapse photography offers an excellent source of the right kind of photos to use. [ 204 ]

Download from Wow! eBook <br />

},<br />

timer = setInterval(function() { removeFrame() }, 50);<br />

for(var x = 75; x--;) {<br />

$("", {<br />

src: srcStr1 + counter + srcStr2<br />

}).css("z<strong>Index</strong>", x).appendTo(frames);<br />

counter++;<br />

}<br />

frames.appendTo("#cartoon");<br />

When we run the page, the animation should proceed as we expect, much like the type<br />

<strong>of</strong> sketch we perhaps may have idly created in a notepad in our youth and "watched" by<br />

flicking through the pages quickly. The following screenshot shows a single frame <strong>of</strong> the<br />

stickman animation:<br />

Clearly, the best way to view the animation is in a browser.<br />

[ 203 ]<br />

Chapter 7<br />

What just happened?<br />

We start out by initializing some variables. We set a counter variable and a series <strong>of</strong> strings<br />

representing the common strings that we'll need to use repeatedly. These will be used inside<br />

a for loop so we don't want to define them within the loop as JavaScript will create the<br />

same string objects repeatedly, whereas by defining them outside <strong>of</strong> the loop will ensure<br />

they only get created once.<br />

We also create a new container which we'll append each <strong>of</strong> the new frames to, and<br />

assign a function to the setInterval JavaScript function.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!