coily 0.2.0 → 0.4.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/index.cjs CHANGED
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
18
18
  return to;
19
19
  };
20
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
26
  mod
23
27
  ));
@@ -71,26 +75,38 @@ var createSpringImpl = (initial, config, options) => {
71
75
  },
72
76
  set target(val) {
73
77
  target = val;
74
- if (Math.abs(value - target) > restingDistance) {
78
+ if (state !== "moving" && Math.abs(value - target) > restingDistance) {
75
79
  state = "moving";
76
80
  emit("update:state", state);
77
81
  }
78
82
  },
83
+ getTarget: () => target,
79
84
  get value() {
80
85
  return value;
81
86
  },
87
+ getValue: () => value,
82
88
  get velocity() {
83
89
  return velocity;
84
90
  },
91
+ getVelocity: () => velocity,
85
92
  get state() {
86
93
  return state;
87
94
  },
95
+ getState: () => state,
88
96
  get config() {
89
97
  return config;
90
98
  },
91
99
  set config(value2) {
92
100
  config = value2;
93
101
  },
102
+ getConfig: () => config,
103
+ jumpTo: (_value, killVelocity = true) => {
104
+ value = _value;
105
+ if (killVelocity) {
106
+ velocity = 0;
107
+ }
108
+ state = Math.abs(velocity) > restingVelocity || Math.abs(value - target) > restingDistance ? "moving" : "resting";
109
+ },
94
110
  freeze: () => {
95
111
  state = "frozen";
96
112
  emit("update:state", state);
@@ -100,13 +116,15 @@ var createSpringImpl = (initial, config, options) => {
100
116
  emit("update:state", state);
101
117
  }
102
118
  };
119
+ const tension = -config.tension * 1e-6;
120
+ const friction = -config.friction * 1e-3;
103
121
  const simulate = (deltaTime) => {
104
122
  if (state === "moving") {
105
123
  const iterations = Math.ceil(deltaTime);
106
124
  for (let n = 0; n < iterations; n++) {
107
125
  const previousDelta = value - target;
108
- const springForce = -config.tension * 1e-6 * previousDelta;
109
- const dampingForce = -config.friction * 1e-3 * velocity;
126
+ const springForce = tension * previousDelta;
127
+ const dampingForce = friction * velocity;
110
128
  const acceleration = (springForce + dampingForce) / config.mass;
111
129
  velocity = velocity + acceleration;
112
130
  value = value + velocity;
@@ -237,9 +255,11 @@ var createSpringChainImpl = (initial, links, config, options) => {
237
255
  currentTarget = val;
238
256
  updateChainState(currentTarget, currentLinks);
239
257
  },
258
+ getTarget: () => targets[0],
240
259
  get targets() {
241
260
  return targets;
242
261
  },
262
+ getTargets: () => targets,
243
263
  get links() {
244
264
  return links;
245
265
  },
@@ -250,18 +270,23 @@ var createSpringChainImpl = (initial, links, config, options) => {
250
270
  get config() {
251
271
  return config;
252
272
  },
273
+ getConfig: () => config,
253
274
  get values() {
254
275
  return values;
255
276
  },
277
+ getValues: () => values,
256
278
  get velocities() {
257
279
  return velocities;
258
280
  },
281
+ getVelocities: () => velocities,
259
282
  get state() {
260
283
  return state;
261
284
  },
285
+ getState: () => state,
262
286
  get states() {
263
287
  return states;
264
288
  },
289
+ getStates: () => states,
265
290
  freeze: () => {
266
291
  for (const spring of springs) {
267
292
  spring.freeze();
@@ -296,9 +321,12 @@ var createSpringChain = (initial, links, config, options) => {
296
321
  };
297
322
 
298
323
  // src/createSpringSystem.ts
324
+ var import_mitt3 = __toESM(require("mitt"), 1);
299
325
  var createSpringSystem = () => {
300
326
  const springs = /* @__PURE__ */ new Map();
327
+ const { emit, ...emitterApi } = (0, import_mitt3.default)();
301
328
  return {
329
+ ...emitterApi,
302
330
  createSpring: (initial, config, options) => {
303
331
  const [spring, simulate] = createSpringImpl(initial, config, options);
304
332
  springs.set(spring, simulate);
@@ -319,6 +347,7 @@ var createSpringSystem = () => {
319
347
  for (const simulate of springs.values()) {
320
348
  simulate(delta);
321
349
  }
350
+ emit("simulate:after", delta);
322
351
  }
323
352
  };
324
353
  };
package/dist/index.d.ts CHANGED
@@ -19,10 +19,16 @@ type SpringEmitter = Emitter<{
19
19
  }>;
20
20
  interface Spring extends Omit<SpringEmitter, 'emit'> {
21
21
  target: number;
22
+ readonly getTarget: () => number;
22
23
  config: Readonly<SpringConfig>;
24
+ readonly getConfig: () => Readonly<SpringConfig>;
23
25
  readonly value: number;
26
+ readonly getValue: () => number;
24
27
  readonly velocity: number;
28
+ readonly getVelocity: () => number;
25
29
  readonly state: SpringState;
30
+ readonly getState: () => SpringState;
31
+ readonly jumpTo: (value: number, killVelocity?: boolean) => void;
26
32
  readonly freeze: () => void;
27
33
  readonly unfreeze: () => void;
28
34
  }
@@ -38,20 +44,31 @@ type SpringChainEmitter = Emitter<{
38
44
  }>;
39
45
  interface SpringChain extends Omit<SpringChainEmitter, 'emit'> {
40
46
  target: number;
47
+ readonly getTarget: () => number;
41
48
  links: SpringChainLinkGetter[];
42
49
  config: Readonly<SpringConfig>;
50
+ readonly getConfig: () => Readonly<SpringConfig>;
43
51
  readonly targets: ReadonlyArray<number>;
52
+ readonly getTargets: () => ReadonlyArray<number>;
44
53
  readonly values: ReadonlyArray<number>;
54
+ readonly getValues: () => ReadonlyArray<number>;
45
55
  readonly velocities: ReadonlyArray<number>;
56
+ readonly getVelocities: () => ReadonlyArray<number>;
46
57
  readonly state: SpringState;
58
+ readonly getState: () => SpringState;
47
59
  readonly states: ReadonlyArray<SpringState>;
60
+ readonly getStates: () => ReadonlyArray<SpringState>;
48
61
  readonly freeze: () => void;
49
62
  readonly unfreeze: () => void;
50
63
  }
51
64
  interface StandaloneSpringChain extends SpringChain {
52
65
  readonly simulate: SimulateFn;
53
66
  }
54
- interface SpringSystem {
67
+ type SpringSystemEmitter = Emitter<{
68
+ 'simulate:after': number;
69
+ '*': number;
70
+ }>;
71
+ interface SpringSystem extends Omit<SpringSystemEmitter, 'emit'> {
55
72
  createSpring: (initial: number, config: SpringConfig, options?: SpringOptions) => Spring;
56
73
  createSpringChain: (initial: number, links: SpringChainLinkGetter[], config: SpringConfig, options?: SpringOptions) => SpringChain;
57
74
  cleanup: (spring: Spring | SpringChain) => void;
package/dist/index.js CHANGED
@@ -37,26 +37,38 @@ var createSpringImpl = (initial, config, options) => {
37
37
  },
38
38
  set target(val) {
39
39
  target = val;
40
- if (Math.abs(value - target) > restingDistance) {
40
+ if (state !== "moving" && Math.abs(value - target) > restingDistance) {
41
41
  state = "moving";
42
42
  emit("update:state", state);
43
43
  }
44
44
  },
45
+ getTarget: () => target,
45
46
  get value() {
46
47
  return value;
47
48
  },
49
+ getValue: () => value,
48
50
  get velocity() {
49
51
  return velocity;
50
52
  },
53
+ getVelocity: () => velocity,
51
54
  get state() {
52
55
  return state;
53
56
  },
57
+ getState: () => state,
54
58
  get config() {
55
59
  return config;
56
60
  },
57
61
  set config(value2) {
58
62
  config = value2;
59
63
  },
64
+ getConfig: () => config,
65
+ jumpTo: (_value, killVelocity = true) => {
66
+ value = _value;
67
+ if (killVelocity) {
68
+ velocity = 0;
69
+ }
70
+ state = Math.abs(velocity) > restingVelocity || Math.abs(value - target) > restingDistance ? "moving" : "resting";
71
+ },
60
72
  freeze: () => {
61
73
  state = "frozen";
62
74
  emit("update:state", state);
@@ -66,13 +78,15 @@ var createSpringImpl = (initial, config, options) => {
66
78
  emit("update:state", state);
67
79
  }
68
80
  };
81
+ const tension = -config.tension * 1e-6;
82
+ const friction = -config.friction * 1e-3;
69
83
  const simulate = (deltaTime) => {
70
84
  if (state === "moving") {
71
85
  const iterations = Math.ceil(deltaTime);
72
86
  for (let n = 0; n < iterations; n++) {
73
87
  const previousDelta = value - target;
74
- const springForce = -config.tension * 1e-6 * previousDelta;
75
- const dampingForce = -config.friction * 1e-3 * velocity;
88
+ const springForce = tension * previousDelta;
89
+ const dampingForce = friction * velocity;
76
90
  const acceleration = (springForce + dampingForce) / config.mass;
77
91
  velocity = velocity + acceleration;
78
92
  value = value + velocity;
@@ -203,9 +217,11 @@ var createSpringChainImpl = (initial, links, config, options) => {
203
217
  currentTarget = val;
204
218
  updateChainState(currentTarget, currentLinks);
205
219
  },
220
+ getTarget: () => targets[0],
206
221
  get targets() {
207
222
  return targets;
208
223
  },
224
+ getTargets: () => targets,
209
225
  get links() {
210
226
  return links;
211
227
  },
@@ -216,18 +232,23 @@ var createSpringChainImpl = (initial, links, config, options) => {
216
232
  get config() {
217
233
  return config;
218
234
  },
235
+ getConfig: () => config,
219
236
  get values() {
220
237
  return values;
221
238
  },
239
+ getValues: () => values,
222
240
  get velocities() {
223
241
  return velocities;
224
242
  },
243
+ getVelocities: () => velocities,
225
244
  get state() {
226
245
  return state;
227
246
  },
247
+ getState: () => state,
228
248
  get states() {
229
249
  return states;
230
250
  },
251
+ getStates: () => states,
231
252
  freeze: () => {
232
253
  for (const spring of springs) {
233
254
  spring.freeze();
@@ -262,9 +283,12 @@ var createSpringChain = (initial, links, config, options) => {
262
283
  };
263
284
 
264
285
  // src/createSpringSystem.ts
286
+ import mitt3 from "mitt";
265
287
  var createSpringSystem = () => {
266
288
  const springs = /* @__PURE__ */ new Map();
289
+ const { emit, ...emitterApi } = mitt3();
267
290
  return {
291
+ ...emitterApi,
268
292
  createSpring: (initial, config, options) => {
269
293
  const [spring, simulate] = createSpringImpl(initial, config, options);
270
294
  springs.set(spring, simulate);
@@ -285,6 +309,7 @@ var createSpringSystem = () => {
285
309
  for (const simulate of springs.values()) {
286
310
  simulate(delta);
287
311
  }
312
+ emit("simulate:after", delta);
288
313
  }
289
314
  };
290
315
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Simulate values using spring physics",
5
5
  "author": "Tim Morris <timthewebguy@gmail.com>",
6
6
  "homepage": "https://github.com/tkofh/coily#readme",
@@ -38,12 +38,12 @@
38
38
  "url": "https://github.com/tkofh/coily/issues"
39
39
  },
40
40
  "devDependencies": {
41
- "@vitest/ui": "^0.11.0",
42
- "tsup": "^6.1.3",
43
- "vitest": "^0.16.0"
41
+ "@vitest/ui": "^0.29.8",
42
+ "tsup": "^6.7.0",
43
+ "vitest": "^0.29.8"
44
44
  },
45
45
  "dependencies": {
46
46
  "mitt": "^3.0.0"
47
47
  },
48
- "gitHead": "f2a3ee966126e974e2fd3576bc0968c03c0287ca"
48
+ "gitHead": "143ecefa887bfffe5b5caa29fddcf01b3b3e17c1"
49
49
  }