HTML canvas font 屬性

HTML canvas 參考手冊 HTML canvas 參考手冊

實例

在畫布上寫一段 30 像素的文本,使用的字體是 "Arial":

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("Hello World",10,50);


流覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 font 屬性。

注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。


定義和用法

font 屬性設置或返回畫布上文本內容的當前字體屬性。

font 屬性使用的語法與 CSS font 屬性 相同。

默認值: 10px sans-serif
JavaScript 語法: context.font="italic small-caps bold 12px arial";

屬性值

描述
font-style 規定字體樣式。可能的值:
  • normal
  • italic
  • oblique
font-variant 規定字體變體。可能的值:
  • normal
  • small-caps
font-weight 規定字體的粗細。可能的值:
  • normal
  • bold
  • bolder
  • lighter
  • 100
  • 200
  • 300
  • 400
  • 500
  • 600
  • 700
  • 800
  • 900
font-size/line-height 規定字型大小和行高,以像素計。
font-family 規定字體系列。
caption 使用標題控件的字體(比如按鈕、下拉列表等)。
icon 使用用於標記圖示的字體。
menu 使用用於菜單中的字體(下拉列表和菜單列表)。
message-box 使用用於對話框中的字體。
small-caption 使用用於標記小型控件的字體。
status-bar 使用用於窗口狀態欄中的字體。


HTML canvas 參考手冊 HTML canvas 參考手冊