HTML canvas strokeText() method

Definition and Usage

strokeText() Method to draw text on the canvas (without fill color). The default color is black.

Tip:Please use font property to define the font and size, and use strokeStyle Properties render text in another color/gradient.

Example

Use strokeText() to write text "Hello world!" and "codew3c.com" on the canvas:

Your browser does not support the HTML5 canvas tag.

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 color with gradient
ctx.strokeStyle=gradient;
ctx.strokeText("codew3c.com",10,90);

Try It Yourself

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 where the text drawing starts (relative to the canvas).
y The y coordinate position where the text drawing starts (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.