englercj / resource-loader

A middleware-style generic resource loader built with web games in mind.
http://englercj.github.io/resource-loader/
MIT License
424 stars 77 forks source link

Feature request: Font loading via CSS Font Loading API #146

Open qtiki opened 4 years ago

qtiki commented 4 years ago

It would be nice if the resource-loader supported dynamic font loading "out of the box". Currently loading dynamic fonts requires the use of an external package like https://github.com/typekit/webfontloader.

Loading fonts could be implemented with the new CSS Font Loading API: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Font_Loading_API

I know the technology is still experimental but the compatibility chart is already pretty good and hopefully will get better with time (Edge at least should be pretty 1:1 with Chrome now that they're switching to Webkit).

englercj commented 4 years ago

I'm open to giving this a shot again now that browser tech has moved forward a bit.

In the past I've avoided it because of just how complex font loading is. So complex that it would more than double the size of the library just to handle font edge cases.

I wonder if it makes sense to have a separate plugin that adds font support to resource loader, rather than building it in the lib here given how much code seems to be required.

All that being said it has been a while since I looked into this problem space and the state of browsers so I'll at least do some investigation and see where we are. Or if your up to it, PRs are always welcome!

edit: one other complexity is that we support back to ie9 currently, so adding font support would need to support back to that browser as well. Even if we use a modern api we still have to write the fallbacks.

creativefctr commented 4 years ago

I think all you need is a Strategy around FontFaceObserver: https://github.com/bramstein/fontfaceobserver It supports old browsers and also is very lightweight

englercj commented 4 years ago

PRs welcome.

irongaze commented 4 years ago

+1 to this concept - the Venn diagram of resource-loader users who also need to load fonts is likely a circle. :-) Happy to help test if someone with a better understanding of the internals is willing to take a shot.