HTML canvas strokeText()Method

HTML canvas 参考手册HTML canvas Reference

Example

Use strokeText() to write the text "Hello world!" and "Big smile!" on the canvas (with 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 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 use thefontproperty to define the font and font size, and use thestrokeStyleproperty 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 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 参考手册HTML canvas Reference Other Extensions