coily 0.9.0 → 0.10.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.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
- constructor() {
17
- __privateAdd$5(this, _solvers, /* @__PURE__ */ new Set());
18
- }
2
+ #solvers = /* @__PURE__ */ new Set();
19
3
  tick(delta) {
20
- for (const solver of __privateGet$5(this, _solvers).values()) {
4
+ for (const solver of this.#solvers.values()) {
21
5
  solver.tick(delta);
22
6
  if (solver.resting) {
23
- __privateGet$5(this, _solvers).delete(solver);
7
+ this.#solvers.delete(solver);
24
8
  }
25
9
  }
26
10
  }
27
11
  add(solver) {
28
- __privateGet$5(this, _solvers).add(solver);
12
+ this.#solvers.add(solver);
29
13
  }
30
14
  remove(solver) {
31
- __privateGet$5(this, _solvers).delete(solver);
15
+ this.#solvers.delete(solver);
32
16
  }
33
17
  has(solver) {
34
- return __privateGet$5(this, _solvers).has(solver);
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
- constructor() {
55
- __privateAdd$4(this, _handlers, /* @__PURE__ */ new Map());
56
- }
23
+ #handlers = /* @__PURE__ */ new Map();
57
24
  on(type, handler) {
58
- const handlers = __privateGet$4(this, _handlers).get(type);
25
+ const handlers = this.#handlers.get(type);
59
26
  if (handlers) {
60
27
  handlers.push(handler);
61
28
  } else {
62
- __privateGet$4(this, _handlers).set(type, [handler]);
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 = __privateGet$4(this, _handlers).get(type);
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
- __privateGet$4(this, _handlers).set(type, []);
41
+ this.#handlers.set(type, []);
75
42
  }
76
43
  }
77
44
  }
78
45
  emit(type, evt) {
79
- const handlers = __privateGet$4(this, _handlers)?.get(type);
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
- __privateAdd$3(this, _precision, void 0);
122
- __privateAdd$3(this, _restingMagnitude, void 0);
123
- __privateAdd$3(this, _position, void 0);
124
- __privateAdd$3(this, _velocity, void 0);
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 __privateGet$3(this, _position);
78
+ return this.#position;
132
79
  }
133
80
  set position(value) {
134
- __privateSet$3(this, _position, roundTo(value, __privateGet$3(this, _precision)));
81
+ this.#position = roundTo(value, this.#precision);
135
82
  }
136
83
  get velocity() {
137
- return __privateGet$3(this, _velocity);
84
+ return this.#velocity;
138
85
  }
139
86
  set velocity(value) {
140
- __privateSet$3(this, _velocity, roundTo(value, __privateGet$3(this, _precision)));
87
+ this.#velocity = roundTo(value, this.#precision);
141
88
  }
142
89
  get precision() {
143
- return __privateGet$3(this, _precision);
90
+ return this.#precision;
144
91
  }
145
92
  set precision(value) {
146
- __privateSet$3(this, _precision, value);
147
- __privateSet$3(this, _restingMagnitude, 1 / 10 ** __privateGet$3(this, _precision));
148
- __privateSet$3(this, _position, roundTo(__privateGet$3(this, _position), __privateGet$3(this, _precision)));
149
- __privateSet$3(this, _velocity, roundTo(__privateGet$3(this, _velocity), __privateGet$3(this, _precision)));
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(__privateGet$3(this, _velocity)) < __privateGet$3(this, _restingMagnitude) && Math.abs(__privateGet$3(this, _position)) < __privateGet$3(this, _restingMagnitude);
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
- __privateAdd$2(this, _updateSolver);
186
- __privateAdd$2(this, _mass, void 0);
187
- __privateAdd$2(this, _tension, void 0);
188
- __privateAdd$2(this, _damping, void 0);
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
- __privateSet$2(this, _lazyPosition, options.position);
207
- __privateSet$2(this, _emitter, new Emitter());
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 __privateGet$2(this, _mass);
128
+ return this.#mass;
212
129
  }
213
130
  set mass(value) {
214
- __privateSet$2(this, _mass, value);
215
- __privateSet$2(this, _needsUpdate, true);
131
+ this.#mass = value;
132
+ this.#needsUpdate = true;
216
133
  }
217
134
  get tension() {
218
- return __privateGet$2(this, _tension);
135
+ return this.#tension;
219
136
  }
220
137
  set tension(value) {
221
- __privateSet$2(this, _tension, value);
222
- __privateSet$2(this, _needsUpdate, true);
138
+ this.#tension = value;
139
+ this.#needsUpdate = true;
223
140
  }
224
141
  get damping() {
225
- return __privateGet$2(this, _damping);
142
+ return this.#damping;
226
143
  }
227
144
  set damping(value) {
228
- __privateSet$2(this, _damping, value);
229
- __privateSet$2(this, _needsUpdate, true);
145
+ this.#damping = value;
146
+ this.#needsUpdate = true;
230
147
  }
231
148
  get position() {
232
- return __privateGet$2(this, _state).position;
149
+ return this.#state.position;
233
150
  }
234
151
  set position(value) {
235
- __privateGet$2(this, _state).position = value;
236
- __privateSet$2(this, _needsReset, true);
237
- if (!__privateGet$2(this, _state).resting) {
238
- __privateGet$2(this, _emitter).emit("start");
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 __privateGet$2(this, _state).velocity;
159
+ return this.#state.velocity;
246
160
  }
247
161
  set velocity(value) {
248
- __privateGet$2(this, _state).velocity = value;
249
- __privateSet$2(this, _needsReset, true);
162
+ this.#state.velocity = value;
163
+ this.#needsReset = true;
250
164
  }
251
165
  get precision() {
252
- return __privateGet$2(this, _state).precision;
166
+ return this.#state.precision;
253
167
  }
254
168
  set precision(value) {
255
- __privateGet$2(this, _state).precision = value;
169
+ this.#state.precision = value;
256
170
  }
257
171
  get resting() {
258
- return __privateGet$2(this, _state).resting;
172
+ return this.#state.resting;
259
173
  }
260
174
  get naturalFrequency() {
261
- return Math.sqrt(__privateGet$2(this, _tension) / __privateGet$2(this, _mass));
175
+ return Math.sqrt(this.#tension / this.#mass);
262
176
  }
263
177
  get criticalDamping() {
264
- return 2 * __privateGet$2(this, _mass) * this.naturalFrequency;
178
+ return 2 * this.#mass * this.naturalFrequency;
265
179
  }
266
180
  get dampingRatio() {
267
- return __privateGet$2(this, _damping) / this.criticalDamping;
181
+ return this.#damping / this.criticalDamping;
268
182
  }
269
183
  tick(dt, emit = true) {
270
- if (__privateGet$2(this, _needsUpdate)) {
271
- __privateMethod(this, _updateSolver, updateSolver_fn).call(this);
272
- __privateSet$2(this, _needsUpdate, false);
184
+ if (this.#needsUpdate) {
185
+ this.#updateSolver();
186
+ this.#needsUpdate = false;
273
187
  }
274
- if (__privateGet$2(this, _needsReset)) {
275
- __privateGet$2(this, _currentSolver).reset();
276
- __privateSet$2(this, _needsReset, false);
188
+ if (this.#needsReset) {
189
+ this.#currentSolver.reset();
190
+ this.#needsReset = false;
277
191
  }
278
- __privateGet$2(this, _currentSolver).tick(dt);
279
- __privateSet$2(this, _lazyPosition, __privateGet$2(this, _state).position);
192
+ this.#currentSolver.tick(dt);
280
193
  if (emit) {
281
- __privateGet$2(this, _emitter).emit("update");
282
- if (__privateGet$2(this, _state).resting) {
283
- __privateGet$2(this, _emitter).emit("stop");
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 __privateGet$2(this, _emitter).on("update", callback);
201
+ return this.#emitter.on("update", callback);
289
202
  }
290
203
  onStart(callback) {
291
- return __privateGet$2(this, _emitter).on("start", callback);
204
+ return this.#emitter.on("start", callback);
292
205
  }
293
206
  onStop(callback) {
294
- return __privateGet$2(this, _emitter).on("stop", callback);
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
- __privateAdd$2(this, _solver$1, void 0);
328
- __privateAdd$2(this, _state2, void 0);
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
- __privateSet$2(this, _dampedFrequency, __privateGet$2(this, _solver$1).naturalFrequency * Math.sqrt(1 - __privateGet$2(this, _solver$1).dampingRatio ** 2));
339
- __privateSet$2(this, _t, 0);
340
- __privateSet$2(this, _c1, __privateGet$2(this, _state2).position);
341
- __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));
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
- __privateSet$2(this, _t, __privateGet$2(this, _t) + dt);
346
- const sin = Math.sin(__privateGet$2(this, _dampedFrequency) * __privateGet$2(this, _t));
347
- const cos = Math.cos(__privateGet$2(this, _dampedFrequency) * __privateGet$2(this, _t));
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
- -__privateGet$2(this, _solver$1).dampingRatio * __privateGet$2(this, _solver$1).naturalFrequency * __privateGet$2(this, _t)
249
+ -this.#solver.dampingRatio * this.#solver.naturalFrequency * this.#t
350
250
  );
351
- const decayVelocity = -__privateGet$2(this, _solver$1).dampingRatio * __privateGet$2(this, _solver$1).naturalFrequency * decay;
352
- const oscillation = __privateGet$2(this, _c1) * cos + __privateGet$2(this, _c2) * sin;
353
- const oscillationVelocity = -__privateGet$2(this, _c1) * __privateGet$2(this, _dampedFrequency) * sin + __privateGet$2(this, _c2) * __privateGet$2(this, _dampedFrequency) * cos;
354
- __privateGet$2(this, _state2).position = decay * oscillation;
355
- __privateGet$2(this, _state2).velocity = decay * oscillationVelocity + decayVelocity * oscillation;
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
- __privateAdd$2(this, _solver2, void 0);
367
- __privateAdd$2(this, _state3, void 0);
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
- __privateSet$2(this, _t2, 0);
377
- __privateSet$2(this, _c12, __privateGet$2(this, _state3).position);
378
- __privateSet$2(this, _c22, __privateGet$2(this, _state3).velocity + __privateGet$2(this, _solver2).naturalFrequency * __privateGet$2(this, _state3).position);
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
- __privateSet$2(this, _t2, __privateGet$2(this, _t2) + dt);
383
- const decay = Math.exp(-__privateGet$2(this, _solver2).naturalFrequency * __privateGet$2(this, _t2));
384
- const decayVelocity = -__privateGet$2(this, _solver2).naturalFrequency * decay;
385
- const scale = __privateGet$2(this, _c12) + __privateGet$2(this, _c22) * __privateGet$2(this, _t2);
386
- const scaleVelocity = __privateGet$2(this, _c22);
387
- __privateGet$2(this, _state3).position = scale * decay;
388
- __privateGet$2(this, _state3).velocity = scaleVelocity * decay + decayVelocity * scale;
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
- __privateAdd$2(this, _solver3, void 0);
399
- __privateAdd$2(this, _state4, void 0);
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
- __privateSet$2(this, _dampedFrequency2, __privateGet$2(this, _solver3).naturalFrequency * Math.sqrt(__privateGet$2(this, _solver3).dampingRatio ** 2 - 1));
410
- __privateSet$2(this, _t3, 0);
411
- __privateSet$2(this, _c13, __privateGet$2(this, _state4).velocity + __privateGet$2(this, _solver3).dampingRatio * __privateGet$2(this, _solver3).naturalFrequency * __privateGet$2(this, _state4).position);
412
- __privateSet$2(this, _c23, __privateGet$2(this, _state4).position * __privateGet$2(this, _dampedFrequency2));
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
- __privateSet$2(this, _t3, __privateGet$2(this, _t3) + dt);
304
+ this.#t += dt;
416
305
  const decay = Math.exp(
417
- -__privateGet$2(this, _solver3).dampingRatio * __privateGet$2(this, _solver3).naturalFrequency * __privateGet$2(this, _t3)
306
+ -this.#solver.dampingRatio * this.#solver.naturalFrequency * this.#t
418
307
  );
419
- const decayVelocity = -__privateGet$2(this, _solver3).dampingRatio * __privateGet$2(this, _solver3).naturalFrequency * decay;
420
- const clamped = Math.min(__privateGet$2(this, _dampedFrequency2) * __privateGet$2(this, _t3), 300);
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 = __privateGet$2(this, _c13) * sinh + __privateGet$2(this, _c23) * cosh;
424
- const scaleVelocity = -__privateGet$2(this, _c13) * __privateGet$2(this, _dampedFrequency2) * sinh + __privateGet$2(this, _c23) * __privateGet$2(this, _dampedFrequency2) * cosh;
425
- __privateGet$2(this, _state4).position = scale * decay / __privateGet$2(this, _dampedFrequency2);
426
- __privateGet$2(this, _state4).velocity = (scale * decayVelocity + scaleVelocity * decay) / __privateGet$2(this, _dampedFrequency2);
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
- // #newTarget: number
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,133 @@ class Spring {
468
331
  options.precision === void 0 || options.precision > 0,
469
332
  "Precision must be greater than 0"
470
333
  );
471
- __privateSet$1(this, _target, options.target);
472
- __privateSet$1(this, _solver, new Solver({
334
+ this.#target = options.target ?? 0;
335
+ this.#solver = new Solver({
473
336
  mass: options.mass,
474
337
  tension: options.tension,
475
338
  damping: options.damping,
476
- position: options.target + (options.value ?? 0),
339
+ position: this.#target + (options.value ?? 0),
477
340
  velocity: 0,
478
341
  precision: options.precision ?? 2
479
- }));
480
- __privateSet$1(this, _scheduler$1, scheduler);
481
- if (!__privateGet$1(this, _solver).resting) {
482
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
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 __privateGet$1(this, _target);
349
+ return this.#target;
487
350
  }
488
351
  set target(value) {
489
- if (value !== __privateGet$1(this, _target)) {
490
- if (__privateGet$1(this, _solver).position !== __privateGet$1(this, _solver).lazyPosition) {
491
- __privateGet$1(this, _solver).tick(0);
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
- __privateSet$1(this, _target, value);
498
- __privateGet$1(this, _solver).position = currentValue - __privateGet$1(this, _target);
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 __privateGet$1(this, _target) + __privateGet$1(this, _solver).position;
363
+ return this.#target + this.#solver.position;
503
364
  }
504
365
  set value(value) {
505
- const position = value - __privateGet$1(this, _target);
506
- if (position !== __privateGet$1(this, _solver).position) {
507
- if (__privateGet$1(this, _solver).position !== __privateGet$1(this, _solver).lazyPosition) {
508
- __privateGet$1(this, _solver).tick(0);
509
- }
510
- if (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
511
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
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);
512
370
  }
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 __privateGet$1(this, _solver).velocity;
376
+ return this.#solver.velocity;
518
377
  }
519
378
  set velocity(value) {
520
- if (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
521
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
379
+ if (!this.#scheduler.has(this.#solver)) {
380
+ this.#scheduler.add(this.#solver);
522
381
  }
523
- __privateGet$1(this, _solver).velocity = value;
382
+ this.#solver.velocity = value;
524
383
  }
525
384
  get mass() {
526
- return __privateGet$1(this, _solver).mass;
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 (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
531
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
389
+ if (!this.#scheduler.has(this.#solver)) {
390
+ this.#scheduler.add(this.#solver);
532
391
  }
533
- __privateGet$1(this, _solver).mass = value;
392
+ this.#solver.mass = value;
534
393
  }
535
394
  get tension() {
536
- return __privateGet$1(this, _solver).tension;
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 (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
541
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
399
+ if (!this.#scheduler.has(this.#solver)) {
400
+ this.#scheduler.add(this.#solver);
542
401
  }
543
- __privateGet$1(this, _solver).tension = value;
402
+ this.#solver.tension = value;
544
403
  }
545
404
  get damping() {
546
- return __privateGet$1(this, _solver).damping;
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 (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
551
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
409
+ if (!this.#scheduler.has(this.#solver)) {
410
+ this.#scheduler.add(this.#solver);
552
411
  }
553
- __privateGet$1(this, _solver).damping = value;
412
+ this.#solver.damping = value;
554
413
  }
555
414
  get precision() {
556
- return __privateGet$1(this, _solver).precision;
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 (!__privateGet$1(this, _scheduler$1).has(__privateGet$1(this, _solver))) {
561
- __privateGet$1(this, _scheduler$1).add(__privateGet$1(this, _solver));
419
+ if (!this.#scheduler.has(this.#solver)) {
420
+ this.#scheduler.add(this.#solver);
562
421
  }
563
- __privateGet$1(this, _solver).precision = value;
422
+ this.#solver.precision = value;
564
423
  }
565
424
  get resting() {
566
- return __privateGet$1(this, _solver).resting;
425
+ return this.#solver.resting;
567
426
  }
568
427
  onUpdate(callback) {
569
- return __privateGet$1(this, _solver).onUpdate(callback);
428
+ return this.#solver.onUpdate(callback);
570
429
  }
571
430
  onStart(callback) {
572
- return __privateGet$1(this, _solver).onStart(callback);
431
+ return this.#solver.onStart(callback);
573
432
  }
574
433
  onStop(callback) {
575
- return __privateGet$1(this, _solver).onStop(callback);
434
+ return this.#solver.onStop(callback);
576
435
  }
577
436
  jumpTo(value) {
578
- __privateSet$1(this, _target, value);
579
- __privateGet$1(this, _solver).position = 0;
580
- __privateGet$1(this, _solver).velocity = 0;
581
- __privateGet$1(this, _solver).tick(0);
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
- var __accessCheck = (obj, member, msg) => {
589
- if (!member.has(obj))
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
- __privateAdd(this, _scheduler, void 0);
610
- __privateSet(this, _scheduler, new Scheduler());
447
+ this.#scheduler = new Scheduler();
611
448
  }
612
449
  createSpring(options) {
613
- return new Spring(__privateGet(this, _scheduler), {
450
+ return new Spring(this.#scheduler, {
614
451
  ...options,
615
452
  target: options.target ?? 0
616
453
  });
617
454
  }
618
455
  tick(dt) {
619
- __privateGet(this, _scheduler).tick(dt);
456
+ this.#scheduler.tick(dt);
620
457
  }
621
458
  }
622
- _scheduler = new WeakMap();
623
-
624
459
  function createSpringSystem() {
625
- return new SpringSystem();
460
+ return new SpringSystemImpl();
626
461
  }
627
462
 
628
463
  export { createSpringSystem };