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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

We then define two inline functions, one to create the vertical cross and the other to create<br />

the diagonal cross. The growVertical() function is called first with an interval and we<br />

just draw one white rectangle from top to bottom, and one from left to right in the center <strong>of</strong><br />

the background using the width and height variables to repeat the interval as many times<br />

as necessary. The interval is cleared once the rectangles are the correct size and then the<br />

growDiagonal() function is called with another interval.<br />

In this function we need to draw four lines, each starting in the middle <strong>of</strong> the vertical cross.<br />

We use the different properties in our pos object to do this. Each time the function is<br />

executed, we move to the x and y positions specified for each line in the object and then<br />

draw towards the relevant corner. We then update the properties in the object ready for the<br />

next iteration <strong>of</strong> the function.<br />

The properties each need to be updated by different amounts, for example, the line moving<br />

from the center to the top-left <strong>of</strong> the rectangle need to move negatively along both the x and<br />

y axes, whereas the line to move to the top-right corner needs to move positively along the<br />

x axis, but negatively along the y axis. We use a new path on each iteration <strong>of</strong> the function to<br />

preserve the anti-aliasing <strong>of</strong> the lines.<br />

Once the lines are drawn we clear the interval and call the next function. We'll define<br />

this function now. It should be placed after the canvas variable, but directly before the<br />

whiteLines() function that we just added.<br />

Time for action – animating the red crosses<br />

All we need to do now is draw the vertical red cross and the four custom red shapes. Add the<br />

following code in between the rectInt variable declaration near the top <strong>of</strong> the <br />

and the whiteLines function we defined in the previous section:<br />

redCross = function(context) {<br />

context.fillStyle = "#C00";<br />

context.strokeStyle = "#C00";<br />

context.lineWidth = 30;<br />

var width = 0,<br />

height = 0,<br />

pos = {<br />

up : { x: 250, y: 150 },<br />

down : { x: 250, y: 150 },<br />

left: { x: 250, y: 150 },<br />

right: { x: 250, y: 150 }<br />

},<br />

addStripes = function() {<br />

[ 286 ]

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

Saved successfully!

Ooh no, something went wrong!