HTML canvas textBaseline 屬性
實例
在y=100 處繪製一條紅線,然後在y=100 處用不同的textBaseline 值放置每個單詞:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
//Draw a red line at y=100
ctx.strokeStyle="red";
ctx.moveTo(5,100);
ctx.lineTo(395,100);
ctx.stroke();
ctx.font="20px Arial"
//Place each word at y=100 with different textBaseline values
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);
var ctx=c.getContext("2d");
//Draw a red line at y=100
ctx.strokeStyle="red";
ctx.moveTo(5,100);
ctx.lineTo(395,100);
ctx.stroke();
ctx.font="20px Arial"
//Place each word at y=100 with different textBaseline values
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);
嘗試一下»
瀏覽器支持
Internet Explorer 9、Firefox、Opera、Chrome 和Safari 支持textBaseline 屬性。
注意: textBaseline屬性在不同的瀏覽器上效果不同,特別是使用"hanging"或"ideographic"時。
注意: Internet Explorer 8及之前的版本不支持<canvas>元素。
定義和用法
textBaseline 屬性設置或返回在繪製文本時的當前文本基線。
下面的圖示演示了textBaseline 屬性支持的各種基線:
注意: fillText()和strokeText()方法在畫布上定位文本時,將使用指定的textBaseline值。
默認值: | alphabetic |
---|---|
JavaScript 語法: | context.textBaseline="alphabetic|top|hanging|middle|ideographic|bottom"; |
屬性值
值 | 描述 |
---|---|
alphabetic | 默认。文本基线是普通的字母基线。 |
top | 文本基线是 em 方框的顶端。 |
hanging | 文本基线是悬挂基线。 |
middle | 文本基线是 em 方框的正中。 |
ideographic | 文本基线是表意基线。 |
bottom | 文本基线是 em 方框的底端。 |
HTML canvas參考手冊