Metode stroke
Metode stroke dipanggil untuk menyelesaikan
penggambaran garis dengan bantuan lineTo,
serta untuk mengoutline kontur bentuk yang digambar,
contohnya, melalui metode rect.
Sintaks
konteks.stroke()
Contoh
Mari gambar sebuah garis:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.stroke();
:
Contoh
Mari gambar sebuah kotak:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(150, 50);
ctx.lineTo(150, 150);
ctx.lineTo(50, 150);
ctx.closePath();
ctx.stroke();
:
Lihat juga
-
metode
fill,
yang digunakan untuk mengisi bentuk dengan latar belakang -
properti
strokeStyle,
yang mengatur warna garis