HTML canvas fill() Method
Example
Draw two 150*100 pixels rectangles. Fill one with a red color and the other with a blue color:
JavaScript:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.rect(20, 20, 150, 100);
ctx.fillStyle = "red";
ctx.fill();
ctx.beginPath();
ctx.rect(40, 40, 150, 100);
ctx.fillStyle = "blue";
ctx.fill();
Try it yourself »
Browser Support
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the fill() method.
Note: Internet Explorer 8 and earlier versions, do not support the <canvas> element.
Definition and Usage
The fill() method fills the current drawing (path). The default color is black.
Tip: Use the fillStyle property to fill with another color/gradient.
Note: If the path is not closed, the fill() method will add a line from the last point to the startpoint of the path to close the path (like closePath()), and then fill the path.
JavaScript syntax: | context.fill(); |
---|
HTML Canvas Reference