W3cubDocs

/Web APIs

CanvasRenderingContext2D: fill() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

The CanvasRenderingContext2D.fill() method of the Canvas 2D API fills the current or given path with the current fillStyle.

Syntax

fill()
fill(path)
fill(fillRule)
fill(path, fillRule)

Parameters

fillRule

The algorithm by which to determine if a point is inside or outside the filling region. Possible values:

nonzero

The non-zero winding rule. Default rule.

evenodd

The even-odd winding rule.

path

A Path2D path to fill.

Return value

None (undefined).

Examples

>

Filling a rectangle

This example fills a rectangle with the fill() method.

HTML

<canvas id="canvas"></canvas>

JavaScript

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.rect(10, 10, 150, 100);
ctx.fill();

Result

Specifying a path and a fillRule

This example saves some intersecting lines to a Path2D object. The fill() method is then used to render the object to the canvas. A hole is left unfilled in the object's center by using the "evenodd" rule; by default (with the "nonzero" rule), the hole would also be filled.

HTML

<canvas id="canvas"></canvas>

JavaScript

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

// Create path
let region = new Path2D();
region.moveTo(30, 90);
region.lineTo(110, 20);
region.lineTo(240, 130);
region.lineTo(60, 130);
region.lineTo(190, 20);
region.lineTo(270, 90);
region.closePath();

// Fill path
ctx.fillStyle = "green";
ctx.fill(region, "evenodd");

Result

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
fill 1 12 1.5 ≤12.1 2 18 4 ≤12.1 1 1.0 4.4 1
path_parameter 36 14 31 23 7 36 31 24 7 3.0 37 7

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fill