Skip to content

Instantly share code, notes, and snippets.

@ioxua
Created August 9, 2022 12:45
Show Gist options
  • Save ioxua/ccf59f130479d5ca8516870f6b71809f to your computer and use it in GitHub Desktop.
Save ioxua/ccf59f130479d5ca8516870f6b71809f to your computer and use it in GitHub Desktop.
Canvas with Font Face example
const fontUrl = 'https://fonts.gstatic.com/s/robotomono/v22/L0xuDF4xlVMF-BfR8bXMIhJHg45mwgGEFl0_Of2_ROW-AJi8SJQt.woff'
const robotoFont = new FontFace('Roboto Mono', `url(${fontUrl})`);
// Canvas variables
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
robotoFont.load().then((font: FontFace) => {
// This is REQUIRED
document.fonts.add(font);
// Using the font
ctx.font = `30px '${font.family}'`;
ctx.fillText("Hello World!", 10, 50);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment