-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.ts
411 lines (357 loc) · 12.8 KB
/
index.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* @license
* Copyright 2017 Adam Miskiewicz
*
* Use of this source code is governed by a MIT-style license that can be found
* in the LICENSE file or at https://opensource.org/licenses/MIT.
*/
import { invariant, withDefault } from "./utils";
export interface SpringConfig {
fromValue: number; // Starting value of the animation.
toValue: number; // Ending value of the animation.
stiffness: number; // The spring stiffness coefficient.
damping: number; // Defines how the spring’s motion should be damped due to the forces of friction.
mass: number; // The mass of the object attached to the end of the spring.
initialVelocity: number; // The initial velocity (in units/ms) of the object attached to the spring.
allowsOverdamping: boolean; // Whether or not the spring allows "overdamping" (a damping ratio > 1). Defaults to false.
overshootClamping: boolean; // False when overshooting is allowed, true when it is not. Defaults to false.
restVelocityThreshold: number; // When spring's velocity is below `restVelocityThreshold`, it is at rest. Defaults to .001.
restDisplacementThreshold: number; // When the spring's displacement (current value) is below `restDisplacementThreshold`, it is at rest. Defaults to .001.
}
export type PartialSpringConfig = Partial<SpringConfig>;
export type SpringListenerFn = (spring: Spring) => void;
export interface SpringListener {
onUpdate?: SpringListenerFn;
onStart?: SpringListenerFn;
onStop?: SpringListenerFn;
}
/**
* Implements a spring physics simulation based on the equations behind
* damped harmonic oscillators (https://en.wikipedia.org/wiki/Harmonic_oscillator#Damped_harmonic_oscillator).
*/
export class Spring {
static MAX_DELTA_TIME_MS = 1 / 60 * 1000 * 4; // advance 4 frames at max
_listeners: SpringListener[] = [];
private _config: SpringConfig;
private _currentAnimationStep: number = 0; // current requestAnimationFrame
private _currentTime: number = 0; // Current timestamp of animation in ms (real time)
private _springTime: number = 0; // Current time along the spring curve in ms (zero-based)
private _currentValue: number = 0; // the current value of the spring
private _currentVelocity: number = 0; // the current velocity of the spring
private _isAnimating: boolean = false;
private _oscillationVelocityPairs = [];
constructor(config: PartialSpringConfig = {}) {
this._config = {
fromValue: withDefault(config.fromValue, 0),
toValue: withDefault(config.toValue, 1),
stiffness: withDefault(config.stiffness, 100),
damping: withDefault(config.damping, 10),
mass: withDefault(config.mass, 1),
initialVelocity: withDefault(config.initialVelocity, 0),
overshootClamping: withDefault(config.overshootClamping, false),
allowsOverdamping: withDefault(config.allowsOverdamping, false),
restVelocityThreshold: withDefault(config.restVelocityThreshold, 0.001),
restDisplacementThreshold: withDefault(
config.restDisplacementThreshold,
0.001
)
};
this._currentValue = this._config.fromValue;
this._currentVelocity = this._config.initialVelocity;
}
/**
* If `fromValue` differs from `toValue`, or `initialVelocity` is non-zero,
* start the simulation and call the `onStart` listeners.
*/
start(): this {
const { fromValue, toValue, initialVelocity } = this._config;
if (fromValue !== toValue || initialVelocity !== 0) {
this._reset();
this._isAnimating = true;
if (!this._currentAnimationStep) {
this._notifyListeners("onStart");
this._currentAnimationStep = requestAnimationFrame((t: number) => {
this._step(Date.now());
});
}
}
return this;
}
/**
* If a simulation is in progress, stop it and call the `onStop` listeners.
*/
stop(): this {
if (!this._isAnimating) {
return this;
}
this._isAnimating = false;
this._notifyListeners("onStop");
if (this._currentAnimationStep) {
cancelAnimationFrame(this._currentAnimationStep);
this._currentAnimationStep = 0;
}
return this;
}
/**
* The spring's current position.
*/
get currentValue(): number {
return this._currentValue;
}
/**
* The spring's current velocity in units / ms.
*/
get currentVelocity(): number {
return this._currentVelocity; // give velocity in units/ms;
}
/**
* If the spring has reached its `toValue`, or if its velocity is below the
* `restVelocityThreshold`, it is considered at rest. If `stop()` is called
* during a simulation, both `isAnimating` and `isAtRest` will be false.
*/
get isAtRest(): boolean {
return this._isSpringAtRest();
}
/**
* Whether or not the spring is currently emitting values.
*
* Note: this is distinct from whether or not it is at rest.
* See also `isAtRest`.
*/
get isAnimating(): boolean {
return this._isAnimating;
}
/**
* Updates the spring config with the given values. Values not explicitly
* supplied will be reused from the existing config.
*/
updateConfig(updatedConfig: PartialSpringConfig): this {
// When we update the spring config, we reset the simulation to ensure the
// spring always moves the full distance between `fromValue` and `toValue`.
// To ensure that the simulation behaves correctly if those values aren't
// being changed in `updatedConfig`, we run the simulation with `_step()`
// and default `fromValue` and `initialVelocity` to their current values.
this._advanceSpringToTime(Date.now());
const baseConfig = {
fromValue: this._currentValue,
initialVelocity: this._currentVelocity
};
this._config = {
...this._config,
...baseConfig,
...updatedConfig
};
this._reset();
return this;
}
/**
* The provided callback will be invoked when the simulation begins.
*/
onStart(listener: SpringListenerFn): this {
this._listeners.push({ onStart: listener });
return this;
}
/**
* The provided callback will be invoked on each frame while the simulation is
* running.
*/
onUpdate(listener: SpringListenerFn): this {
this._listeners.push({ onUpdate: listener });
return this;
}
/**
* The provided callback will be invoked when the simulation ends.
*/
onStop(listener: SpringListenerFn): this {
this._listeners.push({ onStop: listener });
return this;
}
/**
* Remove a single listener from this spring.
*/
removeListener(listenerFn: SpringListenerFn): this {
this._listeners = this._listeners.reduce(
(result, listener) => {
const foundListenerFn =
Object.values(listener).indexOf(listenerFn) !== -1;
if (!foundListenerFn) {
result.push(listener);
}
return result;
},
[] as SpringListener[]
);
return this;
}
/**
* Removes all listeners from this spring.
*/
removeAllListeners(): this {
this._listeners = [];
return this;
}
private _reset() {
this._currentTime = Date.now();
this._springTime = 0.0;
this._currentValue = this._config.fromValue;
this._currentVelocity = this._config.initialVelocity;
}
private _notifyListeners(eventName: keyof SpringListener) {
this._listeners.forEach((listener: Partial<SpringListener>) => {
const maybeListenerFn = listener[eventName];
if (typeof maybeListenerFn === "function") {
maybeListenerFn(this);
}
});
}
/**
* `_step` is the main loop. While the animation is running, it updates the
* current state once per frame, and schedules the next frame if the spring is
* not yet at rest.
*/
private _step(timestamp: number) {
this._advanceSpringToTime(timestamp, true);
// check `_isAnimating`, in case `stop()` got called during
// `_advanceSpringToTime()`
if (this._isAnimating) {
this._currentAnimationStep = requestAnimationFrame((t: number) =>
this._step(Date.now())
);
}
}
private _advanceSpringToTime(
timestamp: number,
shouldNotifyListeners: boolean = false
) {
// `_advanceSpringToTime` updates `_currentTime` and triggers the listeners.
// Because of these side effects, it's only safe to call when an animation
// is already in-progress.
if (!this._isAnimating) {
return;
}
let deltaTime = timestamp - this._currentTime;
// If for some reason we lost a lot of frames (e.g. process large payload or
// stopped in the debugger), we only advance by 4 frames worth of
// computation and will continue on the next frame. It's better to have it
// running at slower speed than jumping to the end.
if (deltaTime > Spring.MAX_DELTA_TIME_MS) {
deltaTime = Spring.MAX_DELTA_TIME_MS;
}
this._springTime += deltaTime;
const c = this._config.damping;
const m = this._config.mass;
const k = this._config.stiffness;
const fromValue = this._config.fromValue;
const toValue = this._config.toValue;
const v0 = -this._config.initialVelocity;
invariant(m > 0, "Mass value must be greater than 0");
invariant(k > 0, "Stiffness value must be greater than 0");
let zeta = c / (2 * Math.sqrt(k * m)); // damping ratio (dimensionless)
const omega0 = Math.sqrt(k / m) / 1000; // undamped angular frequency of the oscillator (rad/ms)
const omega1 = omega0 * Math.sqrt(1.0 - zeta * zeta); // exponential decay
const omega2 = omega0 * Math.sqrt(zeta * zeta - 1.0); // frequency of damped oscillation
const x0 = toValue - fromValue; // initial displacement of the spring at t = 0
if (zeta > 1 && !this._config.allowsOverdamping) {
zeta = 1;
}
let oscillation = 0.0;
let velocity = 0.0;
const t = this._springTime;
if (zeta < 1) {
// Under damped
const envelope = Math.exp(-zeta * omega0 * t);
oscillation =
toValue -
envelope *
((v0 + zeta * omega0 * x0) / omega1 * Math.sin(omega1 * t) +
x0 * Math.cos(omega1 * t));
// This looks crazy -- it's actually just the derivative of the
// oscillation function
velocity =
zeta *
omega0 *
envelope *
(Math.sin(omega1 * t) * (v0 + zeta * omega0 * x0) / omega1 +
x0 * Math.cos(omega1 * t)) -
envelope *
(Math.cos(omega1 * t) * (v0 + zeta * omega0 * x0) -
omega1 * x0 * Math.sin(omega1 * t));
} else if (zeta === 1) {
// Critically damped
const envelope = Math.exp(-omega0 * t);
oscillation = toValue - envelope * (x0 + (v0 + omega0 * x0) * t);
velocity =
envelope * (v0 * (t * omega0 - 1) + t * x0 * (omega0 * omega0));
} else {
// Overdamped
const envelope = Math.exp(-zeta * omega0 * t);
oscillation =
toValue -
envelope *
((v0 + zeta * omega0 * x0) * Math.sinh(omega2 * t) +
omega2 * x0 * Math.cosh(omega2 * t)) /
omega2;
velocity =
envelope *
zeta *
omega0 *
(Math.sinh(omega2 * t) * (v0 + zeta * omega0 * x0) +
x0 * omega2 * Math.cosh(omega2 * t)) /
omega2 -
envelope *
(omega2 * Math.cosh(omega2 * t) * (v0 + zeta * omega0 * x0) +
omega2 * omega2 * x0 * Math.sinh(omega2 * t)) /
omega2;
}
this._currentTime = timestamp;
this._currentValue = oscillation;
this._currentVelocity = velocity;
if (!shouldNotifyListeners) {
return;
}
this._notifyListeners("onUpdate");
if (!this._isAnimating) {
// a listener might have stopped us in _onUpdate
return;
}
// If the Spring is overshooting (when overshoot clamping is on), or if the
// spring is at rest (based on the thresholds set in the config), stop the
// animation.
if (this._isSpringOvershooting() || this._isSpringAtRest()) {
if (k !== 0) {
// Ensure that we end up with a round value
this._currentValue = toValue;
this._currentVelocity = 0;
this._notifyListeners("onUpdate");
}
this.stop();
return;
}
}
private _isSpringOvershooting() {
const { stiffness, fromValue, toValue, overshootClamping } = this._config;
let isOvershooting = false;
if (overshootClamping && stiffness !== 0) {
if (fromValue < toValue) {
isOvershooting = this._currentValue > toValue;
} else {
isOvershooting = this._currentValue < toValue;
}
}
return isOvershooting;
}
private _isSpringAtRest() {
const {
stiffness,
toValue,
restDisplacementThreshold,
restVelocityThreshold
} = this._config;
const isNoVelocity =
Math.abs(this._currentVelocity) <= restVelocityThreshold;
const isNoDisplacement =
stiffness !== 0 &&
Math.abs(toValue - this._currentValue) <= restDisplacementThreshold;
return isNoDisplacement && isNoVelocity;
}
}