Właściwość textBaseline
Właściwość textBaseline ustawia pionowe
wyrównanie tekstu narysowanego za pomocą
metody fillText
lub metody strokeText.
Przyjmuje jedną z możliwych wartości: top,
hanging, middle, alphabetic (domyślnie),
ideographic, bottom (dla zrozumienia wartości
zobacz www.w3schools.com/tags/canvas_textbaseline.asp).
Składnia
kontekst.textBaseline = wartość;
Przykład
Ustawmy tekstowi pionowe wyrównanie do górnej linii:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'red';
ctx.moveTo(0, 100);
ctx.lineTo(400, 100);
ctx.stroke();
ctx.font="14px Arial";
ctx.textBaseline = 'top';
ctx.fillText('text', 80, 100);
:
Przykład
A teraz ustawmy tekstowi pionowe wyrównanie do dolnej linii:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'red';
ctx.moveTo(0, 100);
ctx.lineTo(400, 100);
ctx.stroke();
ctx.font="14px Arial";
ctx.textBaseline="bottom";
ctx.fillText('text', 80, 100);
: