Egenskaben textBaseline
Egenskaben textBaseline indstiller den lodrette
justering af tekst tegnet med
metoden fillText
eller metoden strokeText.
Accepterer en af de mulige værdier: top,
hanging, middle, alphabetic (som standard),
ideographic, bottom (for at forstå værdierne
se www.w3schools.com/tags/canvas_textbaseline.asp).
Syntaks
kontekst.textBaseline = værdi;
Eksempel
Lad os indstille tekstens lodrette justering til den øverste linje:
<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);
:
Eksempel
Og lad os nu indstille tekstens lodrette justering til den nederste linje:
<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);
: