Canvas textBaseline 屬性
示例
在 y=100 處繪製一條紅線,然後將每個單詞以不同的 textBaseline 值放置在 y=100 處
JavaScript
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
//在 y=100 處繪製一條紅線
ctx.strokeStyle = "red";
ctx.moveTo(5, 100);
ctx.lineTo(395, 100);
ctx.stroke();
ctx.font = "20px Arial"
//將每個單詞以不同的 textBaseline 值放置在 y=100 處
ctx.textBaseline = "top";
ctx.fillText("Top", 5, 100);
ctx.textBaseline = "bottom";
ctx.fillText("Bottom", 50, 100);
ctx.textBaseline = "middle";
ctx.fillText("Middle", 120, 100);
ctx.textBaseline = "alphabetic";
ctx.fillText("Alphabetic", 190, 100);
ctx.textBaseline = "hanging";
ctx.fillText("Hanging", 290, 100);
自己動手試一試 »
描述
textBaseline
屬性設定或返回繪製文字時使用的文字基線。
預設值為 alphabetic
請參閱下面的插圖,瞭解 textBaseline
支援的基線

另請參閱
font 屬性 (設定文字字型和大小)
fillStyle 屬性 (設定文字顏色/漸變)
textAlign 屬性 (設定文字對齊方式)
fillText() 方法 (繪製文字)
strokeText() 方法 (繪製文字)
注意
fillText() 和 strokeText() 在繪製文字時都將使用 textBaseline 值。
語法
context.textBaseline = "alphabetic|top|hanging|middle|ideographic|bottom" |
屬性值
值 | 描述 | 試一試 |
---|---|---|
alphabetic | 正常的字母基線 | 試一試 » |
top | em 方塊的頂部 | 試一試 » |
hanging | 懸掛基線 | 試一試 » |
middle(居中) | em 方塊的中間 | 試一試 » |
ideographic | 表意文字基線 | 試一試 » |
bottom | 包圍盒的底部 | 試一試 » |
瀏覽器支援
<canvas>
元素是 HTML5 標準(2014)。
textBaseLine
在所有現代瀏覽器中都受支援
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 9-11 |
❮ Canvas 參考