Skip to content

Instantly share code, notes, and snippets.

@m0o0scar
Created February 27, 2018 10:18
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 m0o0scar/3dd0048c4c0f6bd570a68eace678e5ac to your computer and use it in GitHub Desktop.
Save m0o0scar/3dd0048c4c0f6bd570a68eace678e5ac to your computer and use it in GitHub Desktop.
用JS检测用户是否安装某font-family字体
//原文及原理请见:http://www.zhangxinxu.com/wordpress/2018/02/js-detect-suppot-font-family/
var isSupportFontFamily=function(f){if(typeof f!="string"){return false}var h="Arial";if(f.toLowerCase()==h.toLowerCase()){return true}var e="a";var d=100;var a=100,i=100;var c=document.createElement("canvas");var b=c.getContext("2d");c.width=a;c.height=i;b.textAlign="center";b.fillStyle="black";b.textBaseline="middle";var g=function(j){b.clearRect(0,0,a,i);b.font=d+"px "+j+", "+h;b.fillText(e,a/2,i/2);var k=b.getImageData(0,0,a,i).data;return[].slice.call(k).filter(function(l){return l!=0})};return g(h).join("")!==g(f).join("")};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment