| <!-- |
| // Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| --> |
| <script> |
| module.exports = class AnimationTimer { |
| constructor(delegate) { |
| this.delegate_ = delegate; |
| this.startTime_ = 0; |
| this.duration_ = 0; |
| this.animationId_ = 0; |
| Object.preventExtensions(this); |
| } |
| |
| start(duration) { |
| if (this.animationId_) |
| this.stop(); |
| this.duration_ = duration; |
| this.scheduleTick_(); |
| } |
| |
| stop() { |
| cancelAnimationFrame(this.animationId_); |
| this.startTime_ = 0; |
| this.duration_ = 0; |
| this.animationId_ = 0; |
| } |
| |
| scheduleTick_() { |
| if (this.animationId_) |
| throw new Error("Tick already scheduled."); |
| this.animationId_ = requestAnimationFrame(this.tick_.bind(this)); |
| } |
| |
| tick_(timeStamp) { |
| this.animationId_ = 0; |
| if (!this.startTime_) |
| this.startTime_ = timeStamp; |
| var elapsedTime = timeStamp - this.startTime_; |
| var t = Math.max(0, Math.min(1, elapsedTime / this.duration_)); |
| if (t < 1) |
| this.scheduleTick_(); |
| else |
| this.stop(); |
| this.delegate_.updateAnimation(t); |
| } |
| }; |
| </script> |