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

HTML canvas fillText() method

HTML canvas fillText() method

Canvas Object Reference Canvas object

Example

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

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 the fillText() method.

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

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


Definition and Usage

The fillText() method draws filled text on the canvas. The default text color is black.

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

JavaScript syntax: context.fillText(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.

Canvas Object Reference Canvas object