25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 2-7. Stylish stroked tree canopy<br />

Working with Fill Styles<br />

CHAPTER 2 ■ USING THE <strong>HTML</strong>5 CANVAS API<br />

As you might expect, stroking is not the only way to affect the appearance of canvas shapes. The next<br />

common way to modify a shape is to specify how its paths and subpaths are filled. Listing 2-11 shows<br />

how simple it is to fill our canopy with a pleasant, green color.<br />

Listing 2-11. Using a fill style<br />

// Set the fill color to green and fill the canopy<br />

context.fillStyle = '#339900';<br />

context.fill();<br />

First, we set the fillStyle to the appropriate color. As we will see later, it is also possible to set the<br />

fill to be a gradient or an image pattern. Then, we simply call the context’s fill function to let the canvas<br />

fill all the pixels inside all the closed paths of our current shape, as shown in Figure 2-8.<br />

39

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

Saved successfully!

Ooh no, something went wrong!