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

The properties we can set on text include: Property Usage font Sets the size and the font-family of the text. [ 271 ] Chapter 10 textAlign Sets the alignment of the text. Can be either start (the default), end, left, right, or center. textBaseline Sets the baseline of the text. Can be either alphabetic (the default), top, hanging, middle, ideographic, or bottom. Transformation methods The can have the same transforms applied to it that we saw in the last chapter, which can be applied using the following methods: Method Usage rotate(a) Rotates a shape by the specified number of radians. scale(a, b) Scales a shape along each axis by the specified amount, with a being the x axis and b the y axis. translate(a, b) Translates the shape along each axis by the specified amount, with a being the x axis and b the y axis. transform(a, b, c, d, e, f) The transform() method is equivalent to the matrix transform form function and can be used in the same way to scale, translate, and/or skew the shape. Pixel manipulation The even allows us to work directly with the pixels in the canvas and can retrieve shapes as imageData objects, or create shapes directly by manipulating the at pixel-level. We have the following methods for manipulating pixels: Method Usage createImageData(a, b) Creates a new, blank imageData object using the supplied arguments as width and height properties. This method can also be passed another imageData object, which will cause the method to return an (empty) imageData object the same width and height as the original. getImageData(a, b, c, d) Returns an imageData object containing the pixel data for the specified area of the . Arguments a and b are the start coordinates of the area, c and d are the width and height. putImageData(a, b, c) Paints the pixel data to the . The first argument is the imageData object to use, the second and third are the start coordinates of the resulting shape.

Canvas Animations All imageData objects, either those we get from the , or those we create with the createImageDate() method have several properties we can make use of, including: Property Usage data This property is a CanvasPixelArray, and is read-only when we get an imageData object from the . We can also use it to set pixel data in an imageData object we create. The array contains four items per-pixel: the r, g, and b values for the pixel, and the alpha. height The height of the image represented by the imageData object. This property is read-only. length The length of the CanvasPixelArray in bytes. This property is read-only. width The width of the image represented by the imageData object. This property is read-only. Drawing to the canvas Drawing to the programmatically is very straight forward in theory. The methods and properties are easy to use, and are quite consistent between supporting browsers. Direct pixel manipulation is the trickiest part of the API to master, but other than that there is nothing really complicated. One thing we do find is that our code can very quickly mount up. As soon as we're drawing multiple, complex shapes, and setting various properties, our code can easily run to a few hundred lines or more even for relatively simple drawings. This is especially true when animating the contents of the . Time for action – drawing to the canvas Let's take a look at a quick example of drawing a non-animated shape. We don't even need jQuery for this. 1. Add the element to the of our template file: Your browser doesn't support the canvas element! [ 272 ]

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

All imageData objects, either those we get from the , or those we create with the<br />

createImageDate() method have several properties we can make use <strong>of</strong>, including:<br />

Property Usage<br />

data This property is a CanvasPixelArray, and is read-only when<br />

we get an imageData object from the . We can also<br />

use it to set pixel data in an imageData object we create. The<br />

array contains four items per-pixel: the r, g, and b values for<br />

the pixel, and the alpha.<br />

height The height <strong>of</strong> the image represented by the imageData<br />

object. This property is read-only.<br />

length The length <strong>of</strong> the CanvasPixelArray in bytes. This<br />

property is read-only.<br />

width The width <strong>of</strong> the image represented by the imageData object.<br />

This property is read-only.<br />

Drawing to the canvas<br />

Drawing to the programmatically is very straight forward in theory. The methods<br />

and properties are easy to use, and are quite consistent between supporting browsers.<br />

Direct pixel manipulation is the trickiest part <strong>of</strong> the API to master, but other than that there<br />

is nothing really complicated.<br />

One thing we do find is that our code can very quickly mount up. As soon as we're drawing<br />

multiple, complex shapes, and setting various properties, our code can easily run to a few<br />

hundred lines or more even for relatively simple drawings. This is especially true when<br />

animating the contents <strong>of</strong> the .<br />

Time for action – drawing to the canvas<br />

Let's take a look at a quick example <strong>of</strong> drawing a non-animated shape. We don't even need<br />

<strong>jQuery</strong> for this.<br />

1. Add the element to the <strong>of</strong> our template file:<br />

<br />

Your browser doesn't support the canvas element!<br />

<br />

[ 272 ]

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

Saved successfully!

Ooh no, something went wrong!