The CanvasRenderingContext2D
.textBaseline
property of the Canvas 2D API specifies the current text baseline used when drawing text.
ctx.textBaseline = "top" || "hanging" || "middle" || "alphabetic" || "ideographic" || "bottom";
Possible values:
"top"
"hanging"
"middle"
"alphabetic"
"ideographic"
"bottom"
The default value is "alphabetic"
.
This example demonstrates the various textBaseline
property values.
<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); });
Edit the code below to see your changes update live in the canvas:
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.textBaseline' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | 12 | 3.5 | 9 | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
CanvasRenderingContext2D
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline