Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 1 | import {globals} from './globals'; |
| 2 | |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 3 | // Copyright (C) 2018 The Android Open Source Project |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | export class Animation { |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 18 | private startMs = 0; |
| 19 | private endMs = 0; |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 20 | private boundOnAnimationFrame = this.onAnimationFrame.bind(this); |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 21 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 22 | constructor(private onAnimationStep: (timeSinceStartMs: number) => void) {} |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 23 | |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 24 | start(durationMs: number) { |
| 25 | const nowMs = performance.now(); |
| 26 | |
| 27 | // If the animation is already happening, just update its end time. |
| 28 | if (nowMs <= this.endMs) { |
| 29 | this.endMs = nowMs + durationMs; |
| 30 | return; |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 31 | } |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 32 | this.startMs = nowMs; |
| 33 | this.endMs = nowMs + durationMs; |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 34 | globals.rafScheduler.start(this.boundOnAnimationFrame); |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | stop() { |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 38 | this.endMs = 0; |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 39 | globals.rafScheduler.stop(this.boundOnAnimationFrame); |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 42 | get startTimeMs(): number { |
| 43 | return this.startMs; |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 46 | private onAnimationFrame(nowMs: number) { |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 47 | if (nowMs >= this.endMs) { |
| 48 | globals.rafScheduler.stop(this.boundOnAnimationFrame); |
| 49 | return; |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 50 | } |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame^] | 51 | this.onAnimationStep(Math.max(Math.round(nowMs - this.startMs), 0)); |
Michail Schwab | 405002c | 2018-07-26 13:19:10 -0400 | [diff] [blame] | 52 | } |
Primiano Tucci | 3cae4b6 | 2018-08-01 22:40:45 +0100 | [diff] [blame] | 53 | } |