02.06.2013 Views

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

jQuery 1.4 Animation Techniques - Index of

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

var context = canvas.getContext("2d");<br />

init(context);<br />

}<br />

} else {<br />

var context = canvas.getContext("2d");<br />

init(context);<br />

}<br />

})();<br />

[ 283 ]<br />

Chapter 10<br />

What just happened?<br />

In the previous examples in this chapter all <strong>of</strong> our variables were global, which is generally<br />

a bad practice when coding for the real world. In this example our code is within the scope<br />

<strong>of</strong> the anonymous function, so the variables are only accessible within that function and are<br />

therefore not considered global.<br />

We also use the same construct for detecting and working with IE that we did before,<br />

where we define an inline function that is either called straight away for most browsers,<br />

or once the onload event <strong>of</strong> the body is fired for IE. The function that is called is init()<br />

in this example.<br />

Within this function we declare width and pos variables and then define another inline<br />

function called rectMotion(), which will be called repeatedly by an interval. Any shapes<br />

drawn outside <strong>of</strong> the bounds <strong>of</strong> the do not exist, so we can't draw a rectangle<br />

out <strong>of</strong> view and then animate it into view. Instead, we gradually build up the rectangle by<br />

starting at the left edge and incrementally widening the rectangle until it is the correct width.<br />

This is done using the first branch <strong>of</strong> the if statement, which will be executed while the<br />

width variable is less than 400. To speed the animation up, we actually increase the<br />

width <strong>of</strong> the rectangle by two pixels at a time (although the speed <strong>of</strong> the animation is also<br />

considerably different between browsers) by increasing the width variable and then using<br />

the variable as the width argument in the fillRect() method.<br />

Once the width variable has reached 400, we then change over to use the pos variable<br />

instead. In this part <strong>of</strong> the conditional, we increase the pos variable by two (the rectangle<br />

will appear to move two pixels at a time, again for speed), reset the by setting<br />

its width, and set the fillStyle property. We then draw the new rectangle, using the pos<br />

variable as the argument for the x axis position.<br />

It will look as if the rectangle is being moved to the right, but this is not the case at all. We<br />

are actually destroying the rectangle and then drawing a completely new one two pixels to<br />

the right <strong>of</strong> the original.

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

Saved successfully!

Ooh no, something went wrong!