Created
August 9, 2022 12:45
-
-
Save ioxua/ccf59f130479d5ca8516870f6b71809f to your computer and use it in GitHub Desktop.
Canvas with Font Face example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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