Propiedad textBaseline

La propiedad textBaseline establece la alineación vertical del texto dibujado con el método fillText o con el método strokeText. Acepta uno de los siguientes valores: top, hanging, middle, alphabetic (por defecto), ideographic, bottom (para entender los valores consulte www.w3schools.com/tags/canvas_textbaseline.asp).

Sintaxis

javascriptcontexto.textBaseline = valor;

Ejemplo

Establezcamos la alineación vertical del texto en la línea superior:

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

Результат:

Ejemplo

Ahora establezcamos la alineación vertical del texto en la línea inferior:

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

Результат:

Véase también

  • propiedad textAlign,
    que alinea el texto horizontalmente
  • propiedad font,
    que establece el tamaño y tipo de fuente