offline version v3


262 of 264 menu

textBaseline property

The textBaseline property specifies the vertical alignment of text drawn using the fillText or the strokeText methods. Takes one of the possible values: top, hanging, middle, alphabetic (default), ideographic, bottom (see www.w3schools.com/tags/canvas_textbaseline for meanings).asp).

Syntax

context.textBaseline = value;

Example

Let's set the text vertical alignment along the top line:

<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);

:

Example

Now let's set the text vertical alignment along the bottom line:

<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);

:

See also

  • the textAlign property
    that aligns text horizontally
  • the font property
    that sets a font size and type
enru