Skip to content

loadFont

Loads a font and registers it in document.fonts, so CSS and canvas can use family right away.

source is either a URL (wrapped in url() for you) or the font file’s bytes. Outside a browser (no FontFace), it rejects with a TypeError.

Available since v3.1.5

import { loadFont } from '@helpers4/browser';
// or, from the all-in-one package (same code, one install):
import { loadFont } from 'helpers4/browser';
loadFont(family: string, source: string | BufferSource, descriptors?: FontFaceDescriptors): Promise<FontFace>
ParameterTypeDescription
familystringThe family name CSS will use, e.g. `‘Inter’`
sourcestring | BufferSourceThe font URL, or its bytes
descriptorsFontFaceDescriptors`weight`, `style`, `unicodeRange`, … as in a CSS `@font-face` rule (optional)

Promise<FontFace> — The loaded FontFace

The font is registered in document.fonts, so CSS and canvas can use it right away.

await loadFont('Inter', '/fonts/Inter-Bold.woff2', { weight: '700' });
context.font = '700 16px Inter';

Pass the bytes of a font file extracted from an archive or a design file.

await loadFont('Embedded', await file.arrayBuffer());

View source on GitHub