You can use a custom async font loader method like this:
let fonts = [
{
"font-family": "Special Elite",
"font-style": "normal",
"font-weight": 400,
src:
"https://fonts.gstatic.com/s/specialelite/v18/XLYgIZbkc4JPUL5CVArUVL0ntnAOSA.woff2"
}
];
drawToCanvas();
async function drawToCanvas() {
var canvas = document.getElementById("my_canvas");
var ctx = canvas.getContext("2d");
//load fonts
await loadFonts(fonts);
ctx.canvas.width = 150;
ctx.canvas.height = 150;
ctx.fillStyle = "red";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = "40px Special Elite";
ctx.fillStyle = "white";
ctx.fillText("hello", 25, 50);
}
async function loadFonts(fontsToLoad) {
if (fontsToLoad.length) {
for (let i = 0; i < fontsToLoad.length; i++) {
let fontProps = fontsToLoad[i];
let fontFamily = fontProps["font-family"];
let fontWeight = fontProps["font-weight"];
let fontStyle = fontProps["font-style"];
let fontUrl = Array.isArray(fontProps["src"])
? fontProps["src"][0][0]
: fontProps["src"];
if (fontUrl.indexOf("url(") === -1) {
fontUrl = "url(" + fontUrl + ")";
}
let fontFormat = fontProps["src"][0][1] ? fontProps["src"][1] : "";
const font = new FontFace(fontFamily, fontUrl);
font.weight = fontWeight;
font.style = fontStyle;
await font.load();
document.fonts.add(font);
console.log(fontFamily, "loaded");
// apply font styles to body
let fontDOMEl = document.createElement("div");
fontDOMEl.textContent = "";
document.body.appendChild(fontDOMEl);
fontDOMEl.setAttribute(
"style",
`position:fixed; height:0; width:0; overflow:hidden; font-family:${fontFamily}; font-weight:${fontWeight}; font-style:${fontStyle}`
);
}
}
}
<canvas id="my_canvas"></canvas>
Open the google font css URL to get the actual font file
https://fonts.googleapis.com/css2?family=Special+Elite
will return something like this (depending on your browser)
/* latin */
@font-face {
font-family: 'Special Elite';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/specialelite/v18/XLYgIZbkc4JPUL5CVArUVL0ntnAOSA.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
Copy the src to the fonts object array (you can add multiple fonts):
let fonts = [
{
"font-family": "Special Elite",
"font-style": "normal",
"font-weight": 400,
src:
"https://fonts.gstatic.com/s/specialelite/v18/XLYgIZbkc4JPUL5CVArUVL0ntnAOSA.woff2"
}
];
Make you drawing function asynchronous
async function drawToCanvas() {
var canvas = document.getElementById("my_canvas");
var ctx = canvas.getContext("2d");
// wait for fonts
await loadFonts(fonts);
ctx.canvas.width = 150;
ctx.canvas.height = 150;
ctx.fillStyle = "red";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = "40px Special Elite";
ctx.fillStyle = "white";
ctx.fillText("hello", 25, 50);
}