Canvas fillText() Method

Definition and Usage

fillText() Method to draw filled text on the canvas. The default text color is black.

Tip:Please use font Property to define font and size, and use fillStyle Properties to render text in another color/gradient.

Example

Use fillText() to write the text "Hello world!" and "codew3c.com" on the canvas:

Your browser does not support the HTML5 canvas tag.

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");
// Gradient fill color
ctx.fillStyle=gradient;
ctx.fillText("codew3c.com",10,90);

Try It Yourself

Syntax

context.fillText(text,x,y,maxWidth);

Parameter Value

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

Browser Support

The numbers in the table indicate the first browser version that fully supports this attribute.

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

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