Latest web development tutorials

HTML canvas strokeText () method

Canvas Object Reference Canvas object

Examples

Use strokeText (), write on the canvas text "Hello world!" And "Big smile!" (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 strokeText () method.

Note: 8 and earlier versions of Internet Explorer do not support the <canvas> element.

Note: Safari does not support maxWidth parameters.


Definition and Usage

strokeText () method to draw text on a canvas (no fill color). The default text color is black.

Tip: Use font attributes to define the font and font size, and use strokeStyle property to another color / gradient to render text.

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

Parameter Value

参数 描述
text 规定在画布上输出的文本。
x 开始绘制文本的 x 坐标位置(相对于画布)。
y 开始绘制文本的 y 坐标位置(相对于画布)。
maxWidth 可选。允许的最大文本宽度,以像素计。


Canvas Object Reference Canvas object