Vlastnost HTML canvas textAlign
Příklad
Vytvořte červenou čáru na pozici 150. Pozice 150 je kotevní bod pro veškerý text definovaný v příkladu níže. Prostudujte si účinek každé hodnoty vlastnosti textAlign:
JavaScript:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Create a red line in position 150
ctx.strokeStyle = "red";
ctx.moveTo(150, 20);
ctx.lineTo(150, 170);
ctx.stroke();
ctx.font = "15px Arial";
// Show the different textAlign values
ctx.textAlign = "start";
ctx.fillText("textAlign=start", 150, 60);
ctx.textAlign = "end";
ctx.fillText("textAlign=end", 150, 80);
ctx.textAlign = "left";
ctx.fillText("textAlign=left", 150, 100);
ctx.textAlign = "center";
ctx.fillText("textAlign=center", 150, 120);
ctx.textAlign = "right";
ctx.fillText("textAlign=right", 150, 140);
Podpora prohlížeče
Čísla v tabulce určují první verzi prohlížeče, která plně podporuje vlastnost.
Property | |||||
---|---|---|---|---|---|
textAlign | Yes | 9.0 | Yes | Yes | Yes |
Definice a použití
Vlastnost textAlign nastavuje nebo vrací aktuální zarovnání obsahu textu podle kotevního bodu.
Normálně text ZAČÍNÁ na zadané pozici, pokud však nastavíte textAlign="right" a umístíte text na pozici 150, znamená to, že text by měl KONČÍT na pozici 150.
Tip: Pomocí metody fillText() nebo strokeText() skutečně nakreslete a umístěte text na plátno.
Výchozí hodnota: | Start |
---|---|
Syntaxe JavaScriptu: | context .textAlign="center|end|left|right|start"; |
Hodnoty vlastností
Values | Description | Play it |
---|---|---|
start | Default. The text starts at the specified position | |
end | The text ends at the specified position | |
center | The center of the text is placed at the specified position | |
left | The text starts at the specified position | |
right | The text ends at the specified position |
❮ HTML Canvas Reference