4 ms·
I recently had to deal with this issue, except in my case, font-display optional really couldn't provide a decent solution. My use case was, I was building a w
by sudosteph 6y ago
I recently had to deal with this issue, except in my case, font-display optional really couldn't provide a decent solution.
My use case was, I was building a website that uses HTML canvases + text and emojis to let users build custom greeting cards. I wanted users to be able to share their cards with their friends (or save it for later), but I didn't want to deal with signup or saving stuff to a database - so I just let people generate a long link that contains the card content. Since we were only using emojis and text, and since I was ok with just using one type of font, I figured I could shrink that info into a link - even if it's ugly[1]
When I first stared, I just used "Impact" font (I wanted a meme vibe), and it worked great - until I loaded it on my phone. So then I found a similar-ish font on google fonts, and that's when I first started seeing race issues. I spent some time messing with it, added preload, but it still doesn't load consistently. Especially I've noticed that the images that are used for the inside pages of a card (which are pngs generated by setting up a canvas then saving it as a blob) seem to load with a default font in race conditions. It all works again if people click to that image and load the actual canvas, but I doubt some people will do that. So it's very frustrating and I really wish there was a better way - because there really are cases where a specific font is necessary.
I think it's probably still not fixed, but here's an example of the type of load that was causing me problems.
[1] https://emojify.cards/?canvas-0=HEY+357+576+6.417+9.018+0+0+0%7C%F0%9F%A6%A7+738+175+32.088+32.029+0+34+0%7C%F0%9F%95%B6%EF%B8%8F+1015+545+11.844+11.822+0+69+0&canvas-1=%F0%9F%99%8C+711+23+39.803+39.729+0+26+0%7C%F0%9F%92%8E+-4+70+38.131+38.061+0+27+0&canvas-2=+35+27+5.268+5.258+0+36+0%7C+0+0+3.896+3.889+0+57+0%7C%2520+0+0+3.896+3.889+0+58+0%7C%2520+-8+0+3.896+3.889+0+59+0%7C%2520+0+0+3.896+3.889+0+60+0%7C%F0%9F%92%9F+77+35+39.853+39.779+0+65+0%7CI%2520love%2520the%2520way%2520you+486+331+2.918+2.913+0+71+0%7Cthe%2520keys%2520to%2520my%2520heart+902+412+2.178+2.174+359+72+0%7Chold+668+592+4.332+4.324+0+73+0%7C%F0%9F%9A%80+1178+646+20.049+20.012+345+74+0%7C%F0%9F%92%B9+1512+54+15.709+15.680+0+75+0%7C(%2520and%2520also%2520%2524GME%2520)+1061+564+1.348+1.346+0+79+0%7C%F0%9F%9F%A9+1540+89+7.726+7.712+0+79+0%7C%F0%9F%9F%A9+1816+89+5.501+5.491+309+80+0%7C+0+0+3.896+3.889+0+81+0%7C%F0%9F%92%96+645+319+5.883+5.872+0+82+0%7C%F0%9F%92%96+653+974+5.992+5.981+0+83+0%7C%250A+1905+666+1.843+1.839+0+84+0%7C+1925+674+1.788+1.785+0+86+0%7CPS%2520-%2520%2520I%2520like%2520the%2520stock%252C%2520but%2520I%2520love%2520YOU+1905+670+1.340+1.338+0+90+0%7C%F0%9F%98%98+1979+1036+3.090+3.084+0+92+0 https://emojify.cards/?canvas-0=HEY+357+576+6.417+9.018+0+0+...
- jfk13 6y agoI haven't examined your site's code, but it sounds like you should be using the CSS Font Loading API (https://drafts.csswg.org/css-font-loading/ https://drafts.csswg.org/css-font-loading/) to ensure the font(s) you want are loaded and ready, before attempting to draw text in canvas.
- sudosteph 6y agoThank you for the tip. I think I did briefly look at that, but the "experimental" banner on the web api documentation page scared me off. Looking at the browser compatibility chart, it does seem to be pretty widely implemented at least.
- cramforce 6y agoFontFaceObserver linked in the blog post is what you want. Also for general usability reasons. The standard API is quite difficult to use.