Proprietatea textBaseline
Proprietatea textBaseline stabilește alinierea verticală
a textului desenat cu ajutorul
metodei fillText
sau a metodei strokeText.
Acceptă una dintre valorile posibile: top,
hanging, middle, alphabetic (implicit),
ideographic, bottom (pentru înțelegerea valorilor
vezi www.w3schools.com/tags/canvas_textbaseline.asp).
Sintaxă
context.textBaseline = valoare;
Exemplu
Să stabilim textului alinierea verticală pe linia superioară:
<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);
:
Exemplu
Iar acum să stabilim textului alinierea verticală pe linia inferioară:
<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);
: