HTML and CSS Reference
In-Depth Information
As you can see, the line is mostly obscured by the rectangle. You might
think that if you could remove the rectangle the line would still be there
underneath; but after you've drawn over it, the line is gone.
The only way to get the line back is to erase both the rectangle
and the line and then draw the line again. The <canvas> element
doesn't store the elements drawn, only the resulting pixels.
What about other shapes? The path-then-stroke approach is the way to
do it. You can use the arc method to draw a circle and then fill it. The
arc method accepts parameters for the location of the center; the
radius; how far around, in radians, the arc should extend; and whether
that should be clockwise or counterclockwise:
function draw(){
var canvas = document
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgb(255,0,0)';
ctx.fillStyle = 'rgb(0,255,0)';
ctx.arc(250, 100,
50, 0,
ctx.strokeStyle =
ctx.lineWidth = 5;
Search WWH ::

Custom Search