|
| 1 | +/* |
| 2 | + * Copyright (C) 2019-2025 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +import {Easing, linear} from './Easings'; |
| 21 | + |
| 22 | +export interface AnimationOptions { |
| 23 | + duration?: number; // Total animation duration in ms |
| 24 | + loop?: boolean; // Whether to loop after finishing |
| 25 | + easing?: Easing; // Easing function |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Abstract base class for animation controllers. |
| 30 | + * |
| 31 | + * Manages timing, easing, looping, and basic control functions such as start, pause, resume, and cancel. |
| 32 | + * This generic controller can be extended to support various animation types |
| 33 | + * (e\.g\., for paths, markers, or camera movements). |
| 34 | + * |
| 35 | + * The actual animation logic should be implemented in the `updateFrame\(t\)` method by subclasses. |
| 36 | + */ |
| 37 | +export class AnimationController { |
| 38 | + protected running = false; |
| 39 | + protected paused = false; |
| 40 | + protected pauseOffset = 0; |
| 41 | + protected startTime = 0; |
| 42 | + protected rafId: number | null = null; |
| 43 | + protected onCompleteCb?: () => void; |
| 44 | + |
| 45 | + protected duration: number; |
| 46 | + protected loop: boolean; |
| 47 | + protected easing: Easing; |
| 48 | + private pauseOffsetStart: DOMHighResTimeStamp; |
| 49 | + |
| 50 | + constructor(options: AnimationOptions = {}) { |
| 51 | + this.duration = options.duration ?? 1000; |
| 52 | + this.loop = options.loop ?? false; |
| 53 | + this.easing = options.easing ?? linear; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Start or restart the animation. |
| 58 | + */ |
| 59 | + start(): void { |
| 60 | + this.running = true; |
| 61 | + this.paused = false; |
| 62 | + this.pauseOffset = 0; |
| 63 | + this.startTime = performance.now(); |
| 64 | + this.rafId = requestAnimationFrame(this.frame.bind(this)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Pause the animation. |
| 69 | + */ |
| 70 | + pause(): void { |
| 71 | + if (!this.running || this.paused) return; |
| 72 | + this.paused = true; |
| 73 | + this.pauseOffsetStart = performance.now(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Resume a paused animation. |
| 78 | + */ |
| 79 | + resume(): void { |
| 80 | + if (!this.running || !this.paused) return; |
| 81 | + this.paused = false; |
| 82 | + if (this.pauseOffsetStart != null) { |
| 83 | + this.pauseOffset += performance.now() - this.pauseOffsetStart; |
| 84 | + this.pauseOffsetStart = undefined; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Cancel the animation. |
| 90 | + */ |
| 91 | + cancel(): void { |
| 92 | + this.running = false; |
| 93 | + if (this.rafId != null) cancelAnimationFrame(this.rafId); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Check if the animation is running. |
| 98 | + */ |
| 99 | + isRunning(): boolean { |
| 100 | + return this.running && !this.paused; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Set a callback to be invoked when the animation completes. |
| 105 | + */ |
| 106 | + onComplete(cb: () => void) { |
| 107 | + this.onCompleteCb = cb; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns a promise that resolves when the animation completes. |
| 112 | + */ |
| 113 | + complete(): Promise<void> { |
| 114 | + return new Promise((resolve) => { |
| 115 | + this.onComplete(() => resolve()); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Internal frame handler. |
| 121 | + * Calls updateFrame(t) which should be implemented by subclasses. |
| 122 | + */ |
| 123 | + protected frame(now: number) { |
| 124 | + if (!this.running) return; |
| 125 | + if (this.paused) { |
| 126 | + this.rafId = requestAnimationFrame(this.frame.bind(this)); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + const elapsed = now - this.startTime - this.pauseOffset; |
| 131 | + let t = Math.max(0, Math.min(1, elapsed / this.duration)); |
| 132 | + t = this.easing(t); |
| 133 | + |
| 134 | + this.updateFrame(t); |
| 135 | + |
| 136 | + if (elapsed >= this.duration) { |
| 137 | + if (this.loop) { |
| 138 | + this.startTime = performance.now(); |
| 139 | + this.pauseOffset = 0; |
| 140 | + this.rafId = requestAnimationFrame(this.frame.bind(this)); |
| 141 | + } else { |
| 142 | + this.running = false; |
| 143 | + this.onCompleteCb?.(); |
| 144 | + } |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + this.rafId = requestAnimationFrame(this.frame.bind(this)); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Override this in subclasses to implement animation logic. |
| 153 | + * @param t normalized time [0..1] after easing |
| 154 | + */ |
| 155 | + protected updateFrame(t: number): void { |
| 156 | + } |
| 157 | +} |
0 commit comments