惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
F
Fortinet All Blogs
B
Blog
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
The Register - Security
The Register - Security
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
B
Blog RSS Feed
WordPress大学
WordPress大学
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
O
OpenAI News
月光博客
月光博客
H
Hacker News: Front Page
S
Security Affairs
W
WeLiveSecurity
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
A
About on SuperTechFans

博客园 - 沧海一声笑

阿里云服务器被挖矿minerd入侵的解决办法 Visor 隐藏应用之一 CSS3 生成器 Visro 应用的前端模板工具介绍 -JsRender visor 发布 在一个老外微信PM的眼中,中国移动App UI那些事儿 7个步骤:让JavaScript变得更好 Websocket 协议解析 HTML5全局属性和事件 HTML5适合移动应用开发的几大特性 JQuery 应用-JQuery.groupTable.js(二) JQuery 应用-JQuery.groupTable.js - 沧海一声笑 - 博客园 DataGridView控件用法合集 .Net 字符串编码和转换 浅析ASP.NET 3.5与ASP.NET 4.0主要差别 单点登录的实现思路(转) jquery 实现iframe 自适应高度 中国惠普前总裁孙振耀的毕生经验之谈 jquery之旅-jquery的Ajax SQL Server 2008 对 T-SQL 语言的增强
如何在HTML5 Canvas 里面显示 Font Awesome 图标
沧海一声笑 · 2015-04-21 · via 博客园 - 沧海一声笑

  Font Awesome 是一套完美的图标字体,主要目的是和 Bootstrap 搭配使用. 提供的CSS 已经可以完美显示这些图标在网页里面。最新的版本4.3 里面,已经提供519 Icons。 这些图标广泛应用在web应用里面。如何在HTML5 Canvas 里去显示这些图标呢?经过一通学习,终于找到好的方案。 Font Awesome 是一套图标字库,提供了几种矢量字体

TTF或.OTF,适用于Firefox 、Safari、Opera

.EOT,适用于Internet Explorer 4.0+

.SVG,适用于Chrome、IPhone

.WOFF 知用于Chrome、Firefox

但它的内容是用Unicode 做编码的。在当字体显示的时候,我们要先获得图标的Unicode,基本思路是

1. 将Font Awesome 的字体文件拷贝到服务器的字体目录里面

2. 设置Canvas 的字体, ctx.font="12px fontawesome"

3. 调用Canvas API  ctx.fillText(text,x,y). 这里text 就是 图标的Unicode。

结果显示的就是Unicode代表的图标了.

其他相关的技巧

1. 如果没有权限或者不想拷贝字体到服务器的字体目录里怎么办?

  可以采用动态调用指定文件字体的方法。在程序里动态加载指定目录下的字体文件。

  如果不知怎么做?可以使用Font.js,项目在https://github.com/Pomax/Font.js

2. 如何正确把图标显示在指定的方框里?

具体看代码

            ctx.save();
            var icon_font=this.icon.size+"px "+font.fontFamily;
            ctx.font=icon_font;
            ctx.strokeStyle=this.icon.color||"black";
            ctx.fillStyle=this.icon.color||"black";
            ctx.textBaseline="top";
            metrics=ctx.measureText(this.icon.text);    
            if(this.icon.fill)
                ctx.fillText(this.icon.text,(this.width-metrics.width)/2,this.margin);
            else
                ctx.strokeText(this.icon.text,(this.width-metrics.width)/2,this.margin);
            ctx.restore();     

关键在hightlight 的代码上,这样就可以保证字体正确显示在方框的中间,并保留margin 大小的距离。

由于Font awesome 的Unicode太多,太难记,我把它生成到Json 数据文件里。

需要的可以去下载, http://www.visor.com.cn/help/fontawesome4_3

我在使用的地方都是应用这些数据去做选择的。