--- title: CanvasRenderingContext2D.textBaseline slug: Web/API/CanvasRenderingContext2D/textBaseline tags: - API - Canvas - CanvasRenderingContext2D - Property - Reference - Référence(2) translation_of: Web/API/CanvasRenderingContext2D/textBaseline ---
Canvas 2D API の CanvasRenderingContext2D.textBaseline プロパティは、テキストを描画するときに用いられる現在のテキストのベースライン (基準線) を指定します。
ctx.textBaseline = "top" || "hanging" || "middle" || "alphabetic" || "ideographic" || "bottom";
指定可能な値:
デフォルト値は alphabetic です。
textBaseline プロパティの使用例さまざまなベースライン設定を行う例です。
<canvas id="canvas" width="550" height="500"></canvas>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'];
ctx.font = '36px serif';
ctx.strokeStyle = 'red';
baselines.forEach(function (baseline, index) {
ctx.textBaseline = baseline;
var y = 75 + index * 75;
ctx.beginPath(); ctx.moveTo(0, y + 0.5); ctx.lineTo(550, y + 0.5); ctx.stroke();
ctx.fillText('Abcdefghijklmnop (' + baseline + ')', 0, y);
});
以下のコードを編集すると、canvas の変更個所をその場で確認できます:
<canvas id="canvas" width="550" height="500" class="playable-canvas"></canvas>
<div class="playable-buttons">
<input id="edit" type="button" value="Edit" />
<input id="reset" type="button" value="Reset" />
</div>
<textarea id="code" class="playable-code">
var baselines = ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'];
ctx.font = '36px serif';
ctx.strokeStyle = 'red';
baselines.forEach(function (baseline, index) {
ctx.textBaseline = baseline;
var y = 75 + index * 75;
ctx.beginPath(); ctx.moveTo(0, y + 0.5); ctx.lineTo(550, y + 0.5); ctx.stroke();
ctx.fillText('Abcdefghijklmnop (' + baseline + ')', 0, y);
});
</textarea>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var textarea = document.getElementById('code');
var reset = document.getElementById('reset');
var edit = document.getElementById('edit');
var code = textarea.value;
function drawCanvas() {
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
eval(textarea.value);
ctx.restore();
}
reset.addEventListener('click', function() {
textarea.value = code;
drawCanvas();
});
edit.addEventListener('click', function() {
textarea.focus();
})
textarea.addEventListener('input', drawCanvas);
window.addEventListener('load', drawCanvas);
{{ EmbedLiveSample('Playable_code', 700, 700) }}
| 仕様書 | 策定状況 | コメント |
|---|---|---|
| {{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-textbaseline", "CanvasRenderingContext2D.textBaseline")}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.CanvasRenderingContext2D.textBaseline")}}