简体   繁体   English

延迟画布的javascript执行,直到加载了字体

[英]delay execution of javascript for canvas until fonts have been loaded

I'm making something in canvas that uses fonts from a web service and what happens is that when the canvas is first loaded the fonts, which I'm loading from google fonts, haven't arrived yet so for the briefest of moments there's only plain font in the canvas and then it switches to my downloaded font. 我正在画布中制作一些东西,该东西使用来自Web服务的字体,发生的事情是,当第一次加载画布时(我从Google字体加载的字体)还没有到,所以暂时来说,只有画布中的纯字体,然后切换到我下载的字体。

This is a minor detail but very annoying. 这是一个很小的细节,但很烦人。 I'm wondering if theres any way to preload the fonts, ie not execute the canvas associated javascript code until after I'm sure I have the fonts already. 我想知道是否有任何方法可以预加载字体,即在确定已经拥有字体之后才执行与画布相关的javascript代码。

The font is placed in the header of the html: 字体放置在html的标题中:

<link href='https://fonts.googleapis.com/css?family=Stalinist+One' rel='stylesheet' type='text/css'>

and the js links at the bottom of the file, just before the closing of the body. 并且js链接位于文件底部,紧接在主体关闭之前。

Use Google's WebFontLoader script 使用Google的WebFontLoader脚本

https://github.com/typekit/webfontloader https://github.com/typekit/webfontloader

WebFontLoader's fontactive callback is the equivalent of the img element's onload . WebFontLoader的fontactive回调等效于img元素的onload

WebFontLoader is exactly on point and WebFontLoader is maintained by Adobe itself (TypeKit ! )--so it's very unlikely that this CDN-link will vanish. WebFontLoader完全正确,WebFontLoader由Adobe自己维护(TypeKit!)-因此,该CDN链接消失的可能性很小。

While I recommend WebFontLoader, you can hack together a font.onload yourself by detecting when the width of your element changes (changed width == the font has loaded). 在我推荐WebFontLoader的同时,您可以一起font.onload通过检测元素的宽度何时更改(更改的宽度==字体已加载)来自行加载字体。

Adobe has created the Adobe Blank font for the purpose of testing when a font has loaded. Adobe已创建Adobe Blank字体,用于测试加载字体时的目的。 AdobeBlank starts out with zero width. AdobeBlank以零宽度开始。 Here's how to use AdobeBlank as font.onload as outlined by Dr. Ken Lunde in this useful TypeKit blog post: http://blog.typekit.com/2013/03/28/introducing-adobe-blank/ Ken Lunde博士在此有用的TypeKit博客文章中概述了如何使用AdobeBlank作为font.onloadhttp : //blog.typekit.com/2013/03/28/introducing-adobe-blank/

  1. Include Adobe Blank as a data URI in the CSS file. 在CSS文件中包含Adobe Blank作为数据URI。

  2. Specify 'font-family: SomeWebFont, “Adobe Blank” in the CSS rule for some DOM element that contains text (and would therefore have a non-zero width when rendered using a regular font). 在CSS规则中为包含文本的DOM元素指定'font-family:SomeWebFont,“ Adob​​e Blank”(因此,使用常规字体呈现时宽度为非零)。 One example would be a element that is absolutely positioned offscreen. 一个示例是绝对位于屏幕外的元素。

  3. Check the width of the DOM element. 检查DOM元素的宽度。 If it's zero, SomeWebFont hasn't loaded yet. 如果为零,则表示尚未加载SomeWebFont。 If it's greater than zero, it has. 如果大于零,则具有。

Good luck with your project! 祝您项目顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM