So, I'm developing a simple web game with the Canvas API. I need to draw characters in the canvas using specified fonts.
In the loading routine, I use promises to wait for the fonts I need, like this:
Promise.all([
(new FontFace("LCD Solid", "url('assets/LCD_Solid.ttf')")).load().then((font)=>{
document.fonts.add(font)
}, (err)=>{
throw {type: "font_loading_err", fontName: "LCD Solid", DOMException: err}
}),
(new FontFace("Conformity", "url('assets/Conformity.ttf')")).load().then((font)=>{
document.fonts.add(font)
}, (err)=>{
throw {type: "font_loading_err", fontName: "Conformity", DOMException: err}
}),
]).then(loadGame, oops)
The promises are resolved, but not loaded. Chrome and Firefox only load them when I use fillText()
, originating some frames with the default serif font, until the font loads, in Chrome.
According to the spec the promises should only be resolved when the fonts are loaded, but it also allows for lazy loading.
Is there any way to avoid lazy-loading the fonts and force the browser to load them at that moment?