Canvas strokeText() Method
Definition and usage
strokeText()
Method to draw text on the canvas (without filling). The default text color is black.
Tip:Please use font Property to define font and size, and use strokeStyle Properties to render text in another color/gradient.
Example
Use strokeText() to write the text "Hello world!" and "codew3c.com" on the canvas:
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="20px Georgia"; ctx.strokeText("Hello World!",10,50); ctx.font="30px Verdana"; // Create gradient var gradient=ctx.createLinearGradient(0,0,c.width,0); gradient.addColorStop("0","magenta"); gradient.addColorStop("0.5","blue"); gradient.addColorStop("1.0","red"); // Fill with gradient ctx.strokeStyle=gradient; ctx.strokeText("codew3c.com",10,90);
Syntax
context.strokeText(text,x,y,maxWidth);
Parameter value
Parameter | Description |
---|---|
text | Specifies the text to be output on the canvas. |
x | The x coordinate position for the start of the text drawing (relative to the canvas). |
y | The y coordinate position for the start of the text drawing (relative to the canvas). |
maxWidth | Optional. The maximum text width in pixels. |
Browser Support
The numbers in the table indicate the first browser version that fully supports this attribute.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.