coily 0.12.0 → 0.12.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/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-CMaMt_7p.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-BD52hyd8.js";
2
2
  export { createSpringSystem, defineSpring };
@@ -34,8 +34,11 @@ function invariant(condition, message) {
34
34
  /**
35
35
  * Immutable spring configuration.
36
36
  */
37
- let globalConfigVersion = 0;
38
- var SpringConfig = class {
37
+ var SpringConfig = class SpringConfig {
38
+ static default = new SpringConfig({
39
+ dampingRatio: 1,
40
+ duration: 500
41
+ });
39
42
  /** @internal Incremented by `SpringConfig.assign` to signal mutations. */
40
43
  #version = 0;
41
44
  mass;
@@ -119,14 +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
125
  static version(config) {
126
126
  return config.#version;
127
127
  }
128
128
  static assign(target, source) {
129
- globalConfigVersion++;
130
129
  target.#version++;
131
130
  const t = target;
132
131
  t.mass = source.mass;
@@ -343,7 +342,6 @@ var Motion = class {
343
342
  #needsUpdate = false;
344
343
  #needsReset = false;
345
344
  #configVersion = 0;
346
- #globalConfigVersion = 0;
347
345
  #timeRemaining = 0;
348
346
  #emitter;
349
347
  constructor(config, position, velocity) {
@@ -378,19 +376,14 @@ var Motion = class {
378
376
  this.#config = config;
379
377
  this.#state.configure(config);
380
378
  this.#configVersion = SpringConfig.version(config);
381
- this.#globalConfigVersion = SpringConfig.globalVersion;
382
379
  this.#needsUpdate = true;
383
380
  }
384
381
  tick(dt, emit = true) {
385
382
  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
- }
383
+ if (SpringConfig.version(this.#config) !== this.#configVersion) {
384
+ this.#state.configure(this.#config);
385
+ this.#configVersion = SpringConfig.version(this.#config);
386
+ this.#needsUpdate = true;
394
387
  }
395
388
  const needsTimeRemaining = this.#needsUpdate || this.#needsReset;
396
389
  if (this.#needsUpdate) {
@@ -455,14 +448,14 @@ function normalizeTarget(target) {
455
448
  };
456
449
  return null;
457
450
  }
458
- var Spring = class Spring {
451
+ var Spring = class {
459
452
  #target;
460
453
  #config;
461
454
  #motion;
462
455
  #motions;
463
456
  #leader = null;
464
457
  #offset = 0;
465
- #hasConfigOverride;
458
+ #ownsConfig;
466
459
  #unsubLeader = null;
467
460
  constructor(motions, position, config) {
468
461
  this.#motions = motions;
@@ -475,7 +468,7 @@ var Spring = class Spring {
475
468
  } else {
476
469
  normalized = normalizeTarget(position.target);
477
470
  if (normalized) {
478
- numericTarget = Spring.#rawValue(normalized.spring) + normalized.offset;
471
+ numericTarget = normalized.spring.#target + normalized.spring.#motion.position + normalized.offset;
479
472
  value = position.value ?? numericTarget;
480
473
  } else {
481
474
  numericTarget = position.target ?? position.value ?? 0;
@@ -483,9 +476,9 @@ var Spring = class Spring {
483
476
  }
484
477
  }
485
478
  const hasOverride = config !== void 0;
486
- this.#hasConfigOverride = hasOverride || !normalized;
479
+ this.#ownsConfig = hasOverride || !normalized;
487
480
  if (normalized && !hasOverride) this.#config = normalized.spring.#config;
488
- else this.#config = config;
481
+ else this.#config = config ?? SpringConfig.default;
489
482
  this.#target = numericTarget;
490
483
  this.#motion = new Motion(this.#config, value - numericTarget, 0);
491
484
  if (!this.#motion.isResting) this.#motions.add(this.#motion);
@@ -493,7 +486,7 @@ var Spring = class Spring {
493
486
  this.#leader = normalized.spring;
494
487
  this.#offset = normalized.offset;
495
488
  this.#unsubLeader = normalized.spring.onUpdate(() => {
496
- this.#setTarget(Spring.#rawValue(this.#leader) + this.#offset);
489
+ this.#setTarget(this.#leader.#target + this.#leader.#motion.position + this.#offset);
497
490
  });
498
491
  }
499
492
  }
@@ -545,23 +538,22 @@ var Spring = class Spring {
545
538
  get precision() {
546
539
  return this.#config.precision;
547
540
  }
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;
541
+ set config(value) {
542
+ if (value) {
543
+ if (this.#leader && !this.#ownsConfig) {
544
+ this.#ownsConfig = true;
545
+ this.#config = new SpringConfig({
546
+ tension: value.tension,
547
+ damping: value.damping,
548
+ mass: value.mass,
549
+ precision: value.precision
550
+ });
551
+ this.#motion.configure(this.#config);
552
+ } else SpringConfig.assign(this.#config, value);
553
+ this.#motions.add(this.#motion);
554
+ } else if (this.#ownsConfig) {
555
+ this.#ownsConfig = false;
556
+ this.#config = this.#leader ? this.#leader.#config : SpringConfig.default;
565
557
  this.#motion.configure(this.#config);
566
558
  this.#motions.add(this.#motion);
567
559
  }
@@ -595,9 +587,6 @@ var Spring = class Spring {
595
587
  onStop(callback) {
596
588
  return this.#motion.onStop(callback);
597
589
  }
598
- static #rawValue(spring) {
599
- return spring.#target + spring.#motion.position;
600
- }
601
590
  #setTarget(value) {
602
591
  if (value !== this.#target) {
603
592
  this.#motions.add(this.#motion);
@@ -611,14 +600,14 @@ var Spring = class Spring {
611
600
  if (this.#unsubLeader) this.#unsubLeader();
612
601
  this.#leader = leader;
613
602
  this.#offset = offset;
614
- if (!this.#hasConfigOverride) {
603
+ if (!this.#ownsConfig) {
615
604
  this.#config = leader.#config;
616
605
  this.#motion.configure(this.#config);
617
606
  }
618
607
  this.#unsubLeader = leader.onUpdate(() => {
619
- this.#setTarget(Spring.#rawValue(this.#leader) + this.#offset);
608
+ this.#setTarget(this.#leader.#target + this.#leader.#motion.position + this.#offset);
620
609
  });
621
- this.#setTarget(Spring.#rawValue(leader) + offset);
610
+ this.#setTarget(leader.#target + leader.#motion.position + offset);
622
611
  }
623
612
  #unfollow() {
624
613
  if (!this.#leader) return;
@@ -626,8 +615,8 @@ var Spring = class Spring {
626
615
  this.#unsubLeader = null;
627
616
  this.#leader = null;
628
617
  this.#offset = 0;
629
- if (!this.#hasConfigOverride) {
630
- this.#hasConfigOverride = true;
618
+ if (!this.#ownsConfig) {
619
+ this.#ownsConfig = true;
631
620
  this.#config = new SpringConfig({
632
621
  tension: this.#config.tension,
633
622
  damping: this.#config.damping,
@@ -778,13 +767,9 @@ var Spring2D = class Spring2D {
778
767
  get precision() {
779
768
  return this.#x.precision;
780
769
  }
781
- configure(config) {
782
- this.#x.configure(config);
783
- this.#y.configure(config);
784
- }
785
- clearConfigOverride() {
786
- this.#x.clearConfigOverride();
787
- this.#y.clearConfigOverride();
770
+ set config(value) {
771
+ this.#x.config = value;
772
+ this.#y.config = value;
788
773
  }
789
774
  get timeRemaining() {
790
775
  return Math.max(this.#x.timeRemaining, this.#y.timeRemaining);
@@ -71,8 +71,12 @@ 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
78
  #private;
79
+ static readonly default: SpringConfig;
76
80
  readonly mass: number;
77
81
  readonly tension: number;
78
82
  readonly damping: number;
@@ -83,7 +87,6 @@ declare class SpringConfig {
83
87
  readonly precisionMultiplier: number;
84
88
  readonly restingMagnitude: number;
85
89
  constructor(input: SpringOptions);
86
- static get globalVersion(): number;
87
90
  static version(config: SpringConfig): number;
88
91
  static assign(target: SpringConfig, source: SpringConfig): void;
89
92
  /**
@@ -229,15 +232,11 @@ interface SpringWithOffset {
229
232
  offset?: number | undefined;
230
233
  }
231
234
  type SpringTarget = number | Spring | SpringWithOffset;
232
- interface StandaloneSpringPosition {
233
- target?: number | undefined;
234
- value?: number | undefined;
235
- }
236
- interface FollowingSpringPosition {
237
- target: Spring | SpringWithOffset;
235
+ interface DisplacedSpringPosition {
236
+ target?: SpringTarget | undefined;
238
237
  value?: number | undefined;
239
238
  }
240
- type SpringPosition = number | StandaloneSpringPosition | FollowingSpringPosition;
239
+ type SpringPosition = number | DisplacedSpringPosition;
241
240
  declare class Spring {
242
241
  #private;
243
242
  constructor(motions: MotionSet, position: SpringPosition, config?: SpringConfig);
@@ -247,14 +246,13 @@ declare class Spring {
247
246
  set value(value: number);
248
247
  get velocity(): number;
249
248
  set velocity(value: number);
250
- get config(): SpringConfig;
249
+ get config(): SpringConfig | null;
251
250
  get mass(): number;
252
251
  get tension(): number;
253
252
  get damping(): number;
254
253
  get dampingRatio(): number;
255
254
  get precision(): number;
256
- configure(config: SpringConfig): void;
257
- clearConfigOverride(): void;
255
+ set config(value: SpringConfig | null);
258
256
  get timeRemaining(): number;
259
257
  get isResting(): boolean;
260
258
  jumpTo(value: number): void;
@@ -290,14 +288,13 @@ declare class Spring2D {
290
288
  set value(value: Vector2);
291
289
  get velocity(): Readonly<Vector2>;
292
290
  set velocity(value: Vector2);
293
- get config(): SpringConfig;
291
+ get config(): SpringConfig | null;
294
292
  get mass(): number;
295
293
  get tension(): number;
296
294
  get damping(): number;
297
295
  get dampingRatio(): number;
298
296
  get precision(): number;
299
- configure(config: SpringConfig): void;
300
- clearConfigOverride(): void;
297
+ set config(value: SpringConfig | null);
301
298
  get timeRemaining(): number;
302
299
  get isResting(): boolean;
303
300
  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-CMaMt_7p.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-BD52hyd8.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.1",
4
4
  "description": "Simulate values using spring physics",
5
5
  "homepage": "https://github.com/tkofh/coily#readme",
6
6
  "bugs": {