Last active
March 12, 2025 13:03
-
-
Save av01d/8b5faf495964f2ac3a1c7ae7385b8443 to your computer and use it in GitHub Desktop.
Javascript: Load fonts concurrently, via `new FontFace`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.addEventListener('DOMContentLoaded', async () => { | |
const fonts = ['Action Man', 'Heaven', 'Weltron Urban'] | |
// First method (if you don't need to know when all fonts are finished loading) | |
fonts.forEach(font => { | |
new FontFace(font, `url(fonts/${encodeURIComponent(font)}.woff)`).load().then(font => { document.fonts.add(font) }) | |
}) | |
// Second method (if you do need to know when all fonts are finished loading) | |
const promises = [] | |
fonts.map(font => { | |
promises.push(new FontFace(font, `url(fonts/${encodeURIComponent(font)}.woff)`).load().then(font => { document.fonts.add(font) })) | |
}) | |
await Promise.all(promises) | |
console.log('All fonts loaded') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment