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.

[ 275 ]<br />

Chapter 10<br />

In the previous screenshot, we can see the simple arrangement <strong>of</strong> geometric shapes that<br />

make up the British flag (note that the flag is not completely to scale). Images like this are<br />

easy to produce using the but even simple shapes can require a lot <strong>of</strong> code.<br />

What just happened?<br />

The first thing we do is get the element using JavaScript's getElementById()<br />

method, and then get the two-dimensional context object from the with the<br />

getContext() method. We can now interact with the via the context object.<br />

We set some <strong>of</strong> the color for the context using the fillStyle property, and then draw a<br />

solid rectangle using the fillRect() method. The arguments specified are the starting x<br />

and y location <strong>of</strong> the rectangle, and the width and height.<br />

The filled rectangle picks up the fill style that we just set which is deep blue and will form the<br />

background <strong>of</strong> our flag. We now need to create a white horizontal and diagonal cross on top<br />

<strong>of</strong> the blue background. We can do this by drawing two thick lines across the middle <strong>of</strong> the<br />

flag, one vertical and one horizontal. We'll use paths for this, so we start a new path using<br />

the beginPath() method.<br />

Next we set the color <strong>of</strong> the stroke to white using the strokeStyle property, and<br />

the width <strong>of</strong> the path using the lineWidth property. To draw a path we have tell the<br />

(or the context object actually) where to start the path, which we do using the<br />

moveTo() method, specifying the coordinates to move to as arguments (the top middle<br />

<strong>of</strong> the rectangle).<br />

To make the path, we then use the lineTo() method, specify the coordinates <strong>of</strong> where to<br />

end the path (the bottom middle <strong>of</strong> the rectangle). This gives us the vertical line. To make the<br />

horizontal path, we repeat the same process, moving to the left middle <strong>of</strong> the rectangle and<br />

drawing to the right middle.<br />

Coordinates specified using the moveTo() method are always relative to the canvas itself<br />

with 0, 0 representing the top-left corner <strong>of</strong> the canvas. This is the same for the lineTo()<br />

method as well, even though the line that is drawn begins at the point specified by the last<br />

call <strong>of</strong> moveTo().<br />

Next we need to make a diagonal white cross over the background rectangle and the vertical<br />

cross, which we'll do by drawing paths in the same way as before using combinations <strong>of</strong><br />

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

All <strong>of</strong> the paths we've added so far are part <strong>of</strong> the same path—they are sub-paths, and at<br />

this point they aren't actually visible. To make them visible, we need to either fill or stroke<br />

them, so we stroke them with the stroke() method and then close the path with the<br />

closePath() method.

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

Saved successfully!

Ooh no, something went wrong!