ClosePath() | HTML<canvas>方法
定义与用法
closePath() 方法创建从当前点到开始点的路径。
提示:请使用 stroke() 方法在画布上绘制确切的路径。
提示:请使用 fill() 方法来填充图像(默认是黑色)。请使用 fillStyle 属性来填充另一个颜色/渐变。
语法格式
context.closePath();
示例代码
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.closePath();
ctx.stroke();
</script>
效果展示:
浏览器支持
方法 | |||||
---|---|---|---|---|---|
ClosePath() | Yes | Yes | Yes | Yes | Yes |
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 closePath() 方法。
提示:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
更多示例
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = "red";
ctx.fill();
</script>