coily 0.12.0 → 0.12.2

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/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { _ as defineSpring, a as Spring2D, c as Spring2DWithOffset, d as SpringPosition, f as SpringTarget, g as SpringState, h as SpringOptions, i as TickerOptions, l as Vector2, m as SpringConfig, n as SpringSystemOptions, o as Spring2DPosition, p as SpringWithOffset, r as createSpringSystem, s as Spring2DTarget, t as SpringSystem, u as Spring } from "./system-Do5rE0j7.js";
1
+ import { _ as defineSpring, a as Spring2D, c as Spring2DWithOffset, d as SpringPosition, f as SpringTarget, g as SpringState, h as SpringOptions, i as TickerOptions, l as Vector2, m as SpringConfig, n as SpringSystemOptions, o as Spring2DPosition, p as SpringWithOffset, r as createSpringSystem, s as Spring2DTarget, t as SpringSystem, u as Spring } from "./system-coAFNYnb.js";
2
2
  export { type Spring, type Spring2D, type Spring2DPosition, type Spring2DTarget, type Spring2DWithOffset, type SpringConfig, type SpringOptions, type SpringPosition, type SpringState, type SpringSystem, type SpringSystemOptions, type SpringTarget, type SpringWithOffset, type TickerOptions, type Vector2, createSpringSystem, defineSpring };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { r as defineSpring, t as createSpringSystem } from "./system-DPaqec-Z.js";
1
+ import { r as defineSpring, t as createSpringSystem } from "./system-Bzrx4uso.js";
2
2
  export { createSpringSystem, defineSpring };
