-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathtrajectory-player.ts
272 lines (233 loc) · 7.25 KB
/
trajectory-player.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
/**
* @file Trajectory Player
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import { Signal } from 'signals'
import { defaults, createParams, updateParams } from '../utils'
import Trajectory from './trajectory'
export type TrajectoryPlayerInterpolateType = ''|'linear'|'spline'
export type TrajectoryPlayerMode = 'loop'|'once'
export type TrajectoryPlayerDirection = 'forward'|'backward'|'bounce'
const TrajectoryPlayerDefaultParameters = {
step: 1, // how many frames to advance when playing
timeout: 50, // how many milliseconds to wait between playing frames
start: 0, // first frame to play
end: 0, // last frame to play
interpolateType: '' as TrajectoryPlayerInterpolateType,
interpolateStep: 5, // window size used for interpolation
mode: 'loop' as TrajectoryPlayerMode,
direction: 'forward' as TrajectoryPlayerDirection
}
export type TrajectoryPlayerParameters = typeof TrajectoryPlayerDefaultParameters
interface TrajectoryPlayerSignals {
startedRunning: Signal
haltedRunning: Signal
}
/**
* Trajectory player for animating coordinate frames
* @example
* var player = new TrajectoryPlayer(trajectory, {step: 1, timeout: 50});
* player.play();
*/
class TrajectoryPlayer {
signals: TrajectoryPlayerSignals = {
startedRunning: new Signal(),
haltedRunning: new Signal()
}
parameters: TrajectoryPlayerParameters
traj: Trajectory
private _run = false
private _previousTime = 0
private _currentTime = 0
private _currentStep = 1
private _currentFrame: number|[number, number, number, number]
private _direction: TrajectoryPlayerDirection
/**
* make trajectory player
* @param {Trajectory} traj - the trajectory
* @param {TrajectoryPlayerParameters} [params] - parameter object
*/
constructor (traj: Trajectory, params: Partial<TrajectoryPlayerParameters> = {}) {
traj.signals.playerChanged.add((player: TrajectoryPlayer) => {
if (player !== this) {
this.pause()
}
}, this)
const n = defaults(traj.frameCount, 1)
this.traj = traj
this.parameters = createParams(params, TrajectoryPlayerDefaultParameters)
this.parameters.end = Math.min(defaults(params.end, n - 1), n - 1)
this.parameters.step = defaults(params.step, Math.ceil((n + 1) / 100))
this._currentFrame = this.parameters.start
this._direction = this.parameters.direction === 'bounce' ? 'forward' : this.parameters.direction
traj.signals.countChanged.add((n: number) => {
this.parameters.end = Math.min(defaults(this.parameters.end, n - 1), n - 1)
}, this)
this._animate = this._animate.bind(this)
}
get isRunning () { return this._run }
/**
* set player parameters
* @param {TrajectoryPlayerParameters} [params] - parameter object
*/
setParameters (params: Partial<TrajectoryPlayerParameters> = {}) {
updateParams(this.parameters, params)
if (params.direction !== undefined && this.parameters.direction !== 'bounce') {
this._direction = this.parameters.direction
}
}
_animate () {
if (!this._run) return
this._currentTime = window.performance.now()
const dt = this._currentTime - this._previousTime
const step = this.parameters.interpolateType ? this.parameters.interpolateStep : 1
const timeout = this.parameters.timeout / step
const traj = this.traj
if (traj && traj.frameCount && !traj.inProgress && dt >= timeout) {
if (this.parameters.interpolateType) {
if (this._currentStep > this.parameters.interpolateStep) {
this._currentStep = 1
}
if (this._currentStep === 1) {
this._currentFrame = this._nextInterpolated()
}
if (traj.hasFrame(this._currentFrame)) {
this._currentStep += 1
const t = this._currentStep / (this.parameters.interpolateStep + 1)
const [i, ip, ipp, ippp] = this._currentFrame as [number, number, number, number]
traj.setFrameInterpolated(
i, ip, ipp, ippp, t, this.parameters.interpolateType
)
this._previousTime = this._currentTime
} else {
traj.loadFrame(this._currentFrame)
}
} else {
const i = this._next()
if (traj.hasFrame(i)) {
traj.setFrame(i)
this._previousTime = this._currentTime
} else {
traj.loadFrame(i)
}
}
}
window.requestAnimationFrame(this._animate)
}
_next () {
const p = this.parameters
let i
if (this._direction === 'forward') {
i = this.traj.currentFrame + p.step
} else {
i = this.traj.currentFrame - p.step
}
if (i > p.end || i < p.start) {
if (p.direction === 'bounce') {
if (this._direction === 'forward') {
this._direction = 'backward'
} else {
this._direction = 'forward'
}
}
if (p.mode === 'once') {
this.pause()
if (p.direction === 'forward') {
i = p.end
} else if (p.direction === 'backward') {
i = p.start
} else {
if (this._direction === 'forward') {
i = p.start
} else {
i = p.end
}
}
} else {
if (this._direction === 'forward') {
i = p.start
if (p.interpolateType) {
i = Math.min(p.end, i + p.step)
}
} else {
i = p.end
if (p.interpolateType) {
i = Math.max(p.start, i - p.step)
}
}
}
}
return i
}
_nextInterpolated () {
const p = this.parameters
const i = this._next()
let ip, ipp, ippp
if (this._direction === 'forward') {
ip = Math.max(p.start, i - p.step)
ipp = Math.max(p.start, i - 2 * p.step)
ippp = Math.max(p.start, i - 3 * p.step)
} else {
ip = Math.min(p.end, i + p.step)
ipp = Math.min(p.end, i + 2 * p.step)
ippp = Math.min(p.end, i + 3 * p.step)
}
return [i, ip, ipp, ippp] as [number, number, number, number]
}
/**
* toggle between playing and pausing the animation
* @return {undefined}
*/
toggle () {
if (this._run) {
this.pause()
} else {
this.play()
}
}
/**
* start the animation
* @return {undefined}
*/
play () {
if (!this._run) {
if (this.traj.player !== this) {
this.traj.setPlayer(this)
}
this._currentStep = 1
const p = this.parameters
const frame = this.traj.currentFrame
// snap to the grid implied by this.step division and multiplication
// thus minimizing cache misses
let i = Math.ceil(frame / p.step) * p.step
// wrap when restarting from the limit (i.e. end or start)
if (p.direction === 'forward' && frame >= p.end) {
i = p.start
} else if (p.direction === 'backward' && frame <= p.start) {
i = p.end
}
this.traj.setFrame(i)
this._run = true
this._animate()
this.signals.startedRunning.dispatch()
}
}
/**
* pause the animation
* @return {undefined}
*/
pause () {
this._run = false
this.signals.haltedRunning.dispatch()
}
/**
* stop the animation (pause and go to start-frame)
* @return {undefined}
*/
stop () {
this.pause()
this.traj.setFrame(this.parameters.start)
}
}
export default TrajectoryPlayer