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
"nonzero"
:
non-zero winding rule
. Default rule.
"evenodd"
:
even-odd winding rule
.
path
Path2D
path to fill.
This example fills a rectangle with the
fill()
方法。
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.rect(10, 10, 150, 100);
ctx.fill();
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.
<canvas id="canvas"></canvas>
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. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
兼容性未知
CanvasRenderingContext2D
CanvasRenderingContext2D.fillStyle
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()