Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas font attribute

HTML canvas font attribute

HTML canvas Reference HTML canvas Reference Manual

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 »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

**Note:** Internet Explorer 8 and earlier versions do not support .


Definition and Usage

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

The font attribute uses the same syntax as the CSS font attribute .

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

Attribute values

Value Description
font-style Specifies the font style. Possible values:
  • normal
  • italic
  • oblique
  • | | *font-variant* | specifies the font variant. Possible values: | | *font-weight* | specifies the font weight. Possible values: | | *font-size/line-height* | specifies the font size and line height, in pixels. | | *font-family* | specifies the font family. | | caption | uses the font for captioned controls (e.g. buttons, drop-down lists, etc.). | | icon | uses the font used for labeling icons. | | menu | uses the font used in menus (drop-down lists and menu lists). | | message-box | uses the font used in dialog boxes. | | small-caption | uses the font for labeling small controls. | | status-bar | uses the font used in the window status bar. |

    HTML canvas Reference HTML canvas Reference Manual