Chromium Code Reviews| 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<!Animation> */ | |
| 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(self.animation_); | |
|
Dan Beam
2016/06/17 19:22:42
why are we keeping |animation_| private but passin
michaelpg
2016/06/28 23:17:23
removed from resolve
| |
| 46 // According to the spec, queue a task to fire the event after | |
| 47 // resolving the promise. | |
| 48 setTimeout(function() { | |
| 49 self.dispatchEvent(e); | |
| 50 self.animation_ = undefined; | |
| 51 }); | |
|
Dan Beam
2016/06/17 19:22:42
can you combine this code into:
function dispatch
michaelpg
2016/06/28 23:17:23
Done.
| |
| 52 }); | |
| 53 self.animation_.addEventListener('cancel', function(e) { | |
| 54 reject(new | |
|
Dan Beam
2016/06/17 19:22:42
afaict, we should not reject for a cancelled anima
michaelpg
2016/06/28 23:17:23
there is a TODO in the spec to make this "cancel"
| |
| 55 /** | |
| 56 * @see https://heycam.github.io/webidl/#es-DOMException-call | |
| 57 * @type {function (new:DOMException, string, string)} | |
| 58 **/( | |
|
Dan Beam
2016/06/17 19:22:42
**/ -> */
michaelpg
2016/06/28 23:17:23
Done.
| |
| 59 DOMException | |
| 60 )('', 'AbortError')); | |
| 61 setTimeout(function() { | |
| 62 self.dispatchEvent(e); | |
| 63 self.animation_ = undefined; | |
| 64 }); | |
| 65 }); | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 Animation.prototype = { | |
| 70 __proto__: cr.EventTarget.prototype, | |
| 71 | |
| 72 finish: function() { | |
| 73 assert(this.animation_); | |
| 74 this.animation_.finish(); | |
| 75 }, | |
| 76 | |
| 77 cancel: function() { | |
| 78 assert(this.animation_); | |
| 79 this.animation_.cancel(); | |
| 80 }, | |
| 81 }; | |
| 82 | |
| 83 return { | |
| 84 Animation: Animation, | |
| 85 Timing: Timing, | |
| 86 }; | |
| 87 }); | |
| OLD | NEW |