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

[ 289 ] Chapter 10 The four custom shapes are drawn using a single master function that accepts a configuration object specifying the start point (passed to the moveTo() method), and the points that make up each sub-path (passed to the lineTo() methods). We then use the setTimeout JavaScript function to create each shape one after the other, using the object passed to the master function to specify the relevant points on the canvas to draw each shape. This is all the code we need, so when we run the page now we should see the animation of the flag being drawn. The code works in all browsers, including IE, but as I mentioned earlier, the performance does vary considerably between browsers, with Webkit and Opera browsers running smoothly and very quickly, Firefox somewhere in the middle, and IE crawling along almost intolerably slowly. Animating the is all about conditional if statements, intervals, and timeouts. As we saw, the code itself is quite straight-forward. We just need rather a lot of it in order to produce even simple animations. Pop quiz – animating the canvas 1. Why did we store each call to setInterval() in a variable? a. For performance reasons b. In order to clear the interval when appropriate c. Because of the closure created with the anonymous function as the first argument to the function d. So that we can pass arguments to the function called by the interval 2. In the first function, where we drew the blue rectangle, we set the width of the each time the rectMotion() function is called by the interval. Why? a. To make sure the was big enough to contain the rectangle as it grew b. To correct a bug in Internet Explorer c. To reset the state of the , ensuring there was only one rectangle at each point in the animation d. As a requirement for setting the fillStyle property Have a go hero – creating canvas animations Go back to the static version of the flag you drew of your home country (or the logo or image of your choice) and convert it so that the different parts of the flag are animated into existence.

Canvas Animations Creating a canvas game The best animations are those that are interactive and engage the user, and this is exactly how a game can be seen, as one continuous, user-driven animation. The power of the element is really highlighted when it is used to create games, as we'll see over the course of this section. We'll create a very basic clone of the arcade classic Space Invaders with a series of alien ships that slowly advance down the screen, and a user-controlled space ship at the bottom that can shoot the incoming aliens: Time for action – creating the initial page The initial page that we'll use for this example is similar to that used in the previous example, although this time we won't be supporting Microsoft's Internet Explorer so some of the initialization code isn't required. 1. Create a new page in your text editor that contains the following markup: A canvas and jQuery Game [ 290 ]

Canvas <strong>Animation</strong>s<br />

Creating a canvas game<br />

The best animations are those that are interactive and engage the user, and this is exactly<br />

how a game can be seen, as one continuous, user-driven animation. The power <strong>of</strong> the<br />

element is really highlighted when it is used to create games, as we'll see over<br />

the course <strong>of</strong> this section.<br />

We'll create a very basic clone <strong>of</strong> the arcade classic Space Invaders with a series <strong>of</strong> alien ships<br />

that slowly advance down the screen, and a user-controlled space ship at the bottom that<br />

can shoot the incoming aliens:<br />

Time for action – creating the initial page<br />

The initial page that we'll use for this example is similar to that used in the previous example,<br />

although this time we won't be supporting Micros<strong>of</strong>t's Internet Explorer so some <strong>of</strong> the<br />

initialization code isn't required.<br />

1. Create a new page in your text editor that contains the following markup:<br />

<br />

<br />

<br />

<br />

A canvas and <strong>jQuery</strong> Game<br />

<br />

<br />

[ 290 ]

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

Saved successfully!

Ooh no, something went wrong!