CanvasRenderingContext2D.font
The CanvasRenderingContext2D
.font
property of the Canvas 2D API specifies the current text style being used when drawing text. This string uses the same syntax as the CSS font specifier. The default font is 10px sans-serif.
Syntax
JavaScript
Copy Code
<var>ctx.font = value;</var>
Options
Examples
Using the font
property
This is just a simple code snippet using the font
property to set a different font size and font family.
HTML
HTML
Copy Code
<canvas id="canvas"></canvas>
JavaScript
JavaScript
Copy Code
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = "48px serif"; ctx.strokeText("Hello world", 50, 100);
Edit the code below and see your changes update live in the canvas:
Playable code
HTML
Copy Code
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code"> ctx.font = "48px serif"; ctx.strokeText("Hello world", 50, 100);</textarea>
JavaScript
Copy Code
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var textarea = document.getElementById("code"); var reset = document.getElementById("reset"); var edit = document.getElementById("edit"); var code = textarea.value; function drawCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); eval(textarea.value); } reset.addEventListener("click", function() { textarea.value = code; drawCanvas(); }); edit.addEventListener("click", function() { textarea.focus(); }) textarea.addEventListener("input", drawCanvas); window.addEventListener("load", drawCanvas);
Loading fonts with the CSS Font Loading API
With the help of the FontFace
API, you can explicitly load fonts before using it in canvas.
JavaScript
Copy Code
var f = new FontFace("test", "url(x)"); f.load().then(function() { // Ready to use the font in a canvas context });
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.font' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 3.5 (1.9.1) | 9 | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 1.0 (1.9.1) | (Yes) | (Yes) | (Yes) |
Gecko-specific notes
- In Gecko-based browsers, such as Firefox, a non-standard and deprecated property
ctx.mozTextStyle
is implemented besides this property. Don't use it.
See also
- The interface defining it,
CanvasRenderingContext2D
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/canvasrenderingcontext2d/font