queryLocalFonts
One of the larger downloads when requesting a webpage are custom fonts. There are many great techniques for lazy loading fonts to improve performance for those on poor connections. By getting insight into what fonts the user has available, we can avoid loading custom fonts. That’s where queryLocalFonts
comes in — an native JavaScript function to gather user font information.
queryLocalFonts
is an async
function that requires user permission via a browser prompt when first executed. queryLocalFonts
returns an array of FontData
objects which contain information about all available fonts:
const localFonts = await window.queryLocalFonts(); // [FontData, FontData, ...] /* { family: "Academy Engraved LET", fullName: "Academy Engraved LET Plain:1.0", postscriptName: "AcademyEngravedLetPlain", style: "Plain", } */
If you’d like to target a specific font face, you can also directly query the postscriptName
property:
const canelaFonts = await window.queryLocalFonts({ postscriptNames: ["Canela", "Canela-Bold"], }); // [FontData, FontData, ...]
With queryLocalFonts
you can leverage a fonts a user already has instead of downloading expensive custom fonts. The prompt for permissions seems like it would deter users from allowing the API, however. It’s so cool that this API exists though!
5 More HTML5 APIs You Didn’t Know Existed
The HTML5 revolution has provided us some awesome JavaScript and HTML APIs. Some are APIs we knew we’ve needed for years, others are cutting edge mobile and desktop helpers. Regardless of API strength or purpose, anything to help us better do our job is a…
Create Namespaced Classes with MooTools
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event…
Prevent Page Zooming in Mobile Browsers
Ever since I got my iPhone, I’ve been more agreeable in going places that my fiancee wants to go. It’s not because I have any interest in checking out women’s shoes, looking at flowers, or that type of stuff — it’s because my iPhone lets…
MooTools Fun with Fx.Shake
Adding movement to your website is a great way to attract attention to specific elements that you want users to notice. Of course you could use Flash or an animated GIF to achieve the movement effect but graphics can be difficult to maintain. Enter…