Latest web development tutorials

HTML canvas fillText () method

Canvas Object Reference Canvas object

Examples

Use fillText (), write on the canvas text "Hello world!" And "Big smile!":

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");

ctx.font="20px Georgia";
ctx.fillText("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.fillStyle=gradient;
ctx.fillText("Big smile!",10,90);

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support fillText () 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

fillText () method to draw text fill on the canvas. The default text color is black.

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

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

Parameter Value

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


Canvas Object Reference Canvas object