OLD | NEW |
| (Empty) |
1 [](https://travis-ci.org/web-animations/web-animations-js) | |
2 | |
3 Latest specification at http://w3c.github.io/web-animations/. | |
4 | |
5 ## Learn the tech | |
6 | |
7 ### Why Web Animations? | |
8 | |
9 Four animation-related specifications already exist on the web platform: [CSS Tr
ansitions](http://dev.w3.org/csswg/css-transitions/), | |
10 [CSS Animations](http://dev.w3.org/csswg/css-animations/), [SVG Animations](http
://www.w3.org/TR/SVG/animate.html) / [SMIL](http://www.w3.org/TR/2001/REC-smil-a
nimation-20010904/), and `requestAnimationFrame()`. However: | |
11 | |
12 - *CSS Transitions / CSS Animations are not very expressive* - animations can't | |
13 be composed, or sequenced, or even reliably run in parallel; and animations can'
t be tweaked from script. | |
14 - *SVG Animations are very expressive, but also very complicated*. SVG Animation
s | |
15 can't be applied to HTML content. | |
16 - *`requestAnimationFrame()` is not a declarative approach* - it requires the us
e | |
17 of the main thread, and will therefore jank if the main thread is busy. | |
18 | |
19 Web Animations is a new specification for animated content on the web. It's bein
g | |
20 developed as a W3C specification as part of the CSS and SVG working groups. It a
ims | |
21 to address the deficiencies inherent in these four specifications. Web Animation
s also aims to replace the underlying implementations of CSS Transitions, CSS An
imations and SVG Animations, so that: | |
22 | |
23 - The code cost of supporting animations on the web is reduced. | |
24 - The various animations specifications are interoperable. | |
25 - Spec authors and browser vendors have a single place to experiment with new an
imation innovations to improve the Web for the future. | |
26 | |
27 ### Basic usage | |
28 | |
29 Here's a simple example of an animation that scales and changes the opacity of | |
30 a `<div>` over 0.5 seconds. The animation alternates producing a pulsing effect. | |
31 | |
32 <div class="pulse" style="width:150px;">Hello world!</div> | |
33 <script> | |
34 var elem = document.querySelector('.pulse'); | |
35 var player = document.timeline.play(new Animation(elem, [ | |
36 {opacity: "0.5", transform: "scale(0.5)"}, | |
37 {opacity: "1.0", transform: "scale(1)"} | |
38 ], | |
39 { | |
40 direction: "alternate", duration: 500, iterations: Infinity | |
41 })); | |
42 </script> | |
43 | |
44 ### The animation model | |
45 | |
46 The Web Animations model is a description of an engine for animation content on
the web. The engine is sufficiently powerful to support CSS Transitions, CSS Ani
mations and SVG Animations. | |
47 | |
48 Web Animations also exposes a JS API to the model. This API defines a number of | |
49 new interfaces that are exposed to JavaScript. We'll go through some of the more | |
50 important ones here: Animations, AnimationEffects, TimingDictionaries, TimingGro
ups, and AnimationPlayers. | |
51 | |
52 An `Animation` object defines a single animation effect that applies to a single
element target. For example: | |
53 | |
54 var animation = new Animation(targetElement, | |
55 [{left: '0px'}, {left: '100px'}], 2000); | |
56 | |
57 Here, the target element's "left" CSS property is modified smoothly from `0px` t
o `100px` over 2 seconds. | |
58 | |
59 ### Specifying animation effects | |
60 | |
61 An `AnimationEffect` object controls which CSS properties and SVG attributes are | |
62 modified by an animation, and the values that those properties and attributes | |
63 vary between. AnimationEffect objects also control whether the effect replaces | |
64 or adds to the underlying value. | |
65 | |
66 There are three major kinds of effects: `KeyframeEffect`, `MotionPathEffect`, an
d `EffectCallback`. | |
67 | |
68 #### Animating between keyframes | |
69 | |
70 A `KeyframeEffect` controls one or more properties/attributes by linearly | |
71 interpolating values between specified keyframes. KeyframeEffects are usually | |
72 defined by specifying the keyframe offset and the property-value pair in a | |
73 dictionary: | |
74 | |
75 [ | |
76 {offset: 0.2, left: "35px"}, | |
77 {offset: 0.6, left: "50px"}, | |
78 {offset: 0.9, left: "70px"}, | |
79 ] | |
80 | |
81 If the offset is not specified, keyframes are evenly distributed at offsets | |
82 between 0 and 1. | |
83 | |
84 [{left: "35px"}, {left: "50px"}, {left: "70px"}] | |
85 | |
86 See the [specification](http://www.w3.org/TR/web-animations/#keyframe-animation-
effects) for the details | |
87 of the keyframe distribution procedure, and how KeyframeEffects are | |
88 evaluated at offsets outside those specified by the keyframes. | |
89 | |
90 #### Animating along paths | |
91 | |
92 A `MotionPathEffect` allows elements to be animated along SVG-style paths. For e
xample: | |
93 | |
94 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> | |
95 <defs> | |
96 <path id=path d="M 100,100 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0"/> | |
97 </defs> | |
98 </svg> | |
99 <script> | |
100 var animFunc = new MotionPathEffect(document.querySelector('#path').pathSe
gList); | |
101 var animation = new Animation(targetElement, animFunc, 2000); | |
102 </script> | |
103 | |
104 #### Custom animation effects | |
105 | |
106 An `EffectCallback` allows animations to generate call-outs to JavaScript | |
107 rather than manipulating properties directly. Please see the | |
108 [specification](http://www.w3.org/TR/web-animations/#custom-effects) for more de
tails on this | |
109 feature. | |
110 | |
111 ### Sequencing and synchronizing animations | |
112 | |
113 Two different types of TimingGroups (`AnimationGroup` and `AnimationSequence`) a
llow animations to be synchronized and sequenced. | |
114 | |
115 To play a list of animations in parallel: | |
116 | |
117 var animationGroup = new AnimationGroup([new Animation(...), new Animation(.
..)]); | |
118 | |
119 To play a list in sequence: | |
120 | |
121 var animationSequence = new AnimationSequence([new Animation(...), new Anima
tion(...)]); | |
122 | |
123 Because `Animation`, `AnimationGroup`, `AnimationSequence` are all TimedItems, g
roups can be nested: | |
124 | |
125 var animationGroup = new AnimationGroup([ | |
126 new AnimationSequence([ | |
127 new Animation(...), | |
128 new Animation(...), | |
129 ]), | |
130 new Animation(...) | |
131 ]); | |
132 | |
133 Groups also take an optional TimingDictionary parameter (see below), which among
other things allows iteration and timing functions to apply at the group level: | |
134 | |
135 var animationGroup = new AnimationGroup([new Animation(...), new Animation(.
..)], {iterations: 4}); | |
136 | |
137 ### Controlling the animation timing | |
138 | |
139 TimingDictionaries are used to control the internal timing of an animation (play
ers control how an animation progresses relative to document time). TimingDictio
naries have several properties that can be tweaked: | |
140 | |
141 - **duration**: the duration of a single iteration of the animation | |
142 - **iterations**: the number of iterations of the animation that will be played
(fractional iterations are allowed) | |
143 - **iterationStart**: the start offset of the first iteration | |
144 - **fill**: whether the animation has effect before starting the first iteration
and/or after finishing the final iteration | |
145 - **delay**: the time between the animation's start time and the first animation
effect of the animation | |
146 - **playbackRate**: the rate at which the animation progresses relative to exter
nal time | |
147 - **direction**: the direction in which successive iterations of the animation p
lay back | |
148 - **easing**: fine-grained control over how external time impacts an animation a
cross the total active duration of the animation. | |
149 | |
150 The values provided within TimingDictionaries combine with the animation hierarc
hy | |
151 to generate concrete start and end values for animation iterations, animation | |
152 backwards fills, and animation forwards fills. There are a few simple rules whic
h govern this: | |
153 | |
154 - Animations never extend beyond the start or end values of their parent iterati
on. | |
155 - Animations only fill beyond their parent iteration if: | |
156 - the relevant fill value is selected for the animation; | |
157 - the matching fill value is selected for the parent; and | |
158 - this is the first parent iteration (for `fill: 'backwards'`) or last paren
t iteration (for `fill: 'forwards'`) | |
159 - Missing `duration` values for TimingGroups are generated based on the calculat
ed durations of the child animations. | |
160 | |
161 The following example illustrates these rules: | |
162 | |
163 var animationGroup = new AnimationGroup([ | |
164 new AnimationSequence([ | |
165 new Animation(..., {duration: 3000}), | |
166 new Animation(..., {duration: 5000, fill: 'both'}) | |
167 ], {duration: 6000, delay: 3000, fill: 'none'}), | |
168 new Animation(..., {duration: 8000, fill: 'forwards'}) | |
169 ], {iterations: 2, fill: 'forwards'}); | |
170 | |
171 In this example: | |
172 | |
173 - The `AnimationSequence` has an explicit `duration` of 6 seconds, and so the | |
174 second child animation will only play for the first 3 of its 5 second duration | |
175 - The `AnimationGroup` has no explicit duration, and will be provided with a | |
176 calculated duration of the max (`duration + delay`) of its children - in this ca
se 9 seconds. | |
177 - Although `fill: "both"` is specified for the second `Animation` within the `An
imationSequence`, the `AnimationSequence` itself has a `fill` of "none". Hence,
as the animation ends right at the end of the `AnimationSequence`, the animation
will only fill backwards, and only up until the boundary of the `AnimationSeque
nce` (i.e. 3 seconds after the start of the `AnimationGroup`). | |
178 - The `Animation` inside the `AnimationGroup` and the `AnimationGroup` are both
`fill: "forwards"`. Therefore the animation will fill forward in two places: | |
179 - from 8 seconds after the `AnimationGroup` starts until the second iteratio
n of the `AnimationGroup` starts (i.e. for 1 second) | |
180 - from 17 seconds after the `AnimationGroup` starts, extending forward indef
initely. | |
181 | |
182 ### Playing Animations | |
183 | |
184 In order to play an `Animation` or `TimingGroup`, an `AnimationPlayer` must be c
onstructed: | |
185 | |
186 var player = document.timeline.play(myAnimation); | |
187 | |
188 AnimationPlayers provide complete control the start time and current playback he
ad of their attached animation. However, players can't modify any internal detai
ls of an animation. | |
189 | |
190 AnimationPlayers can be used to pause, seek, reverse, or modify the playback rat
e of an animation. | |
191 | |
192 `document.timeline.currentTime` is a timeline's global time. It gives the number | |
193 of seconds since the document fired its load event. | |
194 | |
195 ## Polyfill details | |
196 | |
197 ### Getting started | |
198 | |
199 Include `web-animations.js` in your project: | |
200 | |
201 <script src="web-animations-js/web-animations.js"></script> | |
202 | |
203 ### Polyfill notes | |
204 | |
205 #### Prefix handling | |
206 | |
207 In order to work in as many browsers as feasible, we have decided to take the | |
208 following approach to prefix handling: | |
209 | |
210 - the polyfill will automatically detect the correctly prefixed name to use when | |
211 writing animated properties back to the platform. | |
212 - where possible, the polyfill will *only* accept unprefixed versions of experim
ental features. For example: | |
213 | |
214 var animation = new Animation(elem, {"transform": "translate(100px, 100p
x)"}, 2000); | |
215 | |
216 will work in all browsers that implement a conforming version of `transform`,
but | |
217 | |
218 var animation = new Animation(elem, {"-webkit-transform": "translate(10
0px, 100px)"}, 2000); | |
219 | |
220 will not work anywhere. | |
221 | |
222 #### Experimental features | |
223 | |
224 When the polyfill requires features to implement functionality that is not inher
ently specified using those | |
225 features (for example, CSS `calc()` is required in order to implement merging be
tween lengths with different units) | |
226 then the polyfill will provide a console warning in browsers where these feature
s are absent. | |
227 | |
228 ## Tools & testing | |
229 | |
230 For running tests or building minified files, consult the | |
231 [tooling information](http://www.polymer-project.org/resources/tooling-strategy.
html). | |
232 | |
233 ## Breaking changes | |
234 | |
235 When we make a potentially breaking change to the polyfill's API surface (like a
rename) we'll continue supporting the old version, deprecated, for three months
and ensure that there are console warnings that a change is pending. After thre
e months, the old version of the API surface (e.g. the old version of a function
name) will be removed. If you see deprecation warnings you can't avoid it by no
t updating. | |
236 | |
237 We also announce anything that isn't a bug fix on web-animations-changes@googleg
roups.com (https://groups.google.com/forum/#!forum/web-animations-changes). | |
OLD | NEW |