HTML Canvas 的 stroke() 方法

htmlweb developmentfront end technology更新于 2025/6/16 20:28:17

HTML Canvas 的 stroke() 方法用于绘制路径。此路径由 moveTo() 和 lineTo() 方法绘制。<canvas> 元素允许您使用 JavaScript 在网页上绘制图形。每个 Canvas 都有两个元素,分别描述 Canvas 的高度和宽度。

以下是语法 −

ctx.stroke()

现在让我们看一个实现 Canvas 的 stroke() 方法的示例 −

示例

<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="450" height="350" style="border:2px solid red;">
</canvas>
<script>
   var c = document.getElementById("newCanvas");
   var ctx = c.getContext("2d");
   ctx.beginPath();
   ctx.moveTo(100, 200);
   ctx.lineTo(100, 100);
   ctx.strokeStyle = "blue";
   ctx.stroke();
   ctx.beginPath();
   ctx.moveTo(30, 30);
   ctx.lineTo(20, 100);
   ctx.lineTo(70, 100);
   ctx.strokeStyle = "orange";
   ctx.stroke();
</script>
</body>
</html>

输出


相关文章


有用资源