HTML canvas fontProperty

Canvas 对象参考手册Canvas Object

Example

Write a 30-pixel 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 Yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

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


Definition and Usage

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

The syntax used by the font property is the same as theCSS font propertysame.

Default value: 10px sans-serif
JavaScript syntax: context.font="italic small-caps bold 12px arial";

Property Values

Value Description
font-style Specifies the font style. Possible values:
  • normal
  • italic
  • oblique
font-variant Specifies the font variant. Possible values:
  • normal
  • small-caps
font-weight Specifies the weight of the font. Possible values:
  • normal
  • bold
  • bolder
  • lighter
  • 100
  • 200
  • 300
  • 400
  • 500
  • 600
  • 700
  • 800
  • 900
font-size/line-height Specifies the font size and line height, in pixels.
font-family Specifies the font family.
caption Uses the font for caption controls (such as buttons, drop-down lists, etc.).
icon Uses the font used to label icons.
menu Uses the font for menus (drop-down lists and menu lists).
message-box Uses the font for dialog boxes.
small-caption Uses the font for labeling small controls.
status-bar Uses the font for the window status bar.


Canvas 对象参考手册Canvas Object

Other Extensions