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

句法

void ctx.fill([fillRule]);
void ctx.fill(path [, fillRule]);
					

参数

fillRule
The algorithm by which to determine if a point is inside or outside the filling region.
可能的值:
path
Path2D path to fill.

范例

Filling a rectangle

This example fills a rectangle with the fill() 方法。

HTML

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

JavaScript

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

结果

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');
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.fill' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
fill Chrome Yes Edge 12 Firefox 1.5 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
路径 参数 Chrome Yes Edge ≤18 Firefox 31 IE No Opera Yes Safari No WebView Android Yes Chrome Android Yes Firefox Android 31 Opera Android ? Safari iOS ? Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: