HTML5 Canvas 上的外部字体
我完全困惑于如何更改在画布上绘制文本时使用的字体。以下是我在 CSS 中定义的字体:
@font-face{
font-family:"Officina";
src:url(OfficinaSansStd-Book.otf);
}
现在在我的 HTML/JavaScript 中我很想说:
context.font = '30px "Officina"';
但这不起作用。如果我使用网络可用的字体(如 Arial),它工作得很好,而当我直接将纯文本写入网页时,Officina 字体显示得很好。我缺少什么?
I am completely baffled as to how to change the font I'm using when drawing text on a canvas. The following is the font I have defined in my CSS:
@font-face{
font-family:"Officina";
src:url(OfficinaSansStd-Book.otf);
}
Now in my HTML/JavaScript I'm tempted to say:
context.font = '30px "Officina"';
But this doesn't work. It works fine if I use a web available font (like Arial), and the Officina font shows up fine when I just write plain text directly to the webpage. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要获得跨浏览器兼容性,您应该使用 CSS 作为嵌入字体,如下所示:
注意:我在 http: //fontsquirrel.com
这对我有用,但我也在 HTML 标记中使用这种字体,所以也许可以使用一种解决方法(如果字体定义没有帮助)该字体在一些隐藏的
div
中,当然我在正文加载后运行所有 JS。To get cross-browser compatibility you should use CSS for the embedded font like this:
Note: I got those font files somewhere at http://fontsquirrel.com
This is working for me, but I'm using this font also in HTML markup, so maybe a workaround (if the font-face definition doesn't help) can be using that font in some hidden
div
, of course I'm running all JS after body loads.对于 2017 年左右遇到这个问题的人来说,最好使用由 Google 和 Typekit 联合制作的 Web Font Loader,位于此处:
- https://github.com/typekit/webfontloader
For folks coming to this question circa 2017 onwards it would be best to use the Web Font Loader that's co-produced by Google and Typekit located here:
- https://github.com/typekit/webfontloader
这是上一个问题,应该对您有帮助。
将文本绘制到 < ;画布>第一次使用@font-face 不起作用
This is previous question should help you.
Drawing text to <canvas> with @font-face does not work at the first time
您可以使用 Google Web Font 加载器,但这对于许多用途来说相当重量级和/或烦人。相反,我会推荐 Jennifer Simonds 的 FontDetect 库,可在 GitHub 上找到:
You can use the Google Web Font loader, but that's rather heavyweight and/or annoying for many uses. Instead, I'll recommend Jennifer Simonds' FontDetect library, available on GitHub:
在使用之前,您可以使用 FontFace API 加载字体画布:
首先下载字体资源 myfont.woff2。下载完成后,字体将添加到文档的 FontFaceSet 中。
在撰写本文时,FontFace API 的规范还是一个工作草案。 请参阅此处的浏览器兼容性表。
然后,您可以使用字体参数来设置您的字体系列。
注意:
您可以简单地使用“@font-face”CSS 加载字体,但请记住,在画布上绘制之前,必须将字体加载到文档中。
你的所有字体和图像(实际上是所有资源)应该并且必须处于同源或启用 cors-origin,否则大多数浏览器将拒绝网络请求。
You can load fonts with the FontFace API before using it in the canvas:
The font resource myfont.woff2 is first downloaded. Once the download completes, the font is added to the document's FontFaceSet.
The specification of the FontFace API is a working draft at the time of this writing. See browser compatibility table here.
Then, you can use the font parameter to set your font family.
Note:
You can simply load your font using "@font-face" CSS, but remember, before you draw on canvas, your font must be loaded to the document.
All of your fonts and images (actually all resources) should be and must be within the same origin or cors-origin enabled, otherwise most of the browser will reject the network request.
注意:自 2016 年起已过时
使用此技巧并将
onerror
事件绑定到Image< /代码> 元素。
此处演示:http://jsfiddle.net/g6LyK/ — 适用于最新的 Chrome。
NOTE: Outdated as of 2016
Use this trick and bind an
onerror
event to anImage
element.Demo here: http://jsfiddle.net/g6LyK/ — works on the latest Chrome.
尝试将您的网址作为字符串如何:
How about try to put your url as a string: