HTML canvas fillText()Method
Examples
Use fillText() to write the text "Hello world!" and "Big smile!" on the canvas:
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);
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 yourself »
Browser Support
![]()
![]()
![]()
![]()
![]()
Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the fillText() 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 fillText() method draws filled text on the canvas. The default color of the text is black.
Tip:Please use thefontproperty to define the font and font size, and use thefillStyleproperty to render the text in another color/gradient.
| JavaScript Syntax: | context.fillText(text,x,y,maxWidth); |
|---|
Parameter Values
| Parameter | Description |
|---|---|
| text | Specifies the text to be output on the canvas. |
| x | The x-coordinate position (relative to the canvas) where the text drawing starts. |
| y | The y-coordinate position (relative to the canvas) where the text drawing starts. |
| maxWidth | Optional. The maximum allowed text width, in pixels. |
Other Extensions
Canvas Object