HTML 画布 fillStyle 属性
htmlweb developmentfront end technology更新于 2026/1/30 6:04:17
HTML 画布的 fillStyle() 属性用于设置绘图的颜色、渐变或图案。默认值为 #000000。<canvas> 元素允许您使用 JavaScript 在网页上绘制图形。每个画布都有两个元素,分别描述画布的高度和宽度,即 height 和 width。
以下是语法 −
ctx.fillStyle=color|gradient|pattern;
以上值包含 −
- color:绘图的填充颜色,CSS 颜色。
- gradient:用于填充绘图的线性或径向渐变对象。
- pattern:用于填充绘图的图案对象。
现在让我们看一个实现 Canvas 的 fillStyle() 属性的示例 −
示例
<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="500" height="350" style="border:2px solid orange;">
</canvas>
<script>
var c = document.getElementById("newCanvas");
var ctx = c.getContext("2d");
var newGrad =ctx.createLinearGradient(0, 0, 130, 0);
newGrad.addColorStop(0, "blue");
newGrad.addColorStop(0.8, "green");
ctx.fillStyle = newGrad;
ctx.fillRect(0, 0, 500, 350);
</script>
</body>
</html>
输出

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

