Metoda HTML canvas fillText().

❮ HTML Canvas Reference

Příklad

Napište "Ahoj světe!" a "Velký úsměv!" (s přechodem) na plátně pomocí fillText():

Váš prohlížeč nepodporuje HTML5canvatag.

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);

Podpora prohlížeče

Čísla v tabulce určují první verzi prohlížeče, která tuto metodu plně podporuje.

Method
fillText() Yes 9.0 Yes Yes Yes

Poznámka: Parametr maxWidth není podporován v Safari 5.1 a starších verzích.


Definice a použití

Metoda fillText() vykreslí vyplněný text na plátno. Výchozí barva textu je černá.

Tip: Použijte vlastnost font k určení písma a velikosti písma a použijte vlastnost fillStyle k vykreslení textu v jiné barvě/přechodu.

Syntaxe JavaScriptu: kontext .fillText( text,x,y,maxWidth );

Hodnoty parametrů

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 Reference