Chromium Code Reviews| Index: sky/framework/animation/timer.sky |
| diff --git a/sky/framework/animation/timer.sky b/sky/framework/animation/timer.sky |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7346a94ca4931a068abe3ba050b4b5c27b862791 |
| --- /dev/null |
| +++ b/sky/framework/animation/timer.sky |
| @@ -0,0 +1,49 @@ |
| +<!-- |
| +// 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 Timer { |
|
esprehn
2015/01/29 19:03:57
AnimationTimer, I expected this to do setTimeout f
abarth-chromium
2015/01/29 19:16:45
Good point.
|
| + 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 "Tick already scheduled."; |
|
esprehn
2015/01/29 19:03:57
new Error(...);
throwing a string is bad, you don
abarth-chromium
2015/01/29 19:16:45
kk
|
| + 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> |