HTML-Canvas -fillText()- Methode
Beispiel
Schreiben Sie "Hallo Welt!" und "Großes Lächeln!" (mit Farbverlauf) auf der Leinwand mit fillText():
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);
Browser-Unterstützung
Die Zahlen in der Tabelle geben die erste Browserversion an, die das Verfahren vollständig unterstützt.
Method | |||||
---|---|---|---|---|---|
fillText() | Yes | 9.0 | Yes | Yes | Yes |
Hinweis: Der maxWidth-Parameter wird in Safari 5.1 und früheren Versionen nicht unterstützt.
Definition und Verwendung
Die Methode fillText() zeichnet gefüllten Text auf der Leinwand. Die Standardfarbe des Textes ist schwarz.
Tipp: Verwenden Sie die Eigenschaft font , um Schriftart und Schriftgröße anzugeben, und verwenden Sie die Eigenschaft fillStyle , um den Text in einer anderen Farbe/einem anderen Farbverlauf wiederzugeben.
JavaScript-Syntax: | Kontext .fillText( Text,x,y,maxWidth ); |
---|
Parameterwerte
Parameter | Description | Play it |
---|---|---|
text | Specifies the text that will be written on the canvas | |
x | The x coordinate where to start painting the text (relative to the canvas) | |
y | The y coordinate where to start painting the text (relative to the canvas) | |
maxWidth | Optional. The maximum allowed width of the text, in pixels |
❮ HTML-Canvas-Referenz