Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / third_party / polymer / components / core-animation / core-animation.html
blob9a958c9eafadd6dd204e9ca4f62f5862b2727330
1 <!--
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
8 -->
10 <link rel="import" href="../polymer/polymer.html">
11 <link rel="import" href="web-animations.html">
13 <!--
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>
29 </core-animation>
31 <div id="el">Fade me out</div>
33 <script>
34 var animation = document.getElementById('fadeout');
35 animation.target = document.getElementById('el');
36 animation.play();
37 </script>
39 Or do the same imperatively:
41 var animation = new CoreAnimation();
42 animation.duration = 500;
43 animation.keyframes = [
44 {opacity: 1},
45 {opacity: 0}
47 animation.target = document.getElementById('el');
48 animation.play();
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
57 animation.play();
59 Elements that are targets to a `core-animation` are given the `core-animation-target` class.
61 @element core-animation
62 @status beta
63 @homepage github.io
64 -->
65 <polymer-element name="core-animation" constructor="CoreAnimation" attributes="target keyframes customEffect composite duration fill easing iterationStart iterationCount delay direction autoplay targetSelector">
66 <script>
67 (function() {
69 function toNumber(value, allowInfinity) {
70 return (allowInfinity && value === 'Infinity') ? Number.POSITIVE_INFINITY : Number(value);
73 Polymer({
74 /**
75 * Fired when the animation completes.
77 * @event core-animation-finish
80 /**
82 * Fired when the web animation object changes.
84 * @event core-animation-change
87 publish: {
89 /**
90 * One or more nodes to animate.
92 * @property target
93 * @type HTMLElement|Node|Array<HTMLElement|Node>
95 target: {value: null, reflect: true},
97 /**
98 * Animation keyframes specified as an array of dictionaries of
99 * &lt;css properties&gt;:&lt;array of values&gt; pairs. For example,
101 * @property keyframes
102 * @type Object
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"
130 * @default "replace"
132 composite: {value: 'replace', reflect: true},
135 * Animation duration in milliseconds, "Infinity", or "auto". "auto" is
136 * equivalent to 0.
138 * @property duration
139 * @type number|"Infinity"
140 * @default "auto"
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".
157 * @property fill
158 * @type "none"|"forwards"|"backwards"|"both"|"auto"
159 * @default "auto"
161 fill: {value: 'auto', reflect: true},
164 * A transition timing function. The values are equivalent to the CSS
165 * counterparts.
167 * @property easing
168 * @type string
169 * @default "linear"
171 easing: {value: 'linear', reflect: true},
174 * The number of milliseconds to delay before beginning the animation.
176 * @property delay
177 * @type Number
178 * @default 0
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.
187 * @property endDelay
188 * @type number
189 * @default 0
191 endDelay: {value: 0, reflect: true},
194 * The number of iterations this animation should run for.
196 * @property iterations
197 * @type Number|'Infinity'
198 * @default 1
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
206 * run twice.
208 * @property iterationStart
209 * @type Number
210 * @default 0
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.
221 * Example:
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>
228 * </core-animation>
230 * @property iterationComposite
231 * @type "replace"|"accumulate"
232 * @default false
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
242 * direction.
244 * @property direction
245 * @type "normal"|"reverse"|"alternate"|"alternate-reverse"
246 * @default "normal"
248 direction: {value: 'normal', reflect: true},
251 * A multiplier to the playback rate to the animation.
253 * @property playbackRate
254 * @type number
255 * @default 1
257 playbackRate: {value: 1, reflect: true},
260 * If set to true, play the animation when it is created or a property is updated.
262 * @property autoplay
263 * @type boolean
264 * @default false
266 autoplay: {value: false, reflect: true}
270 animation: false,
272 observe: {
273 target: 'apply',
274 keyframes: 'apply',
275 customEffect: 'apply',
276 composite: 'apply',
277 duration: 'apply',
278 fill: 'apply',
279 easing: 'apply',
280 iterations: 'apply',
281 iterationStart: 'apply',
282 iterationComposite: 'apply',
283 delay: 'apply',
284 endDelay: 'apply',
285 direction: 'apply',
286 playbackRate: 'apply',
287 autoplay: 'apply'
290 ready: function() {
291 this.apply();
295 * Plays the animation. If the animation is currently paused, seeks the animation
296 * to the beginning before starting playback.
298 * @method play
299 * @return AnimationPlayer The animation player.
301 play: function() {
302 this.apply();
303 if (this.animation && !this.autoplay) {
304 this.player = document.timeline.play(this.animation);
305 this.player.onfinish = this.animationFinishHandler.bind(this);
306 return this.player;
311 * Stops the animation and clears all effects on the target.
313 * @method cancel
315 cancel: function() {
316 if (this.player) {
317 this.player.cancel();
322 * Seeks the animation to the end.
324 * @method finish
326 finish: function() {
327 if (this.player) {
328 this.player.finish();
333 * Pauses the animation.
335 * @method pause
337 pause: function() {
338 if (this.player) {
339 this.player.pause();
344 * @method hasTarget
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.
355 * @method apply
356 * @return Object A web animation.
358 apply: function() {
359 this.animation = this.makeAnimation();
360 if (this.autoplay && this.animation) {
361 this.play();
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() {
373 if (!this.target) {
374 return null;
376 var animation;
377 if (Array.isArray(this.target)) {
378 var array = [];
379 this.target.forEach(function(t) {
380 array.push(this.makeSingleAnimation(t));
381 }.bind(this));
382 animation = new AnimationGroup(array);
383 } else {
384 animation = this.makeSingleAnimation(this.target);
386 return animation;
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) {
396 if (old) {
397 old.classList.remove('core-animation-target');
401 get timingProps() {
402 var props = {};
403 var timing = {
404 delay: {isNumber: true},
405 endDelay: {isNumber: true},
406 fill: {},
407 iterationStart: {isNumber: true},
408 iterations: {isNumber: true, allowInfinity: true},
409 duration: {isNumber: true},
410 playbackRate: {isNumber: true},
411 direction: {},
412 easing: {}
414 for (t in timing) {
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];
421 return props;
424 get animationEffect() {
425 var props = {};
426 var frames = [];
427 var effect;
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;
441 } else {
442 // effect = new KeyframeEffect(frames, this.composite);
443 effect = frames;
445 return effect;
448 animationFinishHandler: function() {
449 this.fire('core-animation-finish');
453 })();
454 </script>
455 </polymer-element>
457 <!--
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
462 the animation.
464 @element core-animation-keyframe
465 @status beta
466 @homepage github.io
468 <polymer-element name="core-animation-keyframe" attributes="offset">
469 <script>
470 Polymer({
471 publish: {
473 * An offset from 0 to 1.
475 * @property offset
476 * @type Number
478 offset: {value: null, reflect: true}
480 get properties() {
481 var props = {};
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;
489 return props;
492 </script>
493 </polymer-element>
495 <!--
496 `core-animation-prop` represents a CSS property and value pair to use with
497 `core-animation-keyframe`.
499 @element core-animation-prop
500 @status beta
501 @homepage github.io
503 <polymer-element name="core-animation-prop" attributes="name value">
504 <script>
505 Polymer({
506 publish: {
508 * A CSS property name.
510 * @property name
511 * @type string
513 name: {value: '', reflect: true},
516 * The value for the CSS property.
518 * @property value
519 * @type string|number
521 value: {value: '', reflect: true}
524 </script>
525 </polymer-element>