HTML and CSS Reference
Listing 4-5. Using Canvas to Make a Star Shape
//get a reference to the canvas
var canvas = document.getElementById('adCanvas');
var context = canvas.getContext('2d');
//draw a star
context.fillStyle = 'rgb(255, 255, 0)';
You may have noticed that the dimensions of the canvas element needed to be increased to 200 × 200 to view the
full star. Keep this in mind if you try to paint something larger than the canvas dimension supports, as your painting
will render off the element and produce clipping.
Looking at Listing 4-5, you can see that the same methods are being used to draw lines, but this time there's a bit
more complexity in where the starting and ending points are placed. You can also see that a fillStyle method has
been added and given a value of yellow in the RGB (red-green-blue) space. If you're following along, you should get
the image in Figure 4-3 in your browser.
Figure 4-3. A star produced using the canvas element