@@ -34,10 +34,13 @@ function invariant(condition, message) {
34
34
  /**
35
35
  * Immutable spring configuration.
36
36
  */
37
- let globalConfigVersion = 0;
38
- var SpringConfig = class {
39
- /** @internal Incremented by `SpringConfig.assign` to signal mutations. */
40
- #version = 0;
37
+ var SpringConfig = class SpringConfig {
38
+ static default = new SpringConfig({
39
+ dampingRatio: 1,
40
+ duration: 500
41
+ });
42
+ /** @internal Incremented by `assign` to signal mutations. Do not read or write directly. */
43
+ _version = 0;
41
44
  mass;
42
45
  tension;
43
46
  damping;
@@ -119,16 +122,10 @@ var SpringConfig = class {
119
122
  this.criticalDamping = 2 * this.mass * this.naturalFrequency;
120
123
  this.dampingRatio = this.damping / this.criticalDamping;
121
124
  }
122
- static get globalVersion() {
123
- return globalConfigVersion;
124
- }
125
- static version(config) {
126
- return config.#version;
127
- }
128
- static assign(target, source) {
129
- globalConfigVersion++;
130
- target.#version++;
131
- const t = target;
125
+ /** @internal Copies physical parameters from `source` onto this config and bumps `_version`. */
126
+ assign(source) {
127
+ this._version++;
128
+ const t = this;
132
129
  t.mass = source.mass;
133
130
  t.tension = source.tension;
134
131
  t.damping = source.damping;
@@ -343,7 +340,6 @@ var Motion = class {
343
340
  #needsUpdate = false;
344
341
  #needsReset = false;
345
342
  #configVersion = 0;
346
- #globalConfigVersion = 0;
347
343
  #timeRemaining = 0;
348
344
  #emitter;
349
345
  constructor(config, position, velocity) {
@@ -377,20 +373,15 @@ var Motion = class {
377
373
  configure(config) {
378
374
  this.#config = config;
379
375
  this.#state.configure(config);
380
- this.#configVersion = SpringConfig.version(config);
381
- this.#globalConfigVersion = SpringConfig.globalVersion;
376
+ this.#configVersion = config._version;
382
377
  this.#needsUpdate = true;
383
378
  }
384
379
  tick(dt, emit = true) {
385
380
  invariant(this.#currentSolver, "Cannot tick a disposed motion");
386
- const gv = SpringConfig.globalVersion;
387
- if (gv !== this.#globalConfigVersion) {
388
- this.#globalConfigVersion = gv;
389
- if (SpringConfig.version(this.#config) !== this.#configVersion) {
390
- this.#state.configure(this.#config);
391
- this.#configVersion = SpringConfig.version(this.#config);
392
- this.#needsUpdate = true;
393
- }
381
+ if (this.#config._version !== this.#configVersion) {
382
+ this.#state.configure(this.#config);
383
+ this.#configVersion = this.#config._version;
384
+ this.#needsUpdate = true;
394
385
  }
395
386
  const needsTimeRemaining = this.#needsUpdate || this.#needsReset;
396
387
  if (this.#needsUpdate) {
@@ -455,14 +446,14 @@ function normalizeTarget(target) {
455
446
  };
456
447
  return null;
457
448
  }
458
- var Spring = class Spring {
449
+ var Spring = class {
459
450
  #target;
460
451
  #config;
461
452
  #motion;
462
453
  #motions;
463
454
  #leader = null;
464
455
  #offset = 0;
465
- #hasConfigOverride;
456
+ #ownsConfig;
466
457
  #unsubLeader = null;
467
458
  constructor(motions, position, config) {
468
459
  this.#motions = motions;
@@ -475,7 +466,7 @@ var Spring = class Spring {
475
466
  } else {
476
467
  normalized = normalizeTarget(position.target);
477
468
  if (normalized) {
478
- numericTarget = Spring.#rawValue(normalized.spring) + normalized.offset;
469
+ numericTarget = normalized.spring.#target + normalized.spring.#motion.position + normalized.offset;
479
470
  value = position.value ?? numericTarget;
480
471
  } else {
481
472
  numericTarget = position.target ?? position.value ?? 0;
@@ -483,9 +474,9 @@ var Spring = class Spring {
483
474
  }
484
475
  }
485
476
  const hasOverride = config !== void 0;
486
- this.#hasConfigOverride = hasOverride || !normalized;
477
+ this.#ownsConfig = hasOverride || !normalized;
487
478
  if (normalized && !hasOverride) this.#config = normalized.spring.#config;
488
- else this.#config = config;
479
+ else this.#config = config ?? SpringConfig.default;
489
480
  this.#target = numericTarget;
490
481
  this.#motion = new Motion(this.#config, value - numericTarget, 0);
491
482
  if (!this.#motion.isResting) this.#motions.add(this.#motion);
@@ -493,7 +484,7 @@ var Spring = class Spring {
493
484
  this.#leader = normalized.spring;
494
485
  this.#offset = normalized.offset;
495
486
  this.#unsubLeader = normalized.spring.onUpdate(() => {
496
- this.#setTarget(Spring.#rawValue(this.#leader) + this.#offset);
487
+ this.#setTarget(this.#leader.#target + this.#leader.#motion.position + this.#offset);
497
488
  });
498
489
  }
499
490
  }
@@ -545,23 +536,22 @@ var Spring = class Spring {
545
536
  get precision() {
546
537
  return this.#config.precision;
547
538
  }
548
- configure(config) {
549
- if (this.#leader && !this.#hasConfigOverride) {
550
- this.#hasConfigOverride = true;
551
- this.#config = new SpringConfig({
552
- tension: config.tension,
553
- damping: config.damping,
554
- mass: config.mass,
555
- precision: config.precision
556
- });
557
- this.#motion.configure(this.#config);
558
- } else SpringConfig.assign(this.#config, config);
559
- this.#motions.add(this.#motion);
560
- }
561
- clearConfigOverride() {
562
- if (this.#leader && this.#hasConfigOverride) {
563
- this.#hasConfigOverride = false;
564
- this.#config = this.#leader.#config;
539
+ set config(value) {
540
+ if (value) {
541
+ if (this.#leader && !this.#ownsConfig) {
542
+ this.#ownsConfig = true;
543
+ this.#config = new SpringConfig({
544
+ tension: value.tension,
545
+ damping: value.damping,
546
+ mass: value.mass,
547
+ precision: value.precision
548
+ });
549
+ this.#motion.configure(this.#config);
550
+ } else this.#config.assign(value);
551
+ this.#motions.add(this.#motion);
552
+ } else if (this.#ownsConfig) {
553
+ this.#ownsConfig = false;
554
+ this.#config = this.#leader ? this.#leader.#config : SpringConfig.default;
565
555
  this.#motion.configure(this.#config);
566
556
  this.#motions.add(this.#motion);
567
557
  }
@@ -595,9 +585,6 @@ var Spring = class Spring {
595
585
  onStop(callback) {
596
586
  return this.#motion.onStop(callback);
597
587
  }
598
- static #rawValue(spring) {
599
- return spring.#target + spring.#motion.position;
600
- }
601
588
  #setTarget(value) {
602
589
  if (value !== this.#target) {
603
590
  this.#motions.add(this.#motion);
@@ -611,14 +598,14 @@ var Spring = class Spring {
611
598
  if (this.#unsubLeader) this.#unsubLeader();
612
599
  this.#leader = leader;
613
600
  this.#offset = offset;
614
- if (!this.#hasConfigOverride) {
601
+ if (!this.#ownsConfig) {
615
602
  this.#config = leader.#config;
616
603
  this.#motion.configure(this.#config);
617
604
  }
618
605
  this.#unsubLeader = leader.onUpdate(() => {
619
- this.#setTarget(Spring.#rawValue(this.#leader) + this.#offset);
606
+ this.#setTarget(this.#leader.#target + this.#leader.#motion.position + this.#offset);
620
607
  });
621
- this.#setTarget(Spring.#rawValue(leader) + offset);
608
+ this.#setTarget(leader.#target + leader.#motion.position + offset);
622
609
  }
623
610
  #unfollow() {
624
611
  if (!this.#leader) return;
@@ -626,8 +613,8 @@ var Spring = class Spring {
626
613
  this.#unsubLeader = null;
627
614
  this.#leader = null;
628
615
  this.#offset = 0;
629
- if (!this.#hasConfigOverride) {
630
- this.#hasConfigOverride = true;
616
+ if (!this.#ownsConfig) {
617
+ this.#ownsConfig = true;
631
618
  this.#config = new SpringConfig({
632
619
  tension: this.#config.tension,
633
620
  damping: this.#config.damping,
@@ -778,13 +765,9 @@ var Spring2D = class Spring2D {
778
765
  get precision() {
779
766
  return this.#x.precision;
780
767
  }
781
- configure(config) {
782
- this.#x.configure(config);
783
- this.#y.configure(config);
784
- }
785
- clearConfigOverride() {
786
- this.#x.clearConfigOverride();
787
- this.#y.clearConfigOverride();
768
+ set config(value) {
769
+ this.#x.config = value;
770
+ this.#y.config = value;
788
771
  }
789
772
  get timeRemaining() {
790
773
  return Math.max(this.#x.timeRemaining, this.#y.timeRemaining);
@@ -71,8 +71,11 @@ interface TensionBounceDurationOptions extends BaseOptions, WithTension, WithBou
71
71
  interface DampingDurationOptions extends BaseOptions, WithDamping, WithDampingRatio, WithDuration, WithDisplacement {}
72
72
  interface DampingBounceDurationOptions extends BaseOptions, WithDamping, WithBounce, WithDuration, WithDisplacement {}
73
73
  type SpringOptions = DirectOptions | TensionRatioOptions | TensionBounceOptions | DampingRatioOptions | DampingBounceOptions | TensionDampingRatioOptions | TensionDampingBounceOptions | DurationOptions | BounceDurationOptions | TensionDurationOptions | TensionBounceDurationOptions | DampingDurationOptions | DampingBounceDurationOptions;
74
+ /**
75
+ * Immutable spring configuration.
76
+ */
74
77
  declare class SpringConfig {
75
- #private;
78
+ static readonly default: SpringConfig;
76
79
  readonly mass: number;
77
80
  readonly tension: number;
78
81
  readonly damping: number;
@@ -83,9 +86,6 @@ declare class SpringConfig {
83
86
  readonly precisionMultiplier: number;
84
87
  readonly restingMagnitude: number;
85
88
  constructor(input: SpringOptions);
86
- static get globalVersion(): number;
87
- static version(config: SpringConfig): number;
88
- static assign(target: SpringConfig, source: SpringConfig): void;
89
89
  /**
90
90
  * Analytically estimates the time remaining for a spring to come to rest,
91
91
  * given the current displacement and velocity.
@@ -229,15 +229,11 @@ interface SpringWithOffset {
229
229
  offset?: number | undefined;
230
230
  }
231
231
  type SpringTarget = number | Spring | SpringWithOffset;
232
- interface StandaloneSpringPosition {
233
- target?: number | undefined;
232
+ interface DisplacedSpringPosition {
233
+ target?: SpringTarget | undefined;
234
234
  value?: number | undefined;
235
235
  }
236
- interface FollowingSpringPosition {
237
- target: Spring | SpringWithOffset;
238
- value?: number | undefined;
239
- }
240
- type SpringPosition = number | StandaloneSpringPosition | FollowingSpringPosition;
236
+ type SpringPosition = number | DisplacedSpringPosition;
241
237
  declare class Spring {
242
238
  #private;
243
239
  constructor(motions: MotionSet, position: SpringPosition, config?: SpringConfig);
@@ -247,14 +243,13 @@ declare class Spring {
247
243
  set value(value: number);
248
244
  get velocity(): number;
249
245
  set velocity(value: number);
250
- get config(): SpringConfig;
246
+ get config(): SpringConfig | null;
251
247
  get mass(): number;
252
248
  get tension(): number;
253
249
  get damping(): number;
254
250
  get dampingRatio(): number;
255
251
  get precision(): number;
256
- configure(config: SpringConfig): void;
257
- clearConfigOverride(): void;
252
+ set config(value: SpringConfig | null);
258
253
  get timeRemaining(): number;
259
254
  get isResting(): boolean;
260
255
  jumpTo(value: number): void;
@@ -290,14 +285,13 @@ declare class Spring2D {
290
285
  set value(value: Vector2);
291
286
  get velocity(): Readonly<Vector2>;
292
287
  set velocity(value: Vector2);
293
- get config(): SpringConfig;
288
+ get config(): SpringConfig | null;
294
289
  get mass(): number;
295
290
  get tension(): number;
296
291
  get damping(): number;
297
292
  get dampingRatio(): number;
298
293
  get precision(): number;
299
- configure(config: SpringConfig): void;
300
- clearConfigOverride(): void;
294
+ set config(value: SpringConfig | null);
301
295
  get timeRemaining(): number;
302
296
  get isResting(): boolean;
303
297
  jumpTo(value: Vector2): void;
package/dist/vue.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { h as SpringOptions, l as Vector2, m as SpringConfig, n as SpringSystemOptions, t as SpringSystem } from "./system-Do5rE0j7.js";
1
+ import { h as SpringOptions, l as Vector2, m as SpringConfig, n as SpringSystemOptions, t as SpringSystem } from "./system-coAFNYnb.js";
2
2
  import * as vue from "vue";
3
3
  import { App, MaybeRefOrGetter, Ref, SlotsType } from "vue";
4
4
 
@@ -13,6 +13,8 @@ interface SpringRef extends Ref<number> {
13
13
  declare function useSpring(target: MaybeRefOrGetter<number>, options?: UseSpringOptions): SpringRef;
14
14
  declare function useSpring(target: SpringRef, options?: UseSpringOptions): SpringRef;
15
15
  declare function useSpring<const T extends readonly MaybeRefOrGetter<number>[]>(targets: T, options?: UseSpringOptions): { [K in keyof T]: SpringRef };
16
+ //#endregion
17
+ //#region src/vue/spring2d.d.ts
16
18
  interface SpringRef2D extends Ref<Readonly<Vector2>> {
17
19
  readonly velocity: Ref<Readonly<Vector2>>;
18
20
  readonly timeRemaining: Ref<number>;
@@ -21,6 +23,7 @@ interface SpringRef2D extends Ref<Readonly<Vector2>> {
21
23
  }
22
24
  declare function useSpring2D(target: MaybeRefOrGetter<Vector2>, options?: UseSpringOptions): SpringRef2D;
23
25
  declare function useSpring2D(target: SpringRef2D, options?: UseSpringOptions): SpringRef2D;
26
+ declare function useSpring2D<const T extends readonly MaybeRefOrGetter<Vector2>[]>(targets: T, options?: UseSpringOptions): { [K in keyof T]: SpringRef2D };
24
27
  //#endregion
25
28
  //#region src/vue/component.d.ts
26
29
  interface SpringValueProps {
package/dist/vue.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as SpringConfig, t as createSpringSystem } from "./system-DPaqec-Z.js";
1
+ import { n as SpringConfig, t as createSpringSystem } from "./system-Bzrx4uso.js";
2
2
  import { computed, customRef, defineComponent, getCurrentScope, inject, onBeforeUnmount, onMounted, onScopeDispose, provide, toRefs, toValue, watchSyncEffect } from "vue";
3
3
  //#region src/vue/system.ts
4
4
  const SpringSystemKey = Symbol.for("coily/spring-system");
@@ -18,11 +18,6 @@ function useSpringSystem(options) {
18
18
  }
19
19
  //#endregion
20
20
  //#region src/vue/spring.ts
21
- const defaultOptions = {
22
- tension: 100,
23
- damping: 10,
24
- precision: 2
25
- };
26
21
  /** @internal Symbol to access the underlying Spring from a SpringRef */
27
22
  const SpringInstanceKey = Symbol("spring");
28
23
  function hasSpringInstance(value) {
@@ -39,11 +34,13 @@ function createSpringRef(target, options) {
39
34
  const config = computed(() => {
40
35
  const opts = toValue(options);
41
36
  if (opts instanceof SpringConfig) return opts;
42
- return new SpringConfig(opts ?? defaultOptions);
37
+ return opts ? new SpringConfig(opts) : void 0;
43
38
  });
44
- const spring = system.createSpring(toValue(target), config.value);
39
+ const spring = system.createSpring(toValue(target), config.value ?? void 0);
45
40
  watchSyncEffect(() => {
46
- spring.configure(config.value);
41
+ const c = config.value;
42
+ if (c) spring.config = c;
43
+ else spring.config = null;
47
44
  });
48
45
  let triggerValue;
49
46
  let triggerVelocity;
@@ -120,8 +117,8 @@ function createLinkedSpringRef(leaderRef, options) {
120
117
  const spring = system.createSpring({ target: leaderSpring }, config.value);
121
118
  watchSyncEffect(() => {
122
119
  const c = config.value;
123
- if (c) spring.configure(c);
124
- else spring.clearConfigOverride();
120
+ if (c) spring.config = c;
121
+ else spring.config = null;
125
122
  });
126
123
  let triggerValue;
127
124
  let triggerVelocity;
@@ -182,12 +179,15 @@ function createLinkedSpringRef(leaderRef, options) {
182
179
  Object.defineProperty(ref, SpringInstanceKey, { value: spring });
183
180
  return ref;
184
181
  }
182
+ //#endregion
183
+ //#region src/vue/spring2d.ts
185
184
  /** @internal Symbol to access the underlying Spring2D from a SpringRef2D */
186
185
  const Spring2DInstanceKey = Symbol("spring2d");
187
186
  function hasSpring2DInstance(value) {
188
187
  return typeof value === "object" && value !== null && Spring2DInstanceKey in value;
189
188
  }
190
189
  function useSpring2D(target, options) {
190
+ if (Array.isArray(target)) return Array.from(target, (t) => createSpringRef2D(t, options));
191
191
  if (hasSpring2DInstance(target)) return createLinkedSpringRef2D(target, options);
192
192
  return createSpringRef2D(target, options);
193
193
  }
@@ -197,11 +197,13 @@ function createSpringRef2D(target, options) {
197
197
  const config = computed(() => {
198
198
  const opts = toValue(options);
199
199
  if (opts instanceof SpringConfig) return opts;
200
- return new SpringConfig(opts ?? defaultOptions);
200
+ return opts ? new SpringConfig(opts) : void 0;
201
201
  });
202
- const spring = system.createSpring2D(toValue(target), config.value);
202
+ const spring = system.createSpring2D(toValue(target), config.value ?? void 0);
203
203
  watchSyncEffect(() => {
204
- spring.configure(config.value);
204
+ const c = config.value;
205
+ if (c) spring.config = c;
206
+ else spring.config = null;
205
207
  });
206
208
  let triggerValue;
207
209
  let triggerVelocity;
@@ -278,8 +280,8 @@ function createLinkedSpringRef2D(leaderRef, options) {
278
280
  const spring = system.createSpring2D({ target: leaderSpring }, config.value);
279
281
  watchSyncEffect(() => {
280
282
  const c = config.value;
281
- if (c) spring.configure(c);
282
- else spring.clearConfigOverride();
283
+ if (c) spring.config = c;
284
+ else spring.config = null;
283
285
  });
284
286
  let triggerValue;
285
287
  let triggerVelocity;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "Simulate values using spring physics",
5
5
  "homepage": "https://github.com/tkofh/coily#readme",
6
6
  "bugs": {