OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Simplified API wrapping native Web Animations with some sugar. |
| 7 * A compromise between the draft spec and Chrome's evolving support. This API |
| 8 * will be changed (or removed) as Chrome support evolves. |
| 9 */ |
| 10 cr.define('settings.animation', function() { |
| 11 'use strict'; |
| 12 |
| 13 /** |
| 14 * Default timing constants. |
| 15 * @const |
| 16 */ |
| 17 var Timing = { |
| 18 DURATION: 250, |
| 19 EASING: 'cubic-bezier(0.4, 0, 0.2, 1)', // Fast out, slow in. |
| 20 }; |
| 21 |
| 22 /** |
| 23 * Offers a small subset of the v1 Animation interface. The underlying |
| 24 * animation can be reversed, canceled or immediately finished. |
| 25 * @see https://www.w3.org/TR/web-animations-1/#animation |
| 26 * |
| 27 * @constructor |
| 28 * @extends {cr.EventTarget} |
| 29 * @param {!Element} el The element to animate. |
| 30 * @param {!Array<!Object>|!Object<!Array>|!Object<string>} keyframes |
| 31 * Keyframes, as in Element.prototype.animate. |
| 32 * @param {number|!KeyframeEffectOptions=} opt_options Duration or options |
| 33 * object, as in Element.prototype.animate. |
| 34 */ |
| 35 function Animation(el, keyframes, opt_options) { |
| 36 // Disallow direct usage of the underlying animation. |
| 37 this.animation_ = el.animate(keyframes, opt_options); |
| 38 |
| 39 var self = this; |
| 40 /** @type {!Promise} */ |
| 41 this.finished = new Promise(function(resolve, reject) { |
| 42 // If we were implementing the full spec, we'd have to support |
| 43 // removing or resetting these listeners. |
| 44 self.animation_.addEventListener('finish', function(e) { |
| 45 resolve(); |
| 46 // According to the spec, queue a task to fire the event after |
| 47 // resolving the promise. |
| 48 self.queueDispatch_(e); |
| 49 }); |
| 50 self.animation_.addEventListener('cancel', function(e) { |
| 51 reject(new |
| 52 /** |
| 53 * @see https://heycam.github.io/webidl/#es-DOMException-call |
| 54 * @type {function (new:DOMException, string, string)} |
| 55 */( |
| 56 DOMException |
| 57 )('', 'AbortError')); |
| 58 self.queueDispatch_(e); |
| 59 }); |
| 60 }); |
| 61 } |
| 62 |
| 63 Animation.prototype = { |
| 64 __proto__: cr.EventTarget.prototype, |
| 65 |
| 66 finish: function() { |
| 67 assert(this.animation_); |
| 68 this.animation_.finish(); |
| 69 }, |
| 70 |
| 71 cancel: function() { |
| 72 assert(this.animation_); |
| 73 this.animation_.cancel(); |
| 74 }, |
| 75 |
| 76 /** |
| 77 * @param {!Event} e |
| 78 * @private |
| 79 */ |
| 80 queueDispatch_: function(e) { |
| 81 setTimeout(function() { |
| 82 this.dispatchEvent(e); |
| 83 this.animation_ = undefined; |
| 84 }.bind(this)); |
| 85 }, |
| 86 }; |
| 87 |
| 88 return { |
| 89 Animation: Animation, |
| 90 Timing: Timing, |
| 91 }; |
| 92 }); |
OLD | NEW |