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

[ 281 ] Chapter 10 If the browser is not IE, we simply get the context and call our draw() function straight away. Note that we pass the context object into the draw() function as an argument so that the API methods work correctly. Other than these changes, our code is the same and should now function as intended in IE versions 8 and below. Pop Quiz – supporting IE 1. We wrap the getContext() method in a conditional comment that checks for IE and uses an onload handler attached to the . Why? a. The can only be interacted with via the element in IE b. To give explorercanvas a chance to add getContext() support to the in IE c. To prevent memory leaks in IE d. A handler function must be used with explorercanvas 2. Which methods/techniques do now work in IE when using explorercanvas? a. Scaling and Bezier curves b. Radial gradients and quadratic curves c. Radial gradients, clearRect(), and non-uniform scaling d. Non-uniform scaling, PNG-based images, and stroked text Have a go hero – extending IE support Convert your own flag (or logo/alternative) so that it works in IE using the explorercanvas library. You'll more than likely need to make use of an onload event handler to ensure that the getContext() method isn't called until the is ready to be used. Animating the canvas The methods we've looked at so far are easy to use and nothing if not a little repetitive. Animating the objects on the is where things start to get interesting. Animating is harder than simply drawing on the and as we have no real way of debugging it other than trial and error, solving bugs can quickly become problematic and somewhat time-consuming. In our flag example, there was no real benefit to using the . We could have got exactly the same effect, with much less code and processing, by simply including an image of the flag on our page. However, animating the is where its benefits really begin. This is where we can do much more than anything we could achieve with a simple image. The additional complexity that animating the entails is totally worth it.

Canvas Animations Time for action – creating an animation on the canvas In this example, we'll draw the same flag as we did before, except that this time we'll animate the different shapes. The underlying HTML used in this example is exactly the same as in the previous examples. All that changes is the contents of the element at the end of the . 1. To make the working file for this example, just remove everything in the element at the bottom of canvas-explorer.html and resave the file as canvas-animated.html. 2. The first thing we'll do is bring the blue rectangle in from the side of the canvas to the center of the element. Add the following code to the now empty element at the bottom of the page: (function() { var canvas = document.getElementById("c"), init = function(context) { }; var width = 0, pos = 0, rectMotion = function() { if (width < 400) { width = width + 2; context.fillStyle = "#039"; context.fillRect(0, 50, width, 200); } else if (pos < 50) { pos = pos + 2; canvas.width = 500; context.fillStyle = "#039"; context.fillRect(pos, 50, 400, 200); } else { clearInterval(rectInt); whiteLines(context); } }, rectInt = setInterval(function() { rectMotion() }, 1); if (window.ActiveXObject) { window.onload = function() { [ 282 ]

[ 281 ]<br />

Chapter 10<br />

If the browser is not IE, we simply get the context and call our draw() function straight<br />

away. Note that we pass the context object into the draw() function as an argument so that<br />

the API methods work correctly. Other than these changes, our code is the same and should<br />

now function as intended in IE versions 8 and below.<br />

Pop Quiz – supporting IE<br />

1. We wrap the getContext() method in a conditional comment that checks for IE<br />

and uses an onload handler attached to the . Why?<br />

a. The can only be interacted with via the element in IE<br />

b. To give explorercanvas a chance to add getContext() support to the<br />

in IE<br />

c. To prevent memory leaks in IE<br />

d. A handler function must be used with explorercanvas<br />

2. Which methods/techniques do now work in IE when using explorercanvas?<br />

a. Scaling and Bezier curves<br />

b. Radial gradients and quadratic curves<br />

c. Radial gradients, clearRect(), and non-uniform scaling<br />

d. Non-uniform scaling, PNG-based images, and stroked text<br />

Have a go hero – extending IE support<br />

Convert your own flag (or logo/alternative) so that it works in IE using the explorercanvas<br />

library. You'll more than likely need to make use <strong>of</strong> an onload event handler to ensure that<br />

the getContext() method isn't called until the is ready to be used.<br />

Animating the canvas<br />

The methods we've looked at so far are easy to use and nothing if not a little<br />

repetitive. Animating the objects on the is where things start to get interesting.<br />

Animating is harder than simply drawing on the and as we have no real way <strong>of</strong><br />

debugging it other than trial and error, solving bugs can quickly become problematic and<br />

somewhat time-consuming.<br />

In our flag example, there was no real benefit to using the . We could have got<br />

exactly the same effect, with much less code and processing, by simply including an image<br />

<strong>of</strong> the flag on our page. However, animating the is where its benefits really begin.<br />

This is where we can do much more than anything we could achieve with a simple image.<br />

The additional complexity that animating the entails is totally worth it.

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

Saved successfully!

Ooh no, something went wrong!