Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active December 28, 2015 07:09
Show Gist options
  • Select an option

  • Save WebReflection/7461879 to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/7461879 to your computer and use it in GitHub Desktop.
well, a weird trick to know if a font is usable inside canvas too
/*! (C) Andrea Giammarchi */
// @link https://gist.github.com/WebReflection/7461879
// @example
// isFontLoaded('FontAwesome', '\uf06b')
var isFontLoaded = function(canvas1, canvas2) {
function reset(canvas, fontName) {
var context = canvas.getContext('2d');
canvas.width = canvas.height = 16;
context.clearRect(0, 0, 16, 16);
context.fillStyle = "rgb(0,0,0)";
context.font = "16px '" + fontName + "'";
context.fillText('\uf06b', 0, 16);
return canvas.toDataURL();
}
return function (fontName, specialChar) {
return reset(canvas1, 'default', specialChar) !==
reset(canvas2, fontName, specialChar);
};
}(
document.createElement('canvas'),
document.createElement('canvas')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment