Canvas closePath() 方法
示例
繪製一個 L 形路徑,並透過一條線回到起點來閉合路徑
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.closePath();
ctx.stroke();
自己動手試一試 »
描述
closePath() 方法會從當前點建立一個回到起點的路徑。
使用 stroke() 或 fill() 方法來繪製路徑。
另請參閱
beginPath() 方法 (開始一個新路徑)
moveTo() 方法 (將路徑移動到指定點)
lineTo() 方法 (向路徑新增一條線)
fill() 方法 (填充當前路徑)
stroke() 方法 (繪製當前路徑)
語法
| context.closePath() |
引數
| 無 |
返回值
| 無 |
更多示例
示例
用黑色填充路徑
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.closePath();
ctx.stroke();
自己動手試一試 »
示例
用紅色填充路徑
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.closePath();
ctx.fillStyle = "red";
ctx.fill();
自己動手試一試 »
瀏覽器支援
<canvas> 元素是 HTML5 標準(2014)。
closePath() 在所有現代瀏覽器中都受支援
| Chrome | Edge | Firefox | Safari | Opera | IE |
| 是 | 是 | 是 | 是 | 是 | 9-11 |
❮ Canvas 參考