Egenskapen textBaseline
Egenskapen textBaseline ställer in den vertikala
justeringen av text som ritas med
metoden fillText
eller metoden strokeText.
Tar ett av de möjliga värdena: top,
hanging, middle, alphabetic (som standard),
ideographic, bottom (för att förstå värdena
se www.w3schools.com/tags/canvas_textbaseline.asp).
Syntax
kontext.textBaseline = värde;
Exempel
Låt oss ställa in textens vertikala justering till den övre linjen:
<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);
:
Exempel
Och låt oss nu ställa in textens vertikala justering till den nedre linjen:
<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);
: