HTML canvas fill() Method
Example
Draw a 150*100 pixels rectangle, and fill it with the color red:
JavaScript:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.rect(20, 20, 150, 100);
ctx.fillStyle = "red";
ctx.fill();
Try it Yourself »
Description
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(); |
---|
Browser Support
The <canvas>
element is an HTML5 standard (2014).
fill()
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
❮ Canvas API