HTML canvas strokeText()Method

Canvas 对象参考手册Canvas Object

Example

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

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 yourself »

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 the <canvas> element.

Note:Safari does not support the maxWidth parameter.


Definition and Usage

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

Tip:Please usefontthe property to define the font and font size, and usestrokeStylethe property to render the text in another color/gradient.

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

Parameter Values

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


Canvas 对象参考手册Canvas Object

Other Extensions