jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
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 ]
- Page 171 and 172: Download from Wow! eBook Extended
- Page 173 and 174: Extended Animations with jQuery UI
- Page 175 and 176: Extended Animations with jQuery UI
- Page 177 and 178: Extended Animations with jQuery UI
- Page 179 and 180: Extended Animations with jQuery UI
- Page 181 and 182: Extended Animations with jQuery UI
- Page 183 and 184: Extended Animations with jQuery UI
- Page 185 and 186: Extended Animations with jQuery UI
- Page 187 and 188: Extended Animations with jQuery UI
- Page 189 and 190: Extended Animations with jQuery UI
- Page 191 and 192: Extended Animations with jQuery UI
- Page 193 and 194: Extended Animations with jQuery UI
- Page 195 and 196: Extended Animations with jQuery UI
- Page 197 and 198: Extended Animations with jQuery UI
- Page 199 and 200: Extended Animations with jQuery UI
- Page 201 and 202: Full Page Animations Can we animate
- Page 203 and 204: Full Page Animations display:block;
- Page 205 and 206: Download from Wow! eBook Full Page
- Page 207 and 208: Full Page Animations Have a go hero
- Page 209 and 210: Full Page Animations 4. Save this f
- Page 211 and 212: Full Page Animations 2. The bgpos c
- Page 213 and 214: Full Page Animations nav ul { margi
- Page 215 and 216: Full Page Animations screensize = {
- Page 217 and 218: Full Page Animations What just happ
- Page 219 and 220: Full Page Animations The second sec
- Page 221: Full Page Animations Pop quiz - cre
- Page 225 and 226: Full Page Animations Next we define
- Page 228 and 229: 8 Other Popular Animations This cha
- Page 230 and 231: [ 209 ] Chapter 8 } .slider #scroll
- Page 232 and 233: }), pointerText = "Use your pointer
- Page 234 and 235: [ 213 ] Chapter 8 In the previous s
- Page 236 and 237: prox.mousemove(function(ev) { goAni
- Page 238 and 239: Pop quiz - implementing proximity a
- Page 240 and 241: [ 219 ] Chapter 8 What just happene
- Page 242 and 243: 2. Save the new page as marquee.htm
- Page 244 and 245: [ 223 ] Chapter 8 1. jQuery provide
- Page 246 and 247: clear:both; } div#fadeLeft, div#fad
- Page 248 and 249: Time for action - animating the pos
- Page 250: 2. In this example we used the dele
- Page 253 and 254: CSS3 Animations The following trans
- Page 255 and 256: CSS3 Animations Units It is of crit
- Page 257 and 258: CSS3 Animations Skew Remember the t
- Page 259 and 260: CSS3 Animations Calculating the sin
- Page 261 and 262: CSS3 Animations The filter property
- Page 263 and 264: CSS3 Animations If, like me, you th
- Page 265 and 266: CSS3 Animations } el.filters.item("
- Page 267 and 268: CSS3 Animations An unfortunate prob
- Page 269 and 270: CSS3 Animations The context of this
- Page 271 and 272: CSS3 Animations Time for action - i
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 ]