HTML canvas fill()Method

Canvas 对象参考手册Canvas Object

Example

Draw a 150*100 pixel rectangle, then fill it with red:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.rect(20,20,150,100);
ctx.fillStyle="red";
ctx.fill();

Try it Yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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 image (path). The default color is black.

Tip:Please usefillStyleproperty to fill with another color/gradient.

Note:If the path is not closed, the fill() method will add a line from the end point to the start point to close the path (just likeclosePath()), and then fill the path.

JavaScript Syntax: context.fill();


Canvas 对象参考手册Canvas Object

Other Extensions