javascript动画
Julian Shapiro is a world class developer. I first met him when he dropped CSS vs. JS Animation: Which is Faster?, which caught industry attention, and then he was nice enough to return with The Simple Intro to SVG Animation. It didn't take long to realize that Julian was a special talent.
朱利安·夏皮罗(Julian Shapiro)是世界一流的开发商。 当他放弃CSS vs. JS Animation时,我第一次见到他:哪个更快? 引起了业界的关注,然后他很高兴能够将《简单入门》重新带回SVG动画 。 很快就意识到朱利安是一个特殊的才华。
Julian has recently written an exceptional book: Web Animation using JavaScript: Develop & Design. Julian has blessed us all with the fifth chapter of his book: Animating Text. Enjoy!
朱利安(Julian)最近写了一本非常出色的书:《 使用JavaScript的网络动画:开发与设计》 。 朱利安用他的书的第五章“动画文本”祝福了我们所有人。 请享用!
Since textual animation is rarely employed in webpages, using it is an easy way to impress users. That's precisely what makes this topic so much fun to learn: the underlying techniques are simple to program, but the results feel incredibly rich and complex to the user.
由于文本动画很少在网页中使用,因此使用它是打动用户的一种简便方法。 这正是使该主题变得如此有趣的原因:底层技术易于编程,但结果给用户带来了难以置信的丰富和复杂。
This article introduces you to tools that remove the tedious aspects of textual animation and equip you with an efficient workflow. Read on to learn the nuances of this dark art.
本文向您介绍了一些工具,这些工具可以消除文本动画的繁琐部分,并为您提供高效的工作流程。 继续阅读以了解这种黑暗艺术的细微差别。
文字动画的标准方法 (The standard approach to text animation)
The standard HTML elements we code sites with—div
s, table
s, anchor tags, and the like—are the lowest-level components of a webpage that can be styled. Consequently, these are the lowest-level components that can be animated.
我们使用div
, table
,锚标记等对网站进行编码的标准HTML元素是可样式化的网页的最低级组件。 因此,这些是可以动画化的最低级别的组件。
Text does not constitute an element unto itself; a block of text is designated by the browser as a text node, which is an unstylable, lower-level component that must be contained by an element. Further complicating matters is the fact that the browser does not subdivide text nodes into grammatical components; there is no way to access individual letters, words, or sentences.
文字本身并不构成要素; 文本块由浏览器指定为文本节点 ,它是元素必须包含的无法样式化的较低级组件。 更复杂的是,浏览器没有将文本节点细分为语法成分。 无法访问单个字母,单词或句子。
Consequently, to animate text on a letter, word, or sentence basis, you have to break each text node into separate text nodes, and then wrap each of these in a new element. You can then animate them. But manually wrapping text in span elements, for example, is tedious work that results in bloated HTML.
因此,要以字母,单词或句子为基础的文本动画,必须将每个文本节点分解为单独的文本节点,然后将每个文本节点包装在一个新元素中。 然后可以为它们设置动画。 但是,例如,将文本手动包裹在span元素中是繁琐的工作,导致HTML膨胀。
It's no surprise then that text animation on the web is uncommon; it's typically too much of a hassle to deal with. This puts the web at an aesthetic disadvantage to dedicated motion design software, such as Adobe After Effects, which allows for the fine-grained animation of text—the results of which are commonly seen in TV commercials and movie title sequences. These effects can look absolutely beautiful. Unfortunately, in addition to being difficult to integrate on the web, they're also widely considered bad practice. After all, the web is a medium that prioritizes function over form, and text animation is largely about form.
不足为奇的是,网络上的文本动画并不常见。 通常这太麻烦了。 这使Web在专用运动设计软件(如Adobe After Effects)上在美学上处于劣势,该软件允许对文本进行细粒度的动画处理-其结果通常出现在电视广告和电影标题序列中。 这些效果看起来绝对漂亮。 不幸的是,除了很难集成到网络上之外,它们还被广泛认为是不好的做法。 毕竟,网络是一种优先考虑功能而不是形式的媒介,而文本动画在很大程度上与形式有关。
However, there is one textual animation use case that can carry over well to the web when used sparingly: if you pay close attention to the depictions of futuristic hardware interfaces in movies, you'll notice the common thread of text being animated into or out of view on a grammatical level. The future of computing, according to pop culture, consists of words and sentences animating with flickers, glitches, pops, and blurs. These effects look cool, and there isn't much downside to embracing them for the purposes of transitioning content into or out of view since the text had to undergo a visibility animation by one means or another. This concept of transitioning text visibility is precisely what you'll learn about in this article.
但是,有一种文本动画用例在少量使用时可以很好地延续到网络上:如果您密切关注电影中未来派硬件接口的描述,您会注意到被动画化为文本的通用线程。在语法层面上的观点。 根据流行文化,计算的未来由单词和句子组成,这些单词和句子具有闪烁,小故障,流行和模糊的动画效果。 这些效果看起来很酷,并且为了将内容转换为视图或从视图中移出,拥抱它们没有太多不利之处,因为文本必须通过一种或多种方式进行可见性动画处理。 过渡文本可见性的概念正是您将在本文中学到的。
使用Blast.js为动画准备文本元素 (Preparing text elements for animation with Blast.js)
The tool of choice for typographic animation is Blast.js, which handily breaks blocks of text into characters, words, and sentences. You can then animate the resulting parts using Velocity and its UI pack plugin.
印刷动画的首选工具是Blast.js,它可以轻松地将文本块分解为字符,单词和句子。 然后,您可以使用Velocity及其UI Pack插件为生成的零件设置动画。
NOTE: Get Blast.js at Julian.com/research/blast.
注意:从Julian.com/research/blast获取Blast.js 。
Blast.js has three delimiter types to define the grammatical components to be individually extracted: character, word, and sentence. Suppose you have a div
that looks like this:
Blast.js具有三种定界符类型,用于定义要单独提取的语法成分:字符,单词和句子。 假设您有一个如下所示的div
:
Hello World
If you call Blast on this div
using the following syntax:
如果您使用以下语法在此div
上调用Blast:
$("div").blast({ delimiter: "word" });
the div
would turn into this:
div
会变成这样:
Hello
World
As you can see, Blast separated the target div
's text into text parts that are individually wrapped in span elements. If you were to instead use the character
delimiter, the result would have been:
如您所见,Blast将目标div
的文本分成多个文本部分,这些文本部分分别包装在span元素中。 如果改为使用character
定界符,则结果将是:
H
e
l
l
o
W
o
r
l
d
You can now animate these span elements independently. Before you dive into textual animation, however, you're going to learn more about how Blast works so you can take full advantage of its powerful features.
现在,您可以独立为这些span元素设置动画。 但是,在深入研究文本动画之前,您将了解有关Blast如何工作的更多信息,以便您可以充分利用其强大的功能。
Blast.js的工作方式 (How Blast.js works)
The goal of this section is to make you comfortable with the prospect of using Blast to break apart the text on your beloved page. Let's dive in!
本部分的目的是使您对使用Blast拆分喜爱的页面上的文本的前景感到满意。 让我们潜入吧!
divs
, tables
, and the other HTML elements that you're familiar with are called element nodes. An element node commonly consists of two types of children: additional element nodes and text nodes (raw text).
divs
, tables
和您熟悉的其他HTML元素称为元素节点 。 元素节点通常由两种子元素组成:附加元素节点和文本节点(原始文本)。
Take this element, for example:
以这个元素为例:
Hello World
This div
element is composed of two children: a text node ("Hello") and a span element node. The span element node contains a child of its own: another text node ("World").
该div
元素由两个子元素组成:一个文本节点(“ Hello”)和一个span元素节点。 span元素节点包含自己的子节点:另一个文本节点(“世界”)。
When Blast is called, it traverses the entirety of the target element's descendant element chain to find text nodes. With each text node, Blast executes the RegEx query associated with the specified delimiter type (character
, word
, or sentence
) to subdivide the node into new elements, each with its own text node part. Since Blast doesn't actually subdivide element nodes—only text nodes—you can safely apply it to the entire page without worrying about breaking elements' event handlers and other expected behaviors. This versatility is crucial when using Blast on user-generated content that is often dirtied with HTML. (Say, for example, you want to separate the words in a message posted to your site's comments section so you can highlight important passages. With Blast, you can safely do so without concern for breaking the user's embedded links.)
调用Blast时,它将遍历目标元素的所有后代元素链以查找文本节点。 对于每个文本节点,Blast执行与指定的定界符类型( character
, word
或sentence
)关联的RegEx查询,以将该节点细分为新元素,每个元素都有其自己的文本节点部分。 由于Blast实际上并不细分元素节点(仅细分文本节点),因此您可以安全地将其应用于整个页面,而不必担心破坏元素的事件处理程序和其他预期行为。 当对用户生成的内容(通常会被HTML弄脏)使用Blast时,这种多功能性至关重要。 (例如,您要分隔发布到站点评论部分的消息中的单词,以便突出显示重要的段落。借助Blast,您可以放心地这样做,而不必担心破坏用户的嵌入式链接。)
In addition to its robustness, Blast provides a high level of accuracy. It doesn't dumbly split words at spaces, nor does it split sentences at periods within words. It leverages UTF-8 character sets for Latin alphabet languages, meaning that you can accurately apply it to French, German, Spanish, English, Italian, and Portuguese content.
除了强大的功能外,Blast还提供了很高的准确性。 它不会在空格处愚蠢地拆分单词,也不会在单词中的句点拆分句子。 它针对拉丁字母语言利用了UTF-8字符集,这意味着您可以将其准确地应用于法语,德语,西班牙语,英语,意大利语和葡萄牙语内容。
Suppose you used Blast's sentence
delimiter on the following paragraph. (Note that bold and italic are used below to indicate the consecutive text matches that Blast detects.) Blast correctly identified six sentences in the paragraph:
假设您在以下段落中使用了Blast的sentence
定界符。 (请注意,下面使用粗体和斜体表示Blast检测到的连续文本匹配项。)Blast在段落中正确标识了六个句子:
¿Will the sentence delimiter recognize this full sentence containing Spanish punctuation? ¡Yes! « Mais, oui ! » "Nested "quotes" don't break the sentence delimiter!" Further, periods inside words (e.g. Blast.js), in formal titles (e.g. Mrs. Bluth, Dr. Fünke), and in "e.g." and "i.e." do not falsely match as sentence-final punctuation. Darn. That's pretty impressive.
¿句子定界符会识别出包含西班牙标点符号的完整句子吗? 是! «Mais,哦! » “嵌套的“引号”不要破坏句子定界符!” 此外,单词(例如Blast.js),正式标题(例如Bluth夫人,Fünke博士)以及“例如”和“即”中的句点不会错误地匹配为句末标点。 真是 真是令人印象深刻。
Notice how punctuation is associated with its proper sentence, and how errant periods don't falsely demarcate sentence matches.
注意标点符号如何与其适当的句子相关联,以及错误的期间不会错误地划分句子匹配项。
With these foundations covered, it's time to run through how to use Blast.
了解了这些基础之后,就该介绍如何使用Blast。
安装 (Installation)
Blast is installed on a page like any other JavaScript plugin: embed the appropriate script link before your page's