CanvasRenderingContext2D.font
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
CanvasRenderingContext2D.font
– свойство Canvas 2D API, определяющее текущие стили рисуемого текста. Это строка, которая использует синтаксис CSS font.
Синтаксис
ctx.font = value;
Опции
Примеры
Использование пользовательского шрифта
Этот пример задаёт в свойстве font
другие font-size
и font-family
.
HTML
html
<canvas id="canvas"></canvas>
JavaScript
js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.font = "bold 48px serif";
ctx.strokeText("Hello world", 50, 100);
Результат
Загрузка шрифта с помощью CSS Font Loading API
С помощью FontFace
API, вы можете явно загрузить шрифт перед использованием его в canvas
.
js
let f = new FontFace("test", "url(x)");
f.load().then(function () {
// Ready to use the font in a canvas context
});
Спецификации
Specification |
---|
HTML # dom-context-2d-font-dev |
Совместимость с браузерами
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
font |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Смотрите также
- Интерфейс, определяющий это свойство:
CanvasRenderingContext2D