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

}; context.beginPath(); context.moveTo(50, 50); context.lineTo(195, 125); context.lineTo(165, 125); context.lineTo(50, 66); context.fill(); context.closePath(); context.beginPath(); context.moveTo(450, 50); context.lineTo(305, 125); context.lineTo(275, 125); context.lineTo(422, 50); context.lineTo(450, 50); context.fill(); context.closePath(); context.beginPath(); context.moveTo(450, 250); context.lineTo(310, 175); context.lineTo(335, 175); context.lineTo(450, 235); context.lineTo(450, 250); context.fill(); context.closePath(); context.beginPath(); context.moveTo(50, 250); context.lineTo(200, 175); context.lineTo(225, 175); context.lineTo(80, 250); context.lineTo(50, 250); context.fill(); context.closePath(); if (window.ActiveXObject) { window.onload = function() { var context = canvas.getContext("2d"); draw(context); } } else { var context = canvas.getContext("2d"); draw(context); } [ 279 ] Chapter 10

Canvas Animations 3. Save the new page and view it in IE. Our flag should now be visible: IE can be made to understand the element, as we see in the previous screenshot, although its support is not completely identical to that of capable browsers. If we compare our example in IE and Firefox alongside each other, we see that IE also slightly enlarges the flag for some reason. What just happened? First of all we need to link to the explorercanvas library. We don't want to let normal browsers that support the native element use this file as it will slow them down, so we put the element into an IE-specific conditional comment (like we did with the html5shiv file earlier in the book). The .compiled version of the script file is simply a minified version for production use. The next change we make is to put the methods that draw the flag into an inline function stored as a variable. This is necessary because otherwise IE will attempt to use these drawing methods before the explorercanvas library has finished initializing and will throw errors. The next part of our code also deals with this. We use a conditional if statement to check for the presence of an ActiveXObject property of the window object (this will only exist in IE). If it is found, we attach an onload handler to the of the page that calls the getContext() method and our draw() function once the page has finished loading, and the explorercanvas library has done its thing. [ 280 ]

};<br />

context.beginPath();<br />

context.moveTo(50, 50);<br />

context.lineTo(195, 125);<br />

context.lineTo(165, 125);<br />

context.lineTo(50, 66);<br />

context.fill();<br />

context.closePath();<br />

context.beginPath();<br />

context.moveTo(450, 50);<br />

context.lineTo(305, 125);<br />

context.lineTo(275, 125);<br />

context.lineTo(422, 50);<br />

context.lineTo(450, 50);<br />

context.fill();<br />

context.closePath();<br />

context.beginPath();<br />

context.moveTo(450, 250);<br />

context.lineTo(310, 175);<br />

context.lineTo(335, 175);<br />

context.lineTo(450, 235);<br />

context.lineTo(450, 250);<br />

context.fill();<br />

context.closePath();<br />

context.beginPath();<br />

context.moveTo(50, 250);<br />

context.lineTo(200, 175);<br />

context.lineTo(225, 175);<br />

context.lineTo(80, 250);<br />

context.lineTo(50, 250);<br />

context.fill();<br />

context.closePath();<br />

if (window.ActiveXObject) {<br />

window.onload = function() {<br />

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

draw(context);<br />

}<br />

} else {<br />

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

draw(context);<br />

}<br />

[ 279 ]<br />

Chapter 10

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

Saved successfully!

Ooh no, something went wrong!