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