coily 0.9.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.mts +107 -3
- package/dist/api.d.ts +107 -3
- package/dist/api.mjs +215 -383
- package/dist/loop.d.mts +1 -1
- package/dist/loop.d.ts +1 -1
- package/package.json +7 -10
- package/dist/api.cjs +0 -630
- package/dist/api.d.cts +0 -6
- package/dist/loop.cjs +0 -16
- package/dist/loop.d.cts +0 -5
- package/dist/shared/coily.0b4474af.d.cts +0 -90
- package/dist/shared/coily.0b4474af.d.mts +0 -90
- package/dist/shared/coily.0b4474af.d.ts +0 -90
package/dist/api.mjs
CHANGED
|
@@ -1,82 +1,49 @@
|
|
|
1
|
-
var __accessCheck$5 = (obj, member, msg) => {
|
|
2
|
-
if (!member.has(obj))
|
|
3
|
-
throw TypeError("Cannot " + msg);
|
|
4
|
-
};
|
|
5
|
-
var __privateGet$5 = (obj, member, getter) => {
|
|
6
|
-
__accessCheck$5(obj, member, "read from private field");
|
|
7
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
-
};
|
|
9
|
-
var __privateAdd$5 = (obj, member, value) => {
|
|
10
|
-
if (member.has(obj))
|
|
11
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
-
};
|
|
14
|
-
var _solvers;
|
|
15
1
|
class Scheduler {
|
|
16
|
-
|
|
17
|
-
__privateAdd$5(this, _solvers, /* @__PURE__ */ new Set());
|
|
18
|
-
}
|
|
2
|
+
#solvers = /* @__PURE__ */ new Set();
|
|
19
3
|
tick(delta) {
|
|
20
|
-
for (const solver of
|
|
4
|
+
for (const solver of this.#solvers.values()) {
|
|
21
5
|
solver.tick(delta);
|
|
22
6
|
if (solver.resting) {
|
|
23
|
-
|
|
7
|
+
this.#solvers.delete(solver);
|
|
24
8
|
}
|
|
25
9
|
}
|
|
26
10
|
}
|
|
27
11
|
add(solver) {
|
|
28
|
-
|
|
12
|
+
this.#solvers.add(solver);
|
|
29
13
|
}
|
|
30
14
|
remove(solver) {
|
|
31
|
-
|
|
15
|
+
this.#solvers.delete(solver);
|
|
32
16
|
}
|
|
33
17
|
has(solver) {
|
|
34
|
-
return
|
|
18
|
+
return this.#solvers.has(solver);
|
|
35
19
|
}
|
|
36
20
|
}
|
|
37
|
-
_solvers = new WeakMap();
|
|
38
21
|
|
|
39
|
-
var __accessCheck$4 = (obj, member, msg) => {
|
|
40
|
-
if (!member.has(obj))
|
|
41
|
-
throw TypeError("Cannot " + msg);
|
|
42
|
-
};
|
|
43
|
-
var __privateGet$4 = (obj, member, getter) => {
|
|
44
|
-
__accessCheck$4(obj, member, "read from private field");
|
|
45
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
46
|
-
};
|
|
47
|
-
var __privateAdd$4 = (obj, member, value) => {
|
|
48
|
-
if (member.has(obj))
|
|
49
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
50
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
51
|
-
};
|
|
52
|
-
var _handlers;
|
|
53
22
|
class Emitter {
|
|
54
|
-
|
|
55
|
-
__privateAdd$4(this, _handlers, /* @__PURE__ */ new Map());
|
|
56
|
-
}
|
|
23
|
+
#handlers = /* @__PURE__ */ new Map();
|
|
57
24
|
on(type, handler) {
|
|
58
|
-
const handlers =
|
|
25
|
+
const handlers = this.#handlers.get(type);
|
|
59
26
|
if (handlers) {
|
|
60
27
|
handlers.push(handler);
|
|
61
28
|
} else {
|
|
62
|
-
|
|
29
|
+
this.#handlers.set(type, [handler]);
|
|
63
30
|
}
|
|
64
31
|
return () => {
|
|
65
32
|
this.off(type, handler);
|
|
66
33
|
};
|
|
67
34
|
}
|
|
68
35
|
off(type, handler) {
|
|
69
|
-
const handlers =
|
|
36
|
+
const handlers = this.#handlers.get(type);
|
|
70
37
|
if (handlers) {
|
|
71
38
|
if (handler) {
|
|
72
39
|
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
|
73
40
|
} else {
|
|
74
|
-
|
|
41
|
+
this.#handlers.set(type, []);
|
|
75
42
|
}
|
|
76
43
|
}
|
|
77
44
|
}
|
|
78
45
|
emit(type, evt) {
|
|
79
|
-
const handlers =
|
|
46
|
+
const handlers = this.#handlers?.get(type);
|
|
80
47
|
if (handlers) {
|
|
81
48
|
handlers.slice().map((handler) => {
|
|
82
49
|
handler(evt);
|
|
@@ -84,7 +51,6 @@ class Emitter {
|
|
|
84
51
|
}
|
|
85
52
|
}
|
|
86
53
|
}
|
|
87
|
-
_handlers = new WeakMap();
|
|
88
54
|
|
|
89
55
|
function invariant(condition, message) {
|
|
90
56
|
if (condition) {
|
|
@@ -97,367 +63,264 @@ function roundTo(value, precision) {
|
|
|
97
63
|
return Math.round(value * p) / p;
|
|
98
64
|
}
|
|
99
65
|
|
|
100
|
-
var __accessCheck$3 = (obj, member, msg) => {
|
|
101
|
-
if (!member.has(obj))
|
|
102
|
-
throw TypeError("Cannot " + msg);
|
|
103
|
-
};
|
|
104
|
-
var __privateGet$3 = (obj, member, getter) => {
|
|
105
|
-
__accessCheck$3(obj, member, "read from private field");
|
|
106
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
107
|
-
};
|
|
108
|
-
var __privateAdd$3 = (obj, member, value) => {
|
|
109
|
-
if (member.has(obj))
|
|
110
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
111
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
112
|
-
};
|
|
113
|
-
var __privateSet$3 = (obj, member, value, setter) => {
|
|
114
|
-
__accessCheck$3(obj, member, "write to private field");
|
|
115
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
116
|
-
return value;
|
|
117
|
-
};
|
|
118
|
-
var _precision, _restingMagnitude, _position, _velocity;
|
|
119
66
|
class State {
|
|
67
|
+
#precision;
|
|
68
|
+
#restingMagnitude;
|
|
69
|
+
#position;
|
|
70
|
+
#velocity;
|
|
120
71
|
constructor(position, velocity, precision) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
__privateSet$3(this, _precision, precision);
|
|
126
|
-
__privateSet$3(this, _restingMagnitude, 1 / 10 ** __privateGet$3(this, _precision));
|
|
127
|
-
__privateSet$3(this, _position, roundTo(position, __privateGet$3(this, _precision)));
|
|
128
|
-
__privateSet$3(this, _velocity, roundTo(velocity, __privateGet$3(this, _precision)));
|
|
72
|
+
this.#precision = precision;
|
|
73
|
+
this.#restingMagnitude = 1 / 10 ** this.#precision;
|
|
74
|
+
this.#position = roundTo(position, this.#precision);
|
|
75
|
+
this.#velocity = roundTo(velocity, this.#precision);
|
|
129
76
|
}
|
|
130
77
|
get position() {
|
|
131
|
-
return
|
|
78
|
+
return this.#position;
|
|
132
79
|
}
|
|
133
80
|
set position(value) {
|
|
134
|
-
|
|
81
|
+
this.#position = roundTo(value, this.#precision);
|
|
135
82
|
}
|
|
136
83
|
get velocity() {
|
|
137
|
-
return
|
|
84
|
+
return this.#velocity;
|
|
138
85
|
}
|
|
139
86
|
set velocity(value) {
|
|
140
|
-
|
|
87
|
+
this.#velocity = roundTo(value, this.#precision);
|
|
141
88
|
}
|
|
142
89
|
get precision() {
|
|
143
|
-
return
|
|
90
|
+
return this.#precision;
|
|
144
91
|
}
|
|
145
92
|
set precision(value) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
93
|
+
this.#precision = value;
|
|
94
|
+
this.#restingMagnitude = 1 / 10 ** this.#precision;
|
|
95
|
+
this.#position = roundTo(this.#position, this.#precision);
|
|
96
|
+
this.#velocity = roundTo(this.#velocity, this.#precision);
|
|
150
97
|
}
|
|
151
98
|
get resting() {
|
|
152
|
-
return Math.abs(
|
|
99
|
+
return Math.abs(this.#velocity) < this.#restingMagnitude && Math.abs(this.#position) < this.#restingMagnitude;
|
|
153
100
|
}
|
|
154
101
|
}
|
|
155
|
-
_precision = new WeakMap();
|
|
156
|
-
_restingMagnitude = new WeakMap();
|
|
157
|
-
_position = new WeakMap();
|
|
158
|
-
_velocity = new WeakMap();
|
|
159
102
|
|
|
160
|
-
var __accessCheck$2 = (obj, member, msg) => {
|
|
161
|
-
if (!member.has(obj))
|
|
162
|
-
throw TypeError("Cannot " + msg);
|
|
163
|
-
};
|
|
164
|
-
var __privateGet$2 = (obj, member, getter) => {
|
|
165
|
-
__accessCheck$2(obj, member, "read from private field");
|
|
166
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
167
|
-
};
|
|
168
|
-
var __privateAdd$2 = (obj, member, value) => {
|
|
169
|
-
if (member.has(obj))
|
|
170
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
171
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
172
|
-
};
|
|
173
|
-
var __privateSet$2 = (obj, member, value, setter) => {
|
|
174
|
-
__accessCheck$2(obj, member, "write to private field");
|
|
175
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
176
|
-
return value;
|
|
177
|
-
};
|
|
178
|
-
var __privateMethod = (obj, member, method) => {
|
|
179
|
-
__accessCheck$2(obj, member, "access private method");
|
|
180
|
-
return method;
|
|
181
|
-
};
|
|
182
|
-
var _mass, _tension, _damping, _state, _lazyPosition, _underdampedSolver, _criticallyDampedSolver, _overdampedSolver, _currentSolver, _needsUpdate, _needsReset, _emitter, _updateSolver, updateSolver_fn, _solver$1, _state2, _dampedFrequency, _t, _c1, _c2, _solver2, _state3, _t2, _c12, _c22, _solver3, _state4, _dampedFrequency2, _t3, _c13, _c23;
|
|
183
103
|
class Solver {
|
|
104
|
+
#mass;
|
|
105
|
+
#tension;
|
|
106
|
+
#damping;
|
|
107
|
+
#state;
|
|
108
|
+
#underdampedSolver = null;
|
|
109
|
+
#criticallyDampedSolver = null;
|
|
110
|
+
#overdampedSolver = null;
|
|
111
|
+
#currentSolver;
|
|
112
|
+
#needsUpdate = false;
|
|
113
|
+
#needsReset = false;
|
|
114
|
+
#emitter;
|
|
184
115
|
constructor(options) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
__privateAdd$2(this, _state, void 0);
|
|
190
|
-
__privateAdd$2(this, _lazyPosition, void 0);
|
|
191
|
-
__privateAdd$2(this, _underdampedSolver, null);
|
|
192
|
-
__privateAdd$2(this, _criticallyDampedSolver, null);
|
|
193
|
-
__privateAdd$2(this, _overdampedSolver, null);
|
|
194
|
-
__privateAdd$2(this, _currentSolver, void 0);
|
|
195
|
-
__privateAdd$2(this, _needsUpdate, false);
|
|
196
|
-
__privateAdd$2(this, _needsReset, false);
|
|
197
|
-
__privateAdd$2(this, _emitter, void 0);
|
|
198
|
-
__privateSet$2(this, _mass, options.mass);
|
|
199
|
-
__privateSet$2(this, _tension, options.tension);
|
|
200
|
-
__privateSet$2(this, _damping, options.damping);
|
|
201
|
-
__privateSet$2(this, _state, new State(
|
|
116
|
+
this.#mass = options.mass;
|
|
117
|
+
this.#tension = options.tension;
|
|
118
|
+
this.#damping = options.damping;
|
|
119
|
+
this.#state = new State(
|
|
202
120
|
options.position,
|
|
203
121
|
options.velocity,
|
|
204
122
|
options.precision
|
|
205
|
-
)
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
__privateMethod(this, _updateSolver, updateSolver_fn).call(this);
|
|
123
|
+
);
|
|
124
|
+
this.#emitter = new Emitter();
|
|
125
|
+
this.#updateSolver();
|
|
209
126
|
}
|
|
210
127
|
get mass() {
|
|
211
|
-
return
|
|
128
|
+
return this.#mass;
|
|
212
129
|
}
|
|
213
130
|
set mass(value) {
|
|
214
|
-
|
|
215
|
-
|
|
131
|
+
this.#mass = value;
|
|
132
|
+
this.#needsUpdate = true;
|
|
216
133
|
}
|
|
217
134
|
get tension() {
|
|
218
|
-
return
|
|
135
|
+
return this.#tension;
|
|
219
136
|
}
|
|
220
137
|
set tension(value) {
|
|
221
|
-
|
|
222
|
-
|
|
138
|
+
this.#tension = value;
|
|
139
|
+
this.#needsUpdate = true;
|
|
223
140
|
}
|
|
224
141
|
get damping() {
|
|
225
|
-
return
|
|
142
|
+
return this.#damping;
|
|
226
143
|
}
|
|
227
144
|
set damping(value) {
|
|
228
|
-
|
|
229
|
-
|
|
145
|
+
this.#damping = value;
|
|
146
|
+
this.#needsUpdate = true;
|
|
230
147
|
}
|
|
231
148
|
get position() {
|
|
232
|
-
return
|
|
149
|
+
return this.#state.position;
|
|
233
150
|
}
|
|
234
151
|
set position(value) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (!
|
|
238
|
-
|
|
152
|
+
this.#state.position = value;
|
|
153
|
+
this.#needsReset = true;
|
|
154
|
+
if (!this.#state.resting) {
|
|
155
|
+
this.#emitter.emit("start");
|
|
239
156
|
}
|
|
240
157
|
}
|
|
241
|
-
get lazyPosition() {
|
|
242
|
-
return __privateGet$2(this, _lazyPosition);
|
|
243
|
-
}
|
|
244
158
|
get velocity() {
|
|
245
|
-
return
|
|
159
|
+
return this.#state.velocity;
|
|
246
160
|
}
|
|
247
161
|
set velocity(value) {
|
|
248
|
-
|
|
249
|
-
|
|
162
|
+
this.#state.velocity = value;
|
|
163
|
+
this.#needsReset = true;
|
|
250
164
|
}
|
|
251
165
|
get precision() {
|
|
252
|
-
return
|
|
166
|
+
return this.#state.precision;
|
|
253
167
|
}
|
|
254
168
|
set precision(value) {
|
|
255
|
-
|
|
169
|
+
this.#state.precision = value;
|
|
256
170
|
}
|
|
257
171
|
get resting() {
|
|
258
|
-
return
|
|
172
|
+
return this.#state.resting;
|
|
259
173
|
}
|
|
260
174
|
get naturalFrequency() {
|
|
261
|
-
return Math.sqrt(
|
|
175
|
+
return Math.sqrt(this.#tension / this.#mass);
|
|
262
176
|
}
|
|
263
177
|
get criticalDamping() {
|
|
264
|
-
return 2 *
|
|
178
|
+
return 2 * this.#mass * this.naturalFrequency;
|
|
265
179
|
}
|
|
266
180
|
get dampingRatio() {
|
|
267
|
-
return
|
|
181
|
+
return this.#damping / this.criticalDamping;
|
|
268
182
|
}
|
|
269
183
|
tick(dt, emit = true) {
|
|
270
|
-
if (
|
|
271
|
-
|
|
272
|
-
|
|
184
|
+
if (this.#needsUpdate) {
|
|
185
|
+
this.#updateSolver();
|
|
186
|
+
this.#needsUpdate = false;
|
|
273
187
|
}
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
|
|
188
|
+
if (this.#needsReset) {
|
|
189
|
+
this.#currentSolver.reset();
|
|
190
|
+
this.#needsReset = false;
|
|
277
191
|
}
|
|
278
|
-
|
|
279
|
-
__privateSet$2(this, _lazyPosition, __privateGet$2(this, _state).position);
|
|
192
|
+
this.#currentSolver.tick(dt);
|
|
280
193
|
if (emit) {
|
|
281
|
-
|
|
282
|
-
if (
|
|
283
|
-
|
|
194
|
+
this.#emitter.emit("update");
|
|
195
|
+
if (this.#state.resting) {
|
|
196
|
+
this.#emitter.emit("stop");
|
|
284
197
|
}
|
|
285
198
|
}
|
|
286
199
|
}
|
|
287
200
|
onUpdate(callback) {
|
|
288
|
-
return
|
|
201
|
+
return this.#emitter.on("update", callback);
|
|
289
202
|
}
|
|
290
203
|
onStart(callback) {
|
|
291
|
-
return
|
|
204
|
+
return this.#emitter.on("start", callback);
|
|
292
205
|
}
|
|
293
206
|
onStop(callback) {
|
|
294
|
-
return
|
|
207
|
+
return this.#emitter.on("stop", callback);
|
|
208
|
+
}
|
|
209
|
+
#updateSolver() {
|
|
210
|
+
if (this.dampingRatio < 1) {
|
|
211
|
+
this.#underdampedSolver ||= new UnderdampedSolver(this, this.#state);
|
|
212
|
+
this.#currentSolver = this.#underdampedSolver;
|
|
213
|
+
} else if (this.dampingRatio === 1) {
|
|
214
|
+
this.#criticallyDampedSolver ||= new CriticallyDampedSolver(
|
|
215
|
+
this,
|
|
216
|
+
this.#state
|
|
217
|
+
);
|
|
218
|
+
this.#currentSolver = this.#criticallyDampedSolver;
|
|
219
|
+
} else {
|
|
220
|
+
this.#overdampedSolver ||= new OverdampedSolver(this, this.#state);
|
|
221
|
+
this.#currentSolver = this.#overdampedSolver;
|
|
222
|
+
}
|
|
295
223
|
}
|
|
296
224
|
}
|
|
297
|
-
_mass = new WeakMap();
|
|
298
|
-
_tension = new WeakMap();
|
|
299
|
-
_damping = new WeakMap();
|
|
300
|
-
_state = new WeakMap();
|
|
301
|
-
_lazyPosition = new WeakMap();
|
|
302
|
-
_underdampedSolver = new WeakMap();
|
|
303
|
-
_criticallyDampedSolver = new WeakMap();
|
|
304
|
-
_overdampedSolver = new WeakMap();
|
|
305
|
-
_currentSolver = new WeakMap();
|
|
306
|
-
_needsUpdate = new WeakMap();
|
|
307
|
-
_needsReset = new WeakMap();
|
|
308
|
-
_emitter = new WeakMap();
|
|
309
|
-
_updateSolver = new WeakSet();
|
|
310
|
-
updateSolver_fn = function() {
|
|
311
|
-
if (this.dampingRatio < 1) {
|
|
312
|
-
__privateGet$2(this, _underdampedSolver) || __privateSet$2(this, _underdampedSolver, new UnderdampedSolver(this, __privateGet$2(this, _state)));
|
|
313
|
-
__privateSet$2(this, _currentSolver, __privateGet$2(this, _underdampedSolver));
|
|
314
|
-
} else if (this.dampingRatio === 1) {
|
|
315
|
-
__privateGet$2(this, _criticallyDampedSolver) || __privateSet$2(this, _criticallyDampedSolver, new CriticallyDampedSolver(
|
|
316
|
-
this,
|
|
317
|
-
__privateGet$2(this, _state)
|
|
318
|
-
));
|
|
319
|
-
__privateSet$2(this, _currentSolver, __privateGet$2(this, _criticallyDampedSolver));
|
|
320
|
-
} else {
|
|
321
|
-
__privateGet$2(this, _overdampedSolver) || __privateSet$2(this, _overdampedSolver, new OverdampedSolver(this, __privateGet$2(this, _state)));
|
|
322
|
-
__privateSet$2(this, _currentSolver, __privateGet$2(this, _overdampedSolver));
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
225
|
class UnderdampedSolver {
|
|
226
|
+
#solver;
|
|
227
|
+
#state;
|
|
228
|
+
#dampedFrequency;
|
|
229
|
+
#t = 0;
|
|
230
|
+
#c1 = 0;
|
|
231
|
+
#c2 = 0;
|
|
326
232
|
constructor(solver, state) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
__privateAdd$2(this, _dampedFrequency, void 0);
|
|
330
|
-
__privateAdd$2(this, _t, 0);
|
|
331
|
-
__privateAdd$2(this, _c1, 0);
|
|
332
|
-
__privateAdd$2(this, _c2, 0);
|
|
333
|
-
__privateSet$2(this, _solver$1, solver);
|
|
334
|
-
__privateSet$2(this, _state2, state);
|
|
233
|
+
this.#solver = solver;
|
|
234
|
+
this.#state = state;
|
|
335
235
|
this.reset();
|
|
336
236
|
}
|
|
337
237
|
reset() {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
238
|
+
this.#dampedFrequency = this.#solver.naturalFrequency * Math.sqrt(1 - this.#solver.dampingRatio ** 2);
|
|
239
|
+
this.#t = 0;
|
|
240
|
+
this.#c1 = this.#state.position;
|
|
241
|
+
this.#c2 = (this.#state.velocity + this.#solver.dampingRatio * this.#solver.naturalFrequency * this.#state.position) / this.#dampedFrequency;
|
|
342
242
|
this.tick(0);
|
|
343
243
|
}
|
|
344
244
|
tick(dt) {
|
|
345
|
-
|
|
346
|
-
const sin = Math.sin(
|
|
347
|
-
const cos = Math.cos(
|
|
245
|
+
this.#t += dt;
|
|
246
|
+
const sin = Math.sin(this.#dampedFrequency * this.#t);
|
|
247
|
+
const cos = Math.cos(this.#dampedFrequency * this.#t);
|
|
348
248
|
const decay = Math.exp(
|
|
349
|
-
-
|
|
249
|
+
-this.#solver.dampingRatio * this.#solver.naturalFrequency * this.#t
|
|
350
250
|
);
|
|
351
|
-
const decayVelocity = -
|
|
352
|
-
const oscillation =
|
|
353
|
-
const oscillationVelocity = -
|
|
354
|
-
|
|
355
|
-
|
|
251
|
+
const decayVelocity = -this.#solver.dampingRatio * this.#solver.naturalFrequency * decay;
|
|
252
|
+
const oscillation = this.#c1 * cos + this.#c2 * sin;
|
|
253
|
+
const oscillationVelocity = -this.#c1 * this.#dampedFrequency * sin + this.#c2 * this.#dampedFrequency * cos;
|
|
254
|
+
this.#state.position = decay * oscillation;
|
|
255
|
+
this.#state.velocity = decay * oscillationVelocity + decayVelocity * oscillation;
|
|
356
256
|
}
|
|
357
257
|
}
|
|
358
|
-
_solver$1 = new WeakMap();
|
|
359
|
-
_state2 = new WeakMap();
|
|
360
|
-
_dampedFrequency = new WeakMap();
|
|
361
|
-
_t = new WeakMap();
|
|
362
|
-
_c1 = new WeakMap();
|
|
363
|
-
_c2 = new WeakMap();
|
|
364
258
|
class CriticallyDampedSolver {
|
|
259
|
+
#solver;
|
|
260
|
+
#state;
|
|
261
|
+
#t = 0;
|
|
262
|
+
#c1 = 0;
|
|
263
|
+
#c2 = 0;
|
|
365
264
|
constructor(solver, state) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
__privateAdd$2(this, _t2, 0);
|
|
369
|
-
__privateAdd$2(this, _c12, 0);
|
|
370
|
-
__privateAdd$2(this, _c22, 0);
|
|
371
|
-
__privateSet$2(this, _solver2, solver);
|
|
372
|
-
__privateSet$2(this, _state3, state);
|
|
265
|
+
this.#solver = solver;
|
|
266
|
+
this.#state = state;
|
|
373
267
|
this.reset();
|
|
374
268
|
}
|
|
375
269
|
reset() {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
270
|
+
this.#t = 0;
|
|
271
|
+
this.#c1 = this.#state.position;
|
|
272
|
+
this.#c2 = this.#state.velocity + this.#solver.naturalFrequency * this.#state.position;
|
|
379
273
|
this.tick(0);
|
|
380
274
|
}
|
|
381
275
|
tick(dt) {
|
|
382
|
-
|
|
383
|
-
const decay = Math.exp(-
|
|
384
|
-
const decayVelocity = -
|
|
385
|
-
const scale =
|
|
386
|
-
const scaleVelocity =
|
|
387
|
-
|
|
388
|
-
|
|
276
|
+
this.#t += dt;
|
|
277
|
+
const decay = Math.exp(-this.#solver.naturalFrequency * this.#t);
|
|
278
|
+
const decayVelocity = -this.#solver.naturalFrequency * decay;
|
|
279
|
+
const scale = this.#c1 + this.#c2 * this.#t;
|
|
280
|
+
const scaleVelocity = this.#c2;
|
|
281
|
+
this.#state.position = scale * decay;
|
|
282
|
+
this.#state.velocity = scaleVelocity * decay + decayVelocity * scale;
|
|
389
283
|
}
|
|
390
284
|
}
|
|
391
|
-
_solver2 = new WeakMap();
|
|
392
|
-
_state3 = new WeakMap();
|
|
393
|
-
_t2 = new WeakMap();
|
|
394
|
-
_c12 = new WeakMap();
|
|
395
|
-
_c22 = new WeakMap();
|
|
396
285
|
class OverdampedSolver {
|
|
286
|
+
#solver;
|
|
287
|
+
#state;
|
|
288
|
+
#dampedFrequency;
|
|
289
|
+
#t = 0;
|
|
290
|
+
#c1 = 0;
|
|
291
|
+
#c2 = 0;
|
|
397
292
|
constructor(solver, state) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
__privateAdd$2(this, _dampedFrequency2, void 0);
|
|
401
|
-
__privateAdd$2(this, _t3, 0);
|
|
402
|
-
__privateAdd$2(this, _c13, 0);
|
|
403
|
-
__privateAdd$2(this, _c23, 0);
|
|
404
|
-
__privateSet$2(this, _solver3, solver);
|
|
405
|
-
__privateSet$2(this, _state4, state);
|
|
293
|
+
this.#solver = solver;
|
|
294
|
+
this.#state = state;
|
|
406
295
|
this.reset();
|
|
407
296
|
}
|
|
408
297
|
reset() {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
298
|
+
this.#dampedFrequency = this.#solver.naturalFrequency * Math.sqrt(this.#solver.dampingRatio ** 2 - 1);
|
|
299
|
+
this.#t = 0;
|
|
300
|
+
this.#c1 = this.#state.velocity + this.#solver.dampingRatio * this.#solver.naturalFrequency * this.#state.position;
|
|
301
|
+
this.#c2 = this.#state.position * this.#dampedFrequency;
|
|
413
302
|
}
|
|
414
303
|
tick(dt) {
|
|
415
|
-
|
|
304
|
+
this.#t += dt;
|
|
416
305
|
const decay = Math.exp(
|
|
417
|
-
-
|
|
306
|
+
-this.#solver.dampingRatio * this.#solver.naturalFrequency * this.#t
|
|
418
307
|
);
|
|
419
|
-
const decayVelocity = -
|
|
420
|
-
const clamped = Math.min(
|
|
308
|
+
const decayVelocity = -this.#solver.dampingRatio * this.#solver.naturalFrequency * decay;
|
|
309
|
+
const clamped = Math.min(this.#dampedFrequency * this.#t, 300);
|
|
421
310
|
const sinh = Math.sinh(clamped);
|
|
422
311
|
const cosh = Math.cosh(clamped);
|
|
423
|
-
const scale =
|
|
424
|
-
const scaleVelocity = -
|
|
425
|
-
|
|
426
|
-
|
|
312
|
+
const scale = this.#c1 * sinh + this.#c2 * cosh;
|
|
313
|
+
const scaleVelocity = -this.#c1 * this.#dampedFrequency * sinh + this.#c2 * this.#dampedFrequency * cosh;
|
|
314
|
+
this.#state.position = scale * decay / this.#dampedFrequency;
|
|
315
|
+
this.#state.velocity = (scale * decayVelocity + scaleVelocity * decay) / this.#dampedFrequency;
|
|
427
316
|
}
|
|
428
317
|
}
|
|
429
|
-
_solver3 = new WeakMap();
|
|
430
|
-
_state4 = new WeakMap();
|
|
431
|
-
_dampedFrequency2 = new WeakMap();
|
|
432
|
-
_t3 = new WeakMap();
|
|
433
|
-
_c13 = new WeakMap();
|
|
434
|
-
_c23 = new WeakMap();
|
|
435
318
|
|
|
436
|
-
var __accessCheck$1 = (obj, member, msg) => {
|
|
437
|
-
if (!member.has(obj))
|
|
438
|
-
throw TypeError("Cannot " + msg);
|
|
439
|
-
};
|
|
440
|
-
var __privateGet$1 = (obj, member, getter) => {
|
|
441
|
-
__accessCheck$1(obj, member, "read from private field");
|
|
442
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
443
|
-
};
|
|
444
|
-
var __privateAdd$1 = (obj, member, value) => {
|
|
445
|
-
if (member.has(obj))
|
|
446
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
447
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
448
|
-
};
|
|
449
|
-
var __privateSet$1 = (obj, member, value, setter) => {
|
|
450
|
-
__accessCheck$1(obj, member, "write to private field");
|
|
451
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
452
|
-
return value;
|
|
453
|
-
};
|
|
454
|
-
var _target, _solver, _scheduler$1;
|
|
455
319
|
class Spring {
|
|
456
|
-
|
|
320
|
+
#target;
|
|
321
|
+
#solver;
|
|
322
|
+
#scheduler;
|
|
457
323
|
constructor(scheduler, options) {
|
|
458
|
-
__privateAdd$1(this, _target, void 0);
|
|
459
|
-
__privateAdd$1(this, _solver, void 0);
|
|
460
|
-
__privateAdd$1(this, _scheduler$1, void 0);
|
|
461
324
|
invariant(options.mass > 0, "Mass must be greater than 0");
|
|
462
325
|
invariant(options.tension > 0, "Tension must be greater than 0");
|
|
463
326
|
invariant(
|
|
@@ -468,161 +331,130 @@ class Spring {
|
|
|
468
331
|
options.precision === void 0 || options.precision > 0,
|
|
469
332
|
"Precision must be greater than 0"
|
|
470
333
|
);
|
|
471
|
-
|
|
472
|
-
|
|
334
|
+
this.#target = options.target ?? options.value ?? 0;
|
|
335
|
+
this.#solver = new Solver({
|
|
473
336
|
mass: options.mass,
|
|
474
337
|
tension: options.tension,
|
|
475
338
|
damping: options.damping,
|
|
476
|
-
position:
|
|
339
|
+
position: (options.value ?? this.#target) - this.#target,
|
|
477
340
|
velocity: 0,
|
|
478
341
|
precision: options.precision ?? 2
|
|
479
|
-
})
|
|
480
|
-
|
|
481
|
-
if (!
|
|
482
|
-
|
|
342
|
+
});
|
|
343
|
+
this.#scheduler = scheduler;
|
|
344
|
+
if (!this.#solver.resting) {
|
|
345
|
+
this.#scheduler.add(this.#solver);
|
|
483
346
|
}
|
|
484
347
|
}
|
|
485
348
|
get target() {
|
|
486
|
-
return
|
|
349
|
+
return this.#target;
|
|
487
350
|
}
|
|
488
351
|
set target(value) {
|
|
489
|
-
if (value !==
|
|
490
|
-
if (
|
|
491
|
-
|
|
492
|
-
}
|
|
493
|
-
if (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
|
|
494
|
-
__privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
|
|
352
|
+
if (value !== this.#target) {
|
|
353
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
354
|
+
this.#scheduler.add(this.#solver);
|
|
495
355
|
}
|
|
496
356
|
const currentValue = this.value;
|
|
497
|
-
|
|
498
|
-
|
|
357
|
+
this.#target = value;
|
|
358
|
+
this.#solver.position = currentValue - this.#target;
|
|
359
|
+
this.#solver.tick(0);
|
|
499
360
|
}
|
|
500
361
|
}
|
|
501
362
|
get value() {
|
|
502
|
-
return
|
|
363
|
+
return this.#target + this.#solver.position;
|
|
503
364
|
}
|
|
504
365
|
set value(value) {
|
|
505
|
-
const position = value -
|
|
506
|
-
if (position !==
|
|
507
|
-
if (
|
|
508
|
-
|
|
366
|
+
const position = value - this.#target;
|
|
367
|
+
if (position !== this.#solver.position) {
|
|
368
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
369
|
+
this.#scheduler.add(this.#solver);
|
|
509
370
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
}
|
|
513
|
-
__privateGet$1(this, _solver).position = position;
|
|
371
|
+
this.#solver.position = position;
|
|
372
|
+
this.#solver.tick(0);
|
|
514
373
|
}
|
|
515
374
|
}
|
|
516
375
|
get velocity() {
|
|
517
|
-
return
|
|
376
|
+
return this.#solver.velocity;
|
|
518
377
|
}
|
|
519
378
|
set velocity(value) {
|
|
520
|
-
if (!
|
|
521
|
-
|
|
379
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
380
|
+
this.#scheduler.add(this.#solver);
|
|
522
381
|
}
|
|
523
|
-
|
|
382
|
+
this.#solver.velocity = value;
|
|
524
383
|
}
|
|
525
384
|
get mass() {
|
|
526
|
-
return
|
|
385
|
+
return this.#solver.mass;
|
|
527
386
|
}
|
|
528
387
|
set mass(value) {
|
|
529
388
|
invariant(value > 0, "Mass must be greater than 0");
|
|
530
|
-
if (!
|
|
531
|
-
|
|
389
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
390
|
+
this.#scheduler.add(this.#solver);
|
|
532
391
|
}
|
|
533
|
-
|
|
392
|
+
this.#solver.mass = value;
|
|
534
393
|
}
|
|
535
394
|
get tension() {
|
|
536
|
-
return
|
|
395
|
+
return this.#solver.tension;
|
|
537
396
|
}
|
|
538
397
|
set tension(value) {
|
|
539
398
|
invariant(value > 0, "Tension must be greater than 0");
|
|
540
|
-
if (!
|
|
541
|
-
|
|
399
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
400
|
+
this.#scheduler.add(this.#solver);
|
|
542
401
|
}
|
|
543
|
-
|
|
402
|
+
this.#solver.tension = value;
|
|
544
403
|
}
|
|
545
404
|
get damping() {
|
|
546
|
-
return
|
|
405
|
+
return this.#solver.damping;
|
|
547
406
|
}
|
|
548
407
|
set damping(value) {
|
|
549
408
|
invariant(value >= 0, "Damping must be greater than or equal to 0");
|
|
550
|
-
if (!
|
|
551
|
-
|
|
409
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
410
|
+
this.#scheduler.add(this.#solver);
|
|
552
411
|
}
|
|
553
|
-
|
|
412
|
+
this.#solver.damping = value;
|
|
554
413
|
}
|
|
555
414
|
get precision() {
|
|
556
|
-
return
|
|
415
|
+
return this.#solver.precision;
|
|
557
416
|
}
|
|
558
417
|
set precision(value) {
|
|
559
418
|
invariant(value > 0, "Precision must be greater than 0");
|
|
560
|
-
if (!
|
|
561
|
-
|
|
419
|
+
if (!this.#scheduler.has(this.#solver)) {
|
|
420
|
+
this.#scheduler.add(this.#solver);
|
|
562
421
|
}
|
|
563
|
-
|
|
422
|
+
this.#solver.precision = value;
|
|
564
423
|
}
|
|
565
424
|
get resting() {
|
|
566
|
-
return
|
|
425
|
+
return this.#solver.resting;
|
|
567
426
|
}
|
|
568
427
|
onUpdate(callback) {
|
|
569
|
-
return
|
|
428
|
+
return this.#solver.onUpdate(callback);
|
|
570
429
|
}
|
|
571
430
|
onStart(callback) {
|
|
572
|
-
return
|
|
431
|
+
return this.#solver.onStart(callback);
|
|
573
432
|
}
|
|
574
433
|
onStop(callback) {
|
|
575
|
-
return
|
|
434
|
+
return this.#solver.onStop(callback);
|
|
576
435
|
}
|
|
577
436
|
jumpTo(value) {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
437
|
+
this.#target = value;
|
|
438
|
+
this.#solver.position = 0;
|
|
439
|
+
this.#solver.velocity = 0;
|
|
440
|
+
this.#solver.tick(0);
|
|
582
441
|
}
|
|
583
442
|
}
|
|
584
|
-
_target = new WeakMap();
|
|
585
|
-
_solver = new WeakMap();
|
|
586
|
-
_scheduler$1 = new WeakMap();
|
|
587
443
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
throw TypeError("Cannot " + msg);
|
|
591
|
-
};
|
|
592
|
-
var __privateGet = (obj, member, getter) => {
|
|
593
|
-
__accessCheck(obj, member, "read from private field");
|
|
594
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
595
|
-
};
|
|
596
|
-
var __privateAdd = (obj, member, value) => {
|
|
597
|
-
if (member.has(obj))
|
|
598
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
599
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
600
|
-
};
|
|
601
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
602
|
-
__accessCheck(obj, member, "write to private field");
|
|
603
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
604
|
-
return value;
|
|
605
|
-
};
|
|
606
|
-
var _scheduler;
|
|
607
|
-
class SpringSystem {
|
|
444
|
+
class SpringSystemImpl {
|
|
445
|
+
#scheduler;
|
|
608
446
|
constructor() {
|
|
609
|
-
|
|
610
|
-
__privateSet(this, _scheduler, new Scheduler());
|
|
447
|
+
this.#scheduler = new Scheduler();
|
|
611
448
|
}
|
|
612
449
|
createSpring(options) {
|
|
613
|
-
return new Spring(
|
|
614
|
-
...options,
|
|
615
|
-
target: options.target ?? 0
|
|
616
|
-
});
|
|
450
|
+
return new Spring(this.#scheduler, options);
|
|
617
451
|
}
|
|
618
452
|
tick(dt) {
|
|
619
|
-
|
|
453
|
+
this.#scheduler.tick(dt);
|
|
620
454
|
}
|
|
621
455
|
}
|
|
622
|
-
_scheduler = new WeakMap();
|
|
623
|
-
|
|
624
456
|
function createSpringSystem() {
|
|
625
|
-
return new
|
|
457
|
+
return new SpringSystemImpl();
|
|
626
458
|
}
|
|
627
459
|
|
|
628
460
|
export { createSpringSystem };
|