textBaselineプロパティ
textBaselineプロパティは、
メソッド fillText
またはメソッド strokeText
を使用して描画されたテキストの垂直方向の配置を設定します。
取り得る値のいずれか一つを受け入れます: top,
hanging, middle, alphabetic (デフォルト),
ideographic, bottom (値の理解については
www.w3schools.com/tags/canvas_textbaseline.aspを参照)。
構文
コンテキスト.textBaseline = 値;
例
テキストの垂直方向の配置を 上端基準に設定してみましょう:
<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);
:
例
次に、テキストの垂直方向の配置を 下端基準に設定してみましょう:
<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);
: