Svojstvo textBaseline

Svojstvo textBaseline postavlja vertikalno poravnanje teksta, nacrtanog pomoću metode fillText ili metode strokeText. Prihvata jednu od mogućih vrednosti: top, hanging, middle, alphabetic (podrazumevano), ideographic, bottom (za razumevanje vrednosti pogledajte www.w3schools.com/tags/canvas_textbaseline.asp).

Sintaksa

javascriptkontekst.textBaseline = vrednost;

Primer

Hajde da postavimo tekstu vertikalno poravnanje po gornjoj liniji:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptconst 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);

Результат:

Primer

A sada hajde da postavimo tekstu vertikalno poravnanje po donjoj liniji:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptconst 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); 

Результат:

Pogledajte takođe

  • svojstvo textAlign,
    koje poravnava tekst horizontalno
  • svojstvo font,
    koje postavlja veličinu i tip fonta