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

Download from Wow! eBook context.lineWidth = 1; function makeStripe(props) { context.beginPath(); context.moveTo(props.startX, props.startY); context.lineTo(props.line1X, props.line1Y); context.lineTo(props.line2X, props.line2Y); context.lineTo(props.line3X, props.line3Y); context.fill(); context.closePath(); } setTimeout(function() { makeStripe({ startX: 50, startY: 50, line1X: 195, line1Y: 125, line2X: 165, line2Y: 125, line3X: 50, line3Y: 66 })}, 1); setTimeout(function() { makeStripe({ startX: 450, startY: 50, line1X: 305, line1Y: 125, line2X: 275, line2Y: 125, line3X: 422, line3Y: 50 })}, 50); setTimeout(function() { makeStripe({ startX: 450, startY: 250, line1X: 310, line1Y: 175, line2X: 335, line2Y: 175, line3X: 450, line3Y: 235 })}, 100); setTimeout(function() { makeStripe({ startX: 50, startY: 250, line1X: 200, line1Y: 175, line2X: 225, line2Y: 175, line3X: 80, line3Y: 250 })}, 150); }, growVertical = function() { if (height < 100 || width < 200) { if (height < 100) { context.beginPath(); context.moveTo(pos.up.x, pos.up.y); [ 287 ] Chapter 10

Canvas Animations }, context.lineTo(pos.up.x, pos.up.y - 2); context.moveTo(pos.down.x, pos.down.y); context.lineTo(pos.down.x, pos.down.y + 2); context.stroke(); context.closePath(); height = height + 2; pos.up.y = pos.up.y - 2; pos.down.y = pos.down.y + 2; } if (width < 200) { context.beginPath(); context.moveTo(pos.left.x, pos.left.y); context.lineTo(pos.left.x - 2, pos.left.y); context.moveTo(pos.right.x, pos.right.y); context.lineTo(pos.right.x + 2, pos.right.y); context.stroke(); context.closePath(); width = width + 2 pos.left.x = pos.left.x - 2; pos.right.x = pos.right.x + 2; } } else { clearInterval(crossInt); addStripes(); } }, crossInt = setInterval( function() { growVertical() }, 1); What just happened? Again, we have an outer inline function (called redCross()) containing some properties that set the color and line styles, and some nested functions that will be used to draw the red cross and the four custom shapes. As with the previous function, we declare width and height control variables, and an object called pos containing the starting positions for the lines that make up the cross. The cross is drawn first with the growVertical() function. This function is very similar to the function in the last section of code. We draw four lines starting in the middle of the rectangle which radiate to the top and bottom center, and the right and left center. [ 288 ]

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

},<br />

context.lineTo(pos.up.x, pos.up.y - 2);<br />

context.moveTo(pos.down.x, pos.down.y);<br />

context.lineTo(pos.down.x, pos.down.y + 2);<br />

context.stroke();<br />

context.closePath();<br />

height = height + 2;<br />

pos.up.y = pos.up.y - 2;<br />

pos.down.y = pos.down.y + 2;<br />

}<br />

if (width < 200) {<br />

context.beginPath();<br />

context.moveTo(pos.left.x, pos.left.y);<br />

context.lineTo(pos.left.x - 2, pos.left.y);<br />

context.moveTo(pos.right.x, pos.right.y);<br />

context.lineTo(pos.right.x + 2, pos.right.y);<br />

context.stroke();<br />

context.closePath();<br />

width = width + 2<br />

pos.left.x = pos.left.x - 2;<br />

pos.right.x = pos.right.x + 2;<br />

}<br />

} else {<br />

clearInterval(crossInt);<br />

addStripes();<br />

}<br />

},<br />

crossInt = setInterval( function() { growVertical() }, 1);<br />

What just happened?<br />

Again, we have an outer inline function (called redCross()) containing some properties<br />

that set the color and line styles, and some nested functions that will be used to draw the<br />

red cross and the four custom shapes. As with the previous function, we declare width and<br />

height control variables, and an object called pos containing the starting positions for the<br />

lines that make up the cross. The cross is drawn first with the growVertical() function.<br />

This function is very similar to the function in the last section <strong>of</strong> code. We draw four lines<br />

starting in the middle <strong>of</strong> the rectangle which radiate to the top and bottom center, and the<br />

right and left center.<br />

[ 288 ]

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

Saved successfully!

Ooh no, something went wrong!