HTML canvas stroke() Method
Example
Draw a path, shaped as the letter L - in a red color:
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.strokeStyle = "red";
ctx.stroke();
Try it Yourself »
Description
The stroke() method actually draws the path you have defined with all those moveTo() and lineTo() methods. The default color is black.
Tip: Use the strokeStyle property to draw with another color/gradient.
JavaScript syntax: | context.stroke(); |
---|
Browser Support
The <canvas>
element is an HTML5 standard (2014).
stroke()
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
❮ Canvas API