导航
×
   ❮   
HTML CSS JavaScript PHP Go Sass W3C Colors ECMS

HTML canvas fill() 方法


实例

绘制一个 150*100 像素的矩形,并将其填充为红色

您的浏览器不支持 HTML5 canvas 标签。

JavaScript

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


ctx.rect(20, 20, 150, 100);
ctx.fillStyle = "red";
ctx.fill();
亲自试一试 »

描述

fill() 方法用于填充当前路径。

默认的 fillStyle 是 #000000(实心黑)。

fill() 方法会在填充之前关闭路径,就像调用 closePath() 一样。

另请参阅

fillStyle 属性

beginPath() 方法 (开始一条新路径)

closePath() 方法 (关闭当前路径)

moveTo() 方法 (将路径移动到指定点)

lineTo() 方法 (向路径添加一条线)

stroke() 方法 (绘制当前路径)


语法

context.fill()

参数


语法


浏览器支持

<canvas> 元素是 HTML5 标准(2014)。

fill() 在所有现代浏览器中都受支持

           
Chrome Edge Firefox Safari Opera IE
9-11

Copyright ©2020-2026 freew3c.com All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。