Skip to content

Instantly share code, notes, and snippets.

@takanorip
Last active December 3, 2018 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takanorip/2e895488820caff942fc83a555e9acc3 to your computer and use it in GitHub Desktop.
Save takanorip/2e895488820caff942fc83a555e9acc3 to your computer and use it in GitHub Desktop.
const font = new FontFace("hack", "url(https://cdnjs.cloudflare.com/ajax/libs/hack-font/3.003/web/fonts/hack-regular.woff2)", {
style: 'normal',
weight:'400'
});
// DOMツリーのレンダリングを待たずにフォントをダウンロードする
font.load();
// font.load() はPromiseを返す
font.load().then(function() {
// フォントがロードされたらフォントを適用する
document.fonts.add(font);
document.body.style.fontFamily = "hack, sans-serif";
});
// フォントのダウンロード状況を監視
// font.statusは状況に応じて
// "unloaded", "loading", "loaded", "error"
// のうちのどれかを返す
const fontLoadingStatus = font.status
// document.fonts.check()は、そのフォントが読み込まれているかどうかを
// Booleanで返す
const isFontLoaded = document.fonts.check("12px hack");
console.log(fontLoadingStatus);
console.log(isFontLoaded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment