HTML canvas stroke() 方法
HTML canvas 的 stroke() 方法用於繪製路徑。此路徑使用 moveTo() 和 lineTo() 方法繪製。<canvas> 元素允許你使用 JavaScript 在網頁上繪製圖形。每個 canvas 都有兩個元素,分別描述 canvas 的高度和寬度,即 height 和 width。
以下是語法 −
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>
輸出
廣告