2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10 <link rel=
"import" href=
"../polymer/polymer.html">
11 <link rel=
"import" href=
"web-animations.html">
14 @group Polymer Core Elements
16 `core-animation` is a convenience element to use web animations with Polymer elements. It
17 allows you to create a web animation declaratively. You can extend this class to create
18 new types of animations and combine them with `core-animation-group`.
20 Example to create animation to fade out an element over 500ms:
22 <core-animation id="fadeout" duration="500">
23 <core-animation-keyframe>
24 <core-animation-prop name="opacity" value="1"></core-animation-prop>
25 </core-animation-keyframe>
26 <core-animation-keyframe>
27 <core-animation-prop name="opacity" value="0"></core-animation-prop>
28 </core-animation-keyframe>
31 <div id="el">Fade me out</div>
34 var animation = document.getElementById('fadeout');
35 animation.target = document.getElementById('el');
39 Or do the same imperatively:
41 var animation = new CoreAnimation();
42 animation.duration = 500;
43 animation.keyframes = [
47 animation.target = document.getElementById('el');
50 You can also provide a javascript function instead of keyframes to the animation. This
51 behaves essentially the same as `requestAnimationFrame`:
53 var animation = new CoreAnimation();
54 animation.customEffect = function(timeFraction, target, animation) {
55 // do something custom
59 Elements that are targets to a `core-animation` are given the `core-animation-target` class.
61 @element core-animation
65 <polymer-element name="core-animation" constructor="CoreAnimation" attributes="target keyframes customEffect composite duration fill easing iterationStart iterationCount delay direction autoplay targetSelector">
69 function toNumber(value, allowInfinity) {
70 return (allowInfinity && value === 'Infinity') ? Number.POSITIVE_INFINITY : Number(value);
75 * Fired when the animation completes.
77 * @event core-animation-finish
82 * Fired when the web animation object changes.
84 * @event core-animation-change
90 * One or more nodes to animate.
93 * @type HTMLElement|Node|Array<HTMLElement|Node>
95 target: {value: null, reflect: true},
98 * Animation keyframes specified as an array of dictionaries of
99 * <css properties>:<array of values> pairs. For example,
101 * @property keyframes
104 keyframes: {value: null, reflect: true},
107 * A custom animation function. Either provide this or `keyframes`. The signature
108 * of the callback is `EffectsCallback(timeFraction, target, animation)`
110 * @property customEffect
111 * @type Function(number, Object, Object)
113 customEffect: {value: null, reflect: true},
116 * Controls the composition behavior. If set to "replace", the effect overrides
117 * the underlying value for the target. If set the "add", the effect is added to
118 * the underlying value for the target. If set to "accumulate", the effect is
119 * accumulated to the underlying value for the target.
121 * In cases such as numbers or lengths, "add" and "accumulate" produce the same
122 * value. In list values, "add" is appending to the list, while "accumulate" is
123 * adding the individual components of the list.
125 * For example, adding `translateX(10px)` and `translateX(25px)` produces
126 * `translateX(10px) translateX(25px)` and accumulating produces `translateX(35px)`.
128 * @property composite
129 * @type "replace"|"add"|"accumulate"
132 composite: {value: 'replace', reflect: true},
135 * Animation duration in milliseconds, "Infinity", or "auto". "auto" is
139 * @type number|"Infinity"
142 duration: {value: 'auto', reflect: true},
145 * Controls the effect the animation has on the target when it's not playing.
146 * The possible values are "none", "forwards", "backwards", "both" or "auto".
148 * "none" means the animation has no effect when it's not playing.
150 * "forwards" applies the value at the end of the animation after it's finished.
152 * "backwards" applies the value at the start of the animation to the target
153 * before it starts playing and has no effect when the animation finishes.
155 * "both" means "forwards" and "backwards". "auto" is equivalent to "none".
158 * @type "none"|"forwards"|"backwards"|"both"|"auto"
161 fill: {value: 'auto', reflect: true},
164 * A transition timing function. The values are equivalent to the CSS
171 easing: {value: 'linear', reflect: true},
174 * The number of milliseconds to delay before beginning the animation.
180 delay: {value: 0, reflect: true},
183 * The number of milliseconds to wait after the animation finishes. This is
184 * useful, for example, in an animation group to wait for some time before
185 * beginning the next item in the animation group.
191 endDelay: {value: 0, reflect: true},
194 * The number of iterations this animation should run for.
196 * @property iterations
197 * @type Number|'Infinity'
200 iterations: {value: 1, reflect: true},
203 * Number of iterations into the animation in which to begin the effect.
204 * For example, setting this property to 0.5 and `iterations` to 2 will
205 * cause the animation to begin halfway through the first iteration but still
208 * @property iterationStart
212 iterationStart: {value: 0, reflect: true},
215 * (not working in web animations polyfill---do not use)
217 * Controls the iteration composition behavior. If set to "replace", the effect for
218 * every iteration is independent of each other. If set to "accumulate", the effect
219 * for iterations of the animation will build upon the value in the previous iteration.
223 * // Moves the target 50px on the x-axis over 5 iterations.
224 * <core-animation iterations="5" iterationComposite="accumulate">
225 * <core-animation-keyframe>
226 * <core-animation-prop name="transform" value="translateX(10px)"></core-animation-prop>
227 * </core-animation-keyframe>
230 * @property iterationComposite
231 * @type "replace"|"accumulate"
234 iterationComposite: {value: 'replace', reflect: true},
237 * The playback direction of the animation. "normal" plays the animation in the
238 * normal direction. "reverse" plays it in the reverse direction. "alternate"
239 * alternates the playback direction every iteration such that even iterations are
240 * played normally and odd iterations are reversed. "alternate-reverse" plays
241 * even iterations in the reverse direction and odd iterations in the normal
244 * @property direction
245 * @type "normal"|"reverse"|"alternate"|"alternate-reverse"
248 direction: {value: 'normal', reflect: true},
251 * A multiplier to the playback rate to the animation.
253 * @property playbackRate
257 playbackRate: {value: 1, reflect: true},
260 * If set to true, play the animation when it is created or a property is updated.
266 autoplay: {value: false, reflect: true}
275 customEffect: 'apply',
281 iterationStart: 'apply',
282 iterationComposite: 'apply',
286 playbackRate: 'apply',
295 * Plays the animation. If the animation is currently paused, seeks the animation
296 * to the beginning before starting playback.
299 * @return AnimationPlayer The animation player.
303 if (this.animation && !this.autoplay) {
304 this.player = document.timeline.play(this.animation);
305 this.player.onfinish = this.animationFinishHandler.bind(this);
311 * Stops the animation and clears all effects on the target.
317 this.player.cancel();
322 * Seeks the animation to the end.
328 this.player.finish();
333 * Pauses the animation.
345 * @return boolean True if `target` is defined.
347 hasTarget: function() {
348 return this.target !== null;
352 * Creates a web animations object based on this object's properties, and
353 * plays it if autoplay is true.
356 * @return Object A web animation.
359 this.animation = this.makeAnimation();
360 if (this.autoplay && this.animation) {
363 return this.animation;
366 makeSingleAnimation: function(target) {
367 // XXX(yvonne): for selecting all the animated elements.
368 target.classList.add('core-animation-target');
369 return new Animation(target, this.animationEffect, this.timingProps);
372 makeAnimation: function() {
377 if (Array.isArray(this.target)) {
379 this.target.forEach(function(t) {
380 array.push(this.makeSingleAnimation(t));
382 animation = new AnimationGroup(array);
384 animation = this.makeSingleAnimation(this.target);
389 animationChanged: function() {
390 // Sending 'this' with the event so you can always get the animation object
391 // that fired the event, due to event retargetting in shadow DOM.
392 this.fire('core-animation-change', this);
395 targetChanged: function(old) {
397 old.classList.remove('core-animation-target');
404 delay: {isNumber: true},
405 endDelay: {isNumber: true},
407 iterationStart: {isNumber: true},
408 iterations: {isNumber: true, allowInfinity: true},
409 duration: {isNumber: true},
410 playbackRate: {isNumber: true},
415 if (this[t] !== null) {
416 var name = timing[t].property || t;
417 props[name] = timing[t].isNumber && this[t] !== 'auto' ?
418 toNumber(this[t], timing[t].allowInfinity) : this[t];
424 get animationEffect() {
428 if (this.keyframes) {
429 frames = this.keyframes;
430 } else if (!this.customEffect) {
431 var children = this.querySelectorAll('core-animation-keyframe');
432 if (children.length === 0 && this.shadowRoot) {
433 children = this.shadowRoot.querySelectorAll('core-animation-keyframe');
435 Array.prototype.forEach.call(children, function(c) {
436 frames.push(c.properties);
439 if (this.customEffect) {
440 effect = this.customEffect;
442 // effect = new KeyframeEffect(frames, this.composite);
448 animationFinishHandler: function() {
449 this.fire('core-animation-finish');
458 `core-animation-keyframe` represents a keyframe in a `core-animation`. Use them as children of
459 `core-animation` elements to create web animations declaratively. If the `offset` property is
460 unset, the keyframes will be distributed evenly within the animation duration. Use
461 `core-animation-prop` elements as children of this element to specify the CSS properties for
464 @element core-animation-keyframe
468 <polymer-element name="core-animation-keyframe" attributes="offset">
473 * An offset from 0 to 1.
478 offset: {value: null, reflect: true}
482 var children = this.querySelectorAll('core-animation-prop');
483 Array.prototype.forEach.call(children, function(c) {
484 props[c.name] = c.value;
486 if (this.offset !== null) {
487 props.offset = this.offset;
496 `core-animation-prop` represents a CSS property and value pair to use with
497 `core-animation-keyframe`.
499 @element core-animation-prop
503 <polymer-element name="core-animation-prop" attributes="name value">
508 * A CSS property name.
513 name: {value: '', reflect: true},
516 * The value for the CSS property.
519 * @type string|number
521 value: {value: '', reflect: true}