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 />

For the next part <strong>of</strong> the flag, we need to draw a slightly thinner red cross over the white<br />

cross. We'll use another path for this. We set the new color style and width, and draw a new<br />

path across the center <strong>of</strong> the rectangle vertically and horizontally again.<br />

To complete the flag, we need to add four more shapes to make the diagonal parts <strong>of</strong> the red<br />

cross. We can't use straight-line paths for these because they don't intersect, and they are<br />

all positioned slightly differently. This means that we have to draw them manually as custom<br />

shapes and fill them.<br />

These four shapes actually make up the majority <strong>of</strong> the code, but we're basically doing very<br />

similar things as before. Each shape is made by drawing sub-paths and filling them. We use a<br />

new path for each shape to preserve the anti-aliasing <strong>of</strong> the lines. If we used one big path for<br />

all four shapes, the edges <strong>of</strong> the shapes would be jagged.<br />

Pop quiz – drawing to the canvas<br />

1. What arguments are required for the fillRect() method?<br />

a. The x and y location <strong>of</strong> the rectangle<br />

b. The width and height <strong>of</strong> the rectangle<br />

c. The x and y location <strong>of</strong> the rectangle, its width and height, and its color<br />

d. The x and y location <strong>of</strong> the rectangle, and its width and height<br />

2. What method is required to make a path visible?<br />

a. strokeStyle and lineWidth<br />

b. moveTo() and lineTo()<br />

c. stroke() or fill()<br />

d. closePath()<br />

Have a go hero – creating the flag <strong>of</strong> your nation<br />

If you're not from the UK, have a go at drawing the flag <strong>of</strong> your own nation on the canvas.<br />

We can create compositions <strong>of</strong> repeated shapes using standard JavaScript for loops, so use<br />

this to your advantage in keeping the code required for your flag as minimal as possible.<br />

If you are from the UK, try recreating a favorite logo or icon.<br />

If part <strong>of</strong> your flag (or logo) is extremely complex, remember that we can draw images to<br />

the as well as lines and shapes, so feel free to draw out the basic part <strong>of</strong> your<br />

flag using the drawing methods, and then use an image for the complex part.<br />

[ 276 ]

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

Saved successfully!

Ooh no, something went wrong!