Latest web development tutorials

HTML canvas font attributes

HTML canvas Reference Manual HTML canvas Reference Manual

Examples

30 pixels to write a text on the canvas, using the font "Arial":

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

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

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support font attributes.

Note: 8 and earlier versions of Internet Explorer do not support the <canvas> element.


Definition and Usage

font property sets or returns the current font properties of the text on the canvas content.

Grammar and font attributes used CSS font properties the same.

Defaults: 10px sans-serif
JavaScript syntax: context .font = "italic small-capsbold 12px arial";

Property Value

描述
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 Reference Manual HTML canvas Reference Manual