-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
animation-controller.ts
249 lines (218 loc) · 7.58 KB
/
animation-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2022 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import {Component} from 'react';
import {bisectLeft} from 'd3-array';
import {requestAnimationFrame, cancelAnimationFrame} from 'global/window';
import Console from 'global/console';
import {BASE_SPEED, FPS, ANIMATION_WINDOW} from '@kepler.gl/constants';
interface AnimationControllerProps<T extends number | number[]> {
isAnimating?: boolean;
speed?: number;
updateAnimation: (x: T) => void;
animationWindow?: string;
steps?: number[] | null;
domain: number[] | null;
value: T;
baseSpeed?: number;
}
class AnimationControllerType<T extends number | number[]> extends Component<
AnimationControllerProps<T>
> {}
function AnimationControllerFactory(): typeof AnimationControllerType {
/**
* 4 Animation Window Types
* 1. free
* |-> |->
* Current time is a fixed range, animate a moving window that calls next animation frames continuously
* The increment id based on domain / BASE_SPEED * SPEED
*
* 2. incremental
* | |->
* Same as free, current time is a growing range, only the max value of range increment during animation.
* The increment is also based on domain / BASE_SPEED * SPEED
*
* 3. point
* o -> o
* Current time is a point, animate a moving point calls next animation frame continuously
* The increment is based on domain / BASE_SPEED * SPEED
*
* 4. interval
* o ~> o
* Current time is a point. An array of sorted time steps are provided,
* animate a moving point jumps to the next step
*/
class AnimationController<T extends number | number[]> extends Component<
AnimationControllerProps<T>
> {
static defaultProps = {
baseSpeed: BASE_SPEED,
speed: 1,
steps: null,
animationWindow: ANIMATION_WINDOW.free
};
state = {
isAnimating: false
};
componentDidMount() {
this._startOrPauseAnimation();
}
componentDidUpdate() {
this._startOrPauseAnimation();
}
componentWillUnmount() {
if (this._timer) {
cancelAnimationFrame(this._timer);
}
}
_timer = null;
_startTime: number = 0;
_startOrPauseAnimation() {
const {isAnimating, speed = 1} = this.props;
if (!this._timer && isAnimating && speed > 0) {
this._startAnimation();
} else if (this._timer && !isAnimating) {
this._pauseAnimation();
}
}
_animate = delay => {
this._startTime = new Date().getTime();
const loop = () => {
const current = new Date().getTime();
const delta = current - this._startTime;
if (delta >= delay) {
this._nextFrame();
this._startTime = new Date().getTime();
} else {
this._timer = requestAnimationFrame(loop);
}
};
this._timer = requestAnimationFrame(loop);
};
_resetAnimationByDomain = () => {
const {domain, value, animationWindow} = this.props;
if (!domain) {
return;
}
if (Array.isArray(value)) {
if (animationWindow === ANIMATION_WINDOW.incremental) {
this.props.updateAnimation([value[0], value[0] + 1] as T);
} else {
this.props.updateAnimation([domain[0], domain[0] + value[1] - value[0]] as T);
}
} else {
this.props.updateAnimation(domain[0] as T);
}
};
_resetAnimtionByTimeStep = () => {
const {steps = null} = this.props;
if (!steps) return;
// go to the first steps
this.props.updateAnimation([steps[0], 0] as T);
};
_resetAnimation = () => {
if (this.props.animationWindow === ANIMATION_WINDOW.interval) {
this._resetAnimtionByTimeStep();
} else {
this._resetAnimationByDomain();
}
};
_startAnimation = () => {
const {speed = 1} = this.props;
this._clearTimer();
if (speed > 0) {
if (this.props.animationWindow === ANIMATION_WINDOW.interval) {
// animate by interval
// 30*600
const {steps} = this.props;
if (!Array.isArray(steps) || !steps.length) {
Console.warn('animation steps should be an array');
return;
}
// when speed = 1, animation should loop through 600 frames at 60 FPS
// calculate delay based on # steps
const delay = (BASE_SPEED * (1000 / FPS)) / steps.length / (speed || 1);
this._animate(delay);
} else {
this._timer = requestAnimationFrame(this._nextFrame);
}
}
this.setState({isAnimating: true});
};
_clearTimer = () => {
if (this._timer) {
cancelAnimationFrame(this._timer);
this._timer = null;
}
};
_pauseAnimation = () => {
this._clearTimer();
this.setState({isAnimating: false});
};
_nextFrame = () => {
this._timer = null;
const nextValue =
this.props.animationWindow === ANIMATION_WINDOW.interval
? this._nextFrameByTimeStep()
: this._nextFrameByDomain();
this.props.updateAnimation(nextValue as T);
};
_nextFrameByDomain() {
const {domain, value, speed = 1, baseSpeed = 600, animationWindow} = this.props;
if (!domain) {
return;
}
const delta = ((domain[1] - domain[0]) / baseSpeed) * speed;
// loop when reaches the end
// current time is a range
if (Array.isArray(value)) {
let value0: number;
let value1: number;
const readEnd = value[1] + delta > domain[1];
if (animationWindow === ANIMATION_WINDOW.incremental) {
value0 = value[0];
value1 = readEnd ? value[0] + 1 : value[1] + delta;
} else {
value0 = readEnd ? domain[0] : value[0] + delta;
value1 = value0 + value[1] - value[0];
}
return [value0, value1];
}
// current time is a point
return Number(value) + delta > domain[1] ? domain[0] : Number(value) + delta;
}
_nextFrameByTimeStep() {
const {steps = null, value} = this.props;
if (!steps) return;
const val = Array.isArray(value) ? value[0] : Number(value);
const index = bisectLeft(steps, val);
const nextIdx = index >= steps.length - 1 ? 0 : index + 1;
return [steps[nextIdx], nextIdx];
}
render() {
const {isAnimating} = this.state;
const {children} = this.props;
return typeof children === 'function'
? children(isAnimating, this._startAnimation, this._pauseAnimation, this._resetAnimation)
: null;
}
}
return AnimationController;
}
export default AnimationControllerFactory;