HTML-Leinwand fillText () -Methode
Beispiele
Verwenden Sie fillText (), schreiben auf der Leinwand Text "Hallo Welt!" Und "Big smile!":
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);
Versuchen »
Browser-Unterstützung
Internet Explorer 9, Firefox, Opera, Chrome und Safari Unterstützung fillText () -Methode.
Hinweis: 8 und frühere Versionen von Internet Explorer unterstützt nicht das <canvas> -Element.
Hinweis: Safari unterstützt keine maxWidth Parameter.
Definition und Verwendung
fillText () -Methode zu ziehen Text auf der Leinwand zu füllen. Die Standard-Textfarbe ist schwarz.
Tipp: Verwenden Sie Schriftattribute die Schriftart und Schriftgröße zu definieren und verwenden fillStyle Eigenschaft in eine andere Farbe / Gradient Text zu machen.
JavaScript-Syntax: | Kontext .fillText (text, x, y, maxWidth); |
---|
Parameter Wert
参数 | 描述 |
---|---|
text | 规定在画布上输出的文本。 |
x | 开始绘制文本的 x 坐标位置(相对于画布)。 |
y | 开始绘制文本的 y 坐标位置(相对于画布)。 |
maxWidth | 可选。允许的最大文本宽度,以像素计。 |