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

Stop-motion animation [ 201 ] Chapter 7 Stop-motion animation is a technique whereby a scene is laid out (either in 2 or 3 dimensions) and a picture or snap-shot is taken (typically referred to as a frame), then that scene, or certain characters within it are manipulated, moved, or otherwise changed, before another picture or snapshot is taken. This process continues, creating a series of frames that when replayed sequentially produce the effect of motion. It is generally quite easy to produce animations in this way and we can do the same thing on a web page trivially. We won't be using any of jQuery's built-in animation methods, or the animate() method. jQuery is used to help us select elements from the page, and build the frames, but is not essential in this application. Imagery The hard part of any stop-motion animation is the number of frames that need to be generated. Too few frames and the animation will become jerky or overly rapid. But the smoothness that is generally required takes many, many frames. In this example, we'll use a series of separate images. One image is equal to one frame and there are 75 images in total—not a huge number, but enough to make their creation somewhat labor-intensive and time-consuming. Our animation will consist of a stick man that runs across the page, does a flying kick, and then bows to an unseen opponent. You will find all of these images in a folder called stickman in the img folder of the downloadable code archive that accompanies the book. There are many available software products that animators can use to simplify the process of frame creation. I used an application called Pivot Stickfigure Animator, created by Peter Bone, which was specially created to make animating stick figures easier. Technique As well as creating all the individual frames of our animation, hardcoding 75 images into a page, as well as defining a unique style for each one, would also be quite tedious, and our example animation is relatively short. This type of animation can easily run into hundreds of frames for even quite short animations. Instead, we'll create the 75 images and set their attributes and styles programmatically, which makes the process much easier for us to complete, and still happens quite quickly when the page loads.

Full Page Animations Time for action – adding the markup and styling 1. Add the following markup to the template file: 2. Save the template file as stickman.html. Now add the following styles to a new file: #cartoon { width:500px; height:500px; position:relative; } img { position:absolute; top:0; left:0; } img.loading { z-index:0; left:50%; top:50%; } 3. Save this stylesheet as stickman.css in the css folder. What just happened? All we have on the page is a container to load the frames into and a loading icon so that it appears as if something is happening when the page initially loads and the frames are being created. While running this example locally, the frames should be loaded pretty much instantly, but in the wild there would certainly be some delay. The CSS sets the container to the width of a single frame, and the frames are positioned absolutely so that they stack up on top of each other. We'll set the z-index for each element manually in the script. We can also position the loader so that it is roughly in the centre of the container. Time for action – creating the frames and running the animation Next, add the following code to the empty function at the end of the in stickman. html: var counter = 1, srcStr1 = "img/stickman/stick-kick", srcStr2 = ".jpg", frames = $(""), removeFrame = function() { if (frames.children().length > 1) { frames.children(":first").remove(); } else { clearInterval(timer); } [ 202 ]

Full Page <strong>Animation</strong>s<br />

Time for action – adding the markup and styling<br />

1. Add the following markup to the template file:<br />

<br />

<br />

<br />

2. Save the template file as stickman.html. Now add the following styles to a<br />

new file:<br />

#cartoon { width:500px; height:500px; position:relative; }<br />

img { position:absolute; top:0; left:0; }<br />

img.loading { z-index:0; left:50%; top:50%; }<br />

3. Save this stylesheet as stickman.css in the css folder.<br />

What just happened?<br />

All we have on the page is a container to load the frames into and a loading icon so that<br />

it appears as if something is happening when the page initially loads and the frames are<br />

being created. While running this example locally, the frames should be loaded pretty much<br />

instantly, but in the wild there would certainly be some delay.<br />

The CSS sets the container to the width <strong>of</strong> a single frame, and the frames are positioned<br />

absolutely so that they stack up on top <strong>of</strong> each other. We'll set the z-index for each element<br />

manually in the script. We can also position the loader so that it is roughly in the centre <strong>of</strong><br />

the container.<br />

Time for action – creating the frames and running the animation<br />

Next, add the following code to the empty function at the end <strong>of</strong> the in stickman.<br />

html:<br />

var counter = 1,<br />

srcStr1 = "img/stickman/stick-kick",<br />

srcStr2 = ".jpg",<br />

frames = $(""),<br />

removeFrame = function() {<br />

if (frames.children().length > 1) {<br />

frames.children(":first").remove();<br />

} else {<br />

clearInterval(timer);<br />

}<br />

[ 202 ]

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

Saved successfully!

Ooh no, something went wrong!