coily 0.3.0 → 0.4.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.cjs CHANGED
@@ -116,15 +116,13 @@ var createSpringImpl = (initial, config, options) => {
116
116
  emit("update:state", state);
117
117
  }
118
118
  };
119
- const tension = -config.tension * 1e-6;
120
- const friction = -config.friction * 1e-3;
121
119
  const simulate = (deltaTime) => {
122
120
  if (state === "moving") {
123
121
  const iterations = Math.ceil(deltaTime);
124
122
  for (let n = 0; n < iterations; n++) {
125
123
  const previousDelta = value - target;
126
- const springForce = tension * previousDelta;
127
- const dampingForce = friction * velocity;
124
+ const springForce = -config.tension * 1e-6 * previousDelta;
125
+ const dampingForce = -config.friction * 1e-3 * velocity;
128
126
  const acceleration = (springForce + dampingForce) / config.mass;
129
127
  velocity = velocity + acceleration;
130
128
  value = value + velocity;
@@ -321,9 +319,12 @@ var createSpringChain = (initial, links, config, options) => {
321
319
  };
322
320
 
323
321
  // src/createSpringSystem.ts
322
+ var import_mitt3 = __toESM(require("mitt"), 1);
324
323
  var createSpringSystem = () => {
325
324
  const springs = /* @__PURE__ */ new Map();
325
+ const { emit, ...emitterApi } = (0, import_mitt3.default)();
326
326
  return {
327
+ ...emitterApi,
327
328
  createSpring: (initial, config, options) => {
328
329
  const [spring, simulate] = createSpringImpl(initial, config, options);
329
330
  springs.set(spring, simulate);
@@ -344,6 +345,7 @@ var createSpringSystem = () => {
344
345
  for (const simulate of springs.values()) {
345
346
  simulate(delta);
346
347
  }
348
+ emit("simulate:after", delta);
347
349
  }
348
350
  };
349
351
  };
package/dist/index.d.ts CHANGED
@@ -64,7 +64,11 @@ interface SpringChain extends Omit<SpringChainEmitter, 'emit'> {
64
64
  interface StandaloneSpringChain extends SpringChain {
65
65
  readonly simulate: SimulateFn;
66
66
  }
67
- interface SpringSystem {
67
+ type SpringSystemEmitter = Emitter<{
68
+ 'simulate:after': number;
69
+ '*': number;
70
+ }>;
71
+ interface SpringSystem extends Omit<SpringSystemEmitter, 'emit'> {
68
72
  createSpring: (initial: number, config: SpringConfig, options?: SpringOptions) => Spring;
69
73
  createSpringChain: (initial: number, links: SpringChainLinkGetter[], config: SpringConfig, options?: SpringOptions) => SpringChain;
70
74
  cleanup: (spring: Spring | SpringChain) => void;
package/dist/index.js CHANGED
@@ -78,15 +78,13 @@ var createSpringImpl = (initial, config, options) => {
78
78
  emit("update:state", state);
79
79
  }
80
80
  };
81
- const tension = -config.tension * 1e-6;
82
- const friction = -config.friction * 1e-3;
83
81
  const simulate = (deltaTime) => {
84
82
  if (state === "moving") {
85
83
  const iterations = Math.ceil(deltaTime);
86
84
  for (let n = 0; n < iterations; n++) {
87
85
  const previousDelta = value - target;
88
- const springForce = tension * previousDelta;
89
- const dampingForce = friction * velocity;
86
+ const springForce = -config.tension * 1e-6 * previousDelta;
87
+ const dampingForce = -config.friction * 1e-3 * velocity;
90
88
  const acceleration = (springForce + dampingForce) / config.mass;
91
89
  velocity = velocity + acceleration;
92
90
  value = value + velocity;
@@ -283,9 +281,12 @@ var createSpringChain = (initial, links, config, options) => {
283
281
  };
284
282
 
285
283
  // src/createSpringSystem.ts
284
+ import mitt3 from "mitt";
286
285
  var createSpringSystem = () => {
287
286
  const springs = /* @__PURE__ */ new Map();
287
+ const { emit, ...emitterApi } = mitt3();
288
288
  return {
289
+ ...emitterApi,
289
290
  createSpring: (initial, config, options) => {
290
291
  const [spring, simulate] = createSpringImpl(initial, config, options);
291
292
  springs.set(spring, simulate);
@@ -306,6 +307,7 @@ var createSpringSystem = () => {
306
307
  for (const simulate of springs.values()) {
307
308
  simulate(delta);
308
309
  }
310
+ emit("simulate:after", delta);
309
311
  }
310
312
  };
311
313
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
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.29.2",
42
- "tsup": "^6.6.3",
43
- "vitest": "^0.29.2"
41
+ "@vitest/ui": "^0.30.0",
42
+ "tsup": "^6.7.0",
43
+ "vitest": "^0.30.0"
44
44
  },
45
45
  "dependencies": {
46
46
  "mitt": "^3.0.0"
47
47
  },
48
- "gitHead": "f6f52d538e85578e1d734a729e4161f54c5b900f"
48
+ "gitHead": "f9abbda0b74187ce13a07638458df845c5b51ec6"
49
49
  }