jQuery 1.4 Animation Techniques - Index of
jQuery 1.4 Animation Techniques - Index of jQuery 1.4 Animation Techniques - Index of
var context = canvas.getContext("2d"); init(context); } } else { var context = canvas.getContext("2d"); init(context); } })(); [ 283 ] Chapter 10 What just happened? In the previous examples in this chapter all of our variables were global, which is generally a bad practice when coding for the real world. In this example our code is within the scope of the anonymous function, so the variables are only accessible within that function and are therefore not considered global. We also use the same construct for detecting and working with IE that we did before, where we define an inline function that is either called straight away for most browsers, or once the onload event of the body is fired for IE. The function that is called is init() in this example. Within this function we declare width and pos variables and then define another inline function called rectMotion(), which will be called repeatedly by an interval. Any shapes drawn outside of the bounds of the do not exist, so we can't draw a rectangle out of view and then animate it into view. Instead, we gradually build up the rectangle by starting at the left edge and incrementally widening the rectangle until it is the correct width. This is done using the first branch of the if statement, which will be executed while the width variable is less than 400. To speed the animation up, we actually increase the width of the rectangle by two pixels at a time (although the speed of the animation is also considerably different between browsers) by increasing the width variable and then using the variable as the width argument in the fillRect() method. Once the width variable has reached 400, we then change over to use the pos variable instead. In this part of the conditional, we increase the pos variable by two (the rectangle will appear to move two pixels at a time, again for speed), reset the by setting its width, and set the fillStyle property. We then draw the new rectangle, using the pos variable as the argument for the x axis position. It will look as if the rectangle is being moved to the right, but this is not the case at all. We are actually destroying the rectangle and then drawing a completely new one two pixels to the right of the original.
Canvas Animations Once the rectangle is in the correct location we clear the interval and then call the next function, (we'll add this shortly) passing in the context object. After the rectMotion() function, we add a final variable that contains the ID of the interval which calls the function to animate the rectangle. We use this variable to clear the interval once the animation is complete. If you run the page in a browser at this point, the blue rectangle appears to move into the from the left before stopping in the middle. Next, we need to animate the horizontal and diagonal white crosses over the blue rectangle. Time for action – animating the white crosses In this part of the animation, we'll draw a white line down the middle and across the center of the rectangle, and then make the diagonal cross grow out from the center to the corners. The following code should be added in between the canvas and init variables in the code so far: whiteLines = function(context) { context.fillStyle = "#fff"; context.strokeStyle = "#fff"; context.lineWidth = 50; var width = 0, height = 0, pos = { ne: { x: 250, y: 150 }, se: { x: 250, y: 150 }, nw: { x: 250, y: 150 }, sw: { x: 250, y: 150 } }, growDiagonal = function() { if (pos.ne.x >= 50) { context.beginPath(); context.moveTo(pos.ne.x, pos.ne.y); context.lineTo(pos.ne.x - 4, pos.ne.y - 2); context.moveTo(pos.se.x, pos.se.y); context.lineTo(pos.se.x - 4, pos.se.y + 2); context.moveTo(pos.nw.x, pos.nw.y); context.lineTo(pos.nw.x + 4, pos.nw.y + 2); context.moveTo(pos.sw.x, pos.sw.y); context.lineTo(pos.sw.x + 4, pos.sw.y - 2); context.stroke(); [ 284 ]
- 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
- Page 273 and 274: CSS3 Animations The first variable
- Page 275 and 276: CSS3 Animations Time for action - a
- Page 277 and 278: Download from Wow! eBook CSS3 Anim
- Page 279 and 280: CSS3 Animations We also have to upd
- Page 281 and 282: CSS3 Animations flatMatrix[3] = fla
- Page 283 and 284: CSS3 Animations When updating the s
- Page 285 and 286: CSS3 Animations translate translate
- Page 287 and 288: Canvas Animations Subjects that we'
- Page 289 and 290: Canvas Animations We can set the co
- Page 291 and 292: Download from Wow! eBook Canvas An
- Page 293 and 294: Canvas Animations All imageData obj
- Page 295 and 296: Canvas Animations context.beginPath
- Page 297 and 298: Canvas Animations For the next part
- Page 299 and 300: Canvas Animations Time for action -
- Page 301 and 302: Canvas Animations 3. Save the new p
- Page 303: Canvas Animations Time for action -
- Page 307 and 308: Canvas Animations We then define tw
- Page 309 and 310: Canvas Animations }, context.lineTo
- Page 311 and 312: Canvas Animations Creating a canvas
- Page 313 and 314: Canvas Animations Time for action -
- Page 315 and 316: Canvas Animations What just happene
- Page 317 and 318: Canvas Animations What just happene
- Page 319 and 320: Canvas Animations aliens.splice(x,
- Page 321 and 322: Canvas Animations Pop quiz - creati
- Page 323 and 324: Download from Wow! eBook
- Page 325 and 326: Pop Quiz Answers Using fadeToggle()
- Page 327 and 328: Pop Quiz Answers Using slideToggle
- Page 329 and 330: Pop Quiz Answers Implementing the p
- Page 331 and 332: Pop Quiz Answers Animating the canv
- Page 333 and 334: show() or hide() method, triggering
- Page 335 and 336: cssHooks using 239 cssHooks feature
- Page 337 and 338: fading animations 20 flicker effect
- Page 339 and 340: proximity animations about 207 exte
- Page 341 and 342: scaleY 232 skew 232 skewX 232 skewY
- Page 343 and 344: jQuery 1.4 Reference Guide ISBN: 97
Canvas <strong>Animation</strong>s<br />
Once the rectangle is in the correct location we clear the interval and then call the next<br />
function, (we'll add this shortly) passing in the context object. After the rectMotion()<br />
function, we add a final variable that contains the ID <strong>of</strong> the interval which calls the function<br />
to animate the rectangle. We use this variable to clear the interval once the animation<br />
is complete.<br />
If you run the page in a browser at this point, the blue rectangle appears to move into<br />
the from the left before stopping in the middle. Next, we need to animate the<br />
horizontal and diagonal white crosses over the blue rectangle.<br />
Time for action – animating the white crosses<br />
In this part <strong>of</strong> the animation, we'll draw a white line down the middle and across the center<br />
<strong>of</strong> the rectangle, and then make the diagonal cross grow out from the center to the corners.<br />
The following code should be added in between the canvas and init variables in the code<br />
so far:<br />
whiteLines = function(context) {<br />
context.fillStyle = "#fff";<br />
context.strokeStyle = "#fff";<br />
context.lineWidth = 50;<br />
var width = 0,<br />
height = 0,<br />
pos = {<br />
ne: { x: 250, y: 150 },<br />
se: { x: 250, y: 150 },<br />
nw: { x: 250, y: 150 },<br />
sw: { x: 250, y: 150 }<br />
},<br />
growDiagonal = function() {<br />
if (pos.ne.x >= 50) {<br />
context.beginPath();<br />
context.moveTo(pos.ne.x, pos.ne.y);<br />
context.lineTo(pos.ne.x - 4, pos.ne.y - 2);<br />
context.moveTo(pos.se.x, pos.se.y);<br />
context.lineTo(pos.se.x - 4, pos.se.y + 2);<br />
context.moveTo(pos.nw.x, pos.nw.y);<br />
context.lineTo(pos.nw.x + 4, pos.nw.y + 2);<br />
context.moveTo(pos.sw.x, pos.sw.y);<br />
context.lineTo(pos.sw.x + 4, pos.sw.y - 2);<br />
context.stroke();<br />
[ 284 ]