Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas strokeText() method

HTML canvas strokeText() method

HTML canvas Reference HTML canvas Reference Manual

Example

Using strokeText(), write the text "Hello world!" and "Big smile!" on the canvas (with a gradient):

YourbrowserdoesnotsupporttheHTML5canvastag.

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("Big smile!",10,90);

Try it »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support the strokeText() method.

**Note:** Internet Explorer 8 and earlier versions do not support .

**Note:** Safari does not support the maxWidth parameter.


Definition and Usage

The strokeText() method draws text on the canvas (without fill color). The default text color is black.

**Tip:** Please use font attribute to define the font and font size, and use strokeStyle attribute to render the text with another color/gradient.

JavaScript syntax: context.strokeText(text,x,y,maxWidth);

Parameter value

Parameters Description
text Specifies the text to output on the canvas.
x The x-coordinate position where text drawing starts (relative to the canvas).
y The y-coordinate position where text drawing starts (relative to the canvas).
maxWidth Optional. The maximum allowed text width, in pixels.

HTML canvas Reference HTML canvas Reference Manual