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

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 ]

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 ]

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

Saved successfully!

Ooh no, something went wrong!