HTML canvas fillText() methode

Definitie en gebruik

fillText() De methode tekent geschilderde tekst op het canvas. De standaardkleur van de tekst is zwart.

Tip:Gebruik font Eigenschap om lettertype en grootte te definiëren, en gebruik fillStyle Eigenschappen renderen tekst met een andere kleur/verloop.

Voorbeeld

Gebruik fillText() om tekst "Hello world!" en "codew3c.com" op het canvas te schrijven:

Uw browser ondersteunt niet de 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");
// Fill color with gradient
ctx.fillStyle=gradient;
ctx.fillText("codew3c.com",10,90);

Try it yourself

Syntax

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

Parameter value

Parameter Description
text Specifies 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 allowed, in pixels.

Browser support

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

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.