coily 0.4.1 → 0.5.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.
@@ -0,0 +1,86 @@
1
+ interface SolverOptions {
2
+ mass: number;
3
+ tension: number;
4
+ damping: number;
5
+ position: number;
6
+ velocity: number;
7
+ precision: number;
8
+ }
9
+ declare class Solver {
10
+ #private;
11
+ constructor(options: SolverOptions);
12
+ get mass(): number;
13
+ set mass(value: number);
14
+ get tension(): number;
15
+ set tension(value: number);
16
+ get damping(): number;
17
+ set damping(value: number);
18
+ get position(): number;
19
+ set position(value: number);
20
+ get velocity(): number;
21
+ set velocity(value: number);
22
+ get precision(): number;
23
+ set precision(value: number);
24
+ get resting(): boolean;
25
+ get naturalFrequency(): number;
26
+ get criticalDamping(): number;
27
+ get dampingRatio(): number;
28
+ tick(dt: number): void;
29
+ onUpdate(callback: () => void): () => void;
30
+ }
31
+
32
+ type TickCallback = (delta: number) => void;
33
+ declare class Scheduler {
34
+ #private;
35
+ tick(delta: number): void;
36
+ add(solver: Solver): void;
37
+ remove(solver: Solver): void;
38
+ has(solver: Solver): boolean;
39
+ once(callback: TickCallback): void;
40
+ }
41
+
42
+ interface SpringOptions$1 {
43
+ mass: number;
44
+ tension: number;
45
+ damping: number;
46
+ target: number;
47
+ value?: number;
48
+ precsiion?: number;
49
+ }
50
+ declare class Spring {
51
+ #private;
52
+ constructor(scheduler: Scheduler, options: SpringOptions$1);
53
+ get target(): number;
54
+ set target(value: number);
55
+ get value(): number;
56
+ set value(value: number);
57
+ get velocity(): number;
58
+ set velocity(value: number);
59
+ get mass(): number;
60
+ set mass(value: number);
61
+ get tension(): number;
62
+ set tension(value: number);
63
+ get damping(): number;
64
+ set damping(value: number);
65
+ get precision(): number;
66
+ set precision(value: number);
67
+ get resting(): boolean;
68
+ onUpdate(callback: () => void): () => void;
69
+ }
70
+
71
+ interface SpringOptions {
72
+ mass: number;
73
+ tension: number;
74
+ damping: number;
75
+ target?: number;
76
+ value?: number;
77
+ precision?: number;
78
+ }
79
+ declare class SpringSystem {
80
+ #private;
81
+ constructor();
82
+ createSpring(options: SpringOptions): Spring;
83
+ tick(dt: number): void;
84
+ }
85
+
86
+ export { SpringSystem as S, Spring as a };
@@ -0,0 +1,86 @@
1
+ interface SolverOptions {
2
+ mass: number;
3
+ tension: number;
4
+ damping: number;
5
+ position: number;
6
+ velocity: number;
7
+ precision: number;
8
+ }
9
+ declare class Solver {
10
+ #private;
11
+ constructor(options: SolverOptions);
12
+ get mass(): number;
13
+ set mass(value: number);
14
+ get tension(): number;
15
+ set tension(value: number);
16
+ get damping(): number;
17
+ set damping(value: number);
18
+ get position(): number;
19
+ set position(value: number);
20
+ get velocity(): number;
21
+ set velocity(value: number);
22
+ get precision(): number;
23
+ set precision(value: number);
24
+ get resting(): boolean;
25
+ get naturalFrequency(): number;
26
+ get criticalDamping(): number;
27
+ get dampingRatio(): number;
28
+ tick(dt: number): void;
29
+ onUpdate(callback: () => void): () => void;
30
+ }
31
+
32
+ type TickCallback = (delta: number) => void;
33
+ declare class Scheduler {
34
+ #private;
35
+ tick(delta: number): void;
36
+ add(solver: Solver): void;
37
+ remove(solver: Solver): void;
38
+ has(solver: Solver): boolean;
39
+ once(callback: TickCallback): void;
40
+ }
41
+
42
+ interface SpringOptions$1 {
43
+ mass: number;
44
+ tension: number;
45
+ damping: number;
46
+ target: number;
47
+ value?: number;
48
+ precsiion?: number;
49
+ }
50
+ declare class Spring {
51
+ #private;
52
+ constructor(scheduler: Scheduler, options: SpringOptions$1);
53
+ get target(): number;
54
+ set target(value: number);
55
+ get value(): number;
56
+ set value(value: number);
57
+ get velocity(): number;
58
+ set velocity(value: number);
59
+ get mass(): number;
60
+ set mass(value: number);
61
+ get tension(): number;
62
+ set tension(value: number);
63
+ get damping(): number;
64
+ set damping(value: number);
65
+ get precision(): number;
66
+ set precision(value: number);
67
+ get resting(): boolean;
68
+ onUpdate(callback: () => void): () => void;
69
+ }
70
+
71
+ interface SpringOptions {
72
+ mass: number;
73
+ tension: number;
74
+ damping: number;
75
+ target?: number;
76
+ value?: number;
77
+ precision?: number;
78
+ }
79
+ declare class SpringSystem {
80
+ #private;
81
+ constructor();
82
+ createSpring(options: SpringOptions): Spring;
83
+ tick(dt: number): void;
84
+ }
85
+
86
+ export { SpringSystem as S, Spring as a };
@@ -0,0 +1,86 @@
1
+ interface SolverOptions {
2
+ mass: number;
3
+ tension: number;
4
+ damping: number;
5
+ position: number;
6
+ velocity: number;
7
+ precision: number;
8
+ }
9
+ declare class Solver {
10
+ #private;
11
+ constructor(options: SolverOptions);
12
+ get mass(): number;
13
+ set mass(value: number);
14
+ get tension(): number;
15
+ set tension(value: number);
16
+ get damping(): number;
17
+ set damping(value: number);
18
+ get position(): number;
19
+ set position(value: number);
20
+ get velocity(): number;
21
+ set velocity(value: number);
22
+ get precision(): number;
23
+ set precision(value: number);
24
+ get resting(): boolean;
25
+ get naturalFrequency(): number;
26
+ get criticalDamping(): number;
27
+ get dampingRatio(): number;
28
+ tick(dt: number): void;
29
+ onUpdate(callback: () => void): () => void;
30
+ }
31
+
32
+ type TickCallback = (delta: number) => void;
33
+ declare class Scheduler {
34
+ #private;
35
+ tick(delta: number): void;
36
+ add(solver: Solver): void;
37
+ remove(solver: Solver): void;
38
+ has(solver: Solver): boolean;
39
+ once(callback: TickCallback): void;
40
+ }
41
+
42
+ interface SpringOptions$1 {
43
+ mass: number;
44
+ tension: number;
45
+ damping: number;
46
+ target: number;
47
+ value?: number;
48
+ precsiion?: number;
49
+ }
50
+ declare class Spring {
51
+ #private;
52
+ constructor(scheduler: Scheduler, options: SpringOptions$1);
53
+ get target(): number;
54
+ set target(value: number);
55
+ get value(): number;
56
+ set value(value: number);
57
+ get velocity(): number;
58
+ set velocity(value: number);
59
+ get mass(): number;
60
+ set mass(value: number);
61
+ get tension(): number;
62
+ set tension(value: number);
63
+ get damping(): number;
64
+ set damping(value: number);
65
+ get precision(): number;
66
+ set precision(value: number);
67
+ get resting(): boolean;
68
+ onUpdate(callback: () => void): () => void;
69
+ }
70
+
71
+ interface SpringOptions {
72
+ mass: number;
73
+ tension: number;
74
+ damping: number;
75
+ target?: number;
76
+ value?: number;
77
+ precision?: number;
78
+ }
79
+ declare class SpringSystem {
80
+ #private;
81
+ constructor();
82
+ createSpring(options: SpringOptions): Spring;
83
+ tick(dt: number): void;
84
+ }
85
+
86
+ export { SpringSystem as S, Spring as a };
package/package.json CHANGED
@@ -1,20 +1,25 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.4.1",
3
+ "version": "0.5.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",
7
7
  "license": "MIT",
8
8
  "sideEffects": false,
9
9
  "type": "module",
10
- "main": "./dist/index.cjs",
11
- "module": "./dist/index.js",
12
- "types": "./dist/index.d.ts",
10
+ "main": "./dist/api.cjs",
11
+ "module": "./dist/api.mjs",
12
+ "types": "./dist/api.d.ts",
13
13
  "exports": {
14
14
  ".": {
15
- "require": "./dist/index.cjs",
16
- "import": "./dist/index.js",
17
- "types": "./dist/index.d.ts"
15
+ "require": "./dist/api.cjs",
16
+ "import": "./dist/api.mjs",
17
+ "types": "./dist/api.d.ts"
18
+ },
19
+ "./loop": {
20
+ "require": "./dist/loop.cjs",
21
+ "import": "./dist/loop.mjs",
22
+ "types": "./dist/loop.d.ts"
18
23
  }
19
24
  },
20
25
  "files": [
@@ -28,22 +33,20 @@
28
33
  "url": "git+https://github.com/tkofh/coily.git"
29
34
  },
30
35
  "scripts": {
31
- "build": "tsup",
32
- "test": "vitest run",
33
- "test:watch": "vitest watch",
34
- "test:ui": "vitest --ui",
35
- "prepublish": "pnpm test && pnpm build"
36
+ "build": "unbuild",
37
+ "prepublish": "bun run build"
36
38
  },
37
39
  "bugs": {
38
40
  "url": "https://github.com/tkofh/coily/issues"
39
41
  },
40
42
  "devDependencies": {
41
- "@vitest/ui": "^0.30.0",
42
- "tsup": "^6.7.0",
43
- "vitest": "^0.30.0"
43
+ "@vitest/coverage-v8": "^1.4.0",
44
+ "@vitest/ui": "^1.4.0",
45
+ "unbuild": "^2.0.0",
46
+ "vitest": "^1.3.1"
44
47
  },
48
+ "gitHead": "96019bee6135f93006c3fd52d23846afd6271c66",
45
49
  "dependencies": {
46
- "mitt": "^3.0.0"
47
- },
48
- "gitHead": "f9abbda0b74187ce13a07638458df845c5b51ec6"
50
+ "tickloop": "^0.5.0"
51
+ }
49
52
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Tim Morris
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/dist/index.cjs DELETED
@@ -1,357 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
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.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- createSpring: () => createSpring,
34
- createSpringChain: () => createSpringChain,
35
- createSpringSystem: () => createSpringSystem
36
- });
37
- module.exports = __toCommonJS(src_exports);
38
-
39
- // src/lib/createSpring.ts
40
- var import_mitt = __toESM(require("mitt"), 1);
41
- var getArrivalFunction = (behavior) => {
42
- let result;
43
- if (typeof behavior === "function") {
44
- result = behavior;
45
- } else {
46
- let scalar;
47
- if (typeof behavior === "number") {
48
- scalar = behavior;
49
- } else if (behavior === "bounce") {
50
- scalar = -1;
51
- } else if (behavior === "clamp") {
52
- scalar = 0;
53
- } else if (behavior === "none") {
54
- scalar = 1;
55
- }
56
- result = (velocity) => velocity * scalar;
57
- }
58
- return result;
59
- };
60
- var createSpringImpl = (initial, config, options) => {
61
- var _a, _b, _c;
62
- let state = "resting";
63
- let velocity = 0;
64
- let target = initial;
65
- let value = initial;
66
- const restingDistance = Math.max((_a = options == null ? void 0 : options.restingDistance) != null ? _a : 1e-4, 0);
67
- const restingVelocity = Math.max((_b = options == null ? void 0 : options.restingVelocity) != null ? _b : 1e-4, 0);
68
- const arrivalFunction = getArrivalFunction((_c = options == null ? void 0 : options.arrivalBehavior) != null ? _c : 1);
69
- const emitter = (0, import_mitt.default)();
70
- const { emit, ...emitterApi } = emitter;
71
- const spring = {
72
- ...emitterApi,
73
- get target() {
74
- return target;
75
- },
76
- set target(val) {
77
- target = val;
78
- if (state !== "moving" && Math.abs(value - target) > restingDistance) {
79
- state = "moving";
80
- emit("update:state", state);
81
- }
82
- },
83
- getTarget: () => target,
84
- get value() {
85
- return value;
86
- },
87
- getValue: () => value,
88
- get velocity() {
89
- return velocity;
90
- },
91
- getVelocity: () => velocity,
92
- get state() {
93
- return state;
94
- },
95
- getState: () => state,
96
- get config() {
97
- return config;
98
- },
99
- set config(value2) {
100
- config = value2;
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
- },
110
- freeze: () => {
111
- state = "frozen";
112
- emit("update:state", state);
113
- },
114
- unfreeze: () => {
115
- state = Math.abs(velocity) > restingVelocity || Math.abs(value - target) > restingDistance ? "moving" : "resting";
116
- emit("update:state", state);
117
- }
118
- };
119
- const simulate = (deltaTime) => {
120
- if (state === "moving") {
121
- const iterations = Math.ceil(deltaTime);
122
- for (let n = 0; n < iterations; n++) {
123
- const previousDelta = value - target;
124
- const springForce = -config.tension * 1e-6 * previousDelta;
125
- const dampingForce = -config.friction * 1e-3 * velocity;
126
- const acceleration = (springForce + dampingForce) / config.mass;
127
- velocity = velocity + acceleration;
128
- value = value + velocity;
129
- const delta = value - target;
130
- const absDelta = Math.abs(delta);
131
- if (Math.sign(delta) !== Math.sign(previousDelta)) {
132
- velocity = arrivalFunction(velocity * 1e3) * 1e-3;
133
- value = target + velocity * (absDelta / (absDelta + Math.abs(previousDelta)));
134
- }
135
- if (absDelta < restingDistance && Math.abs(velocity) < restingVelocity) {
136
- value = target;
137
- state = "resting";
138
- emit("update:state", state);
139
- break;
140
- }
141
- }
142
- emit("update:value", value);
143
- }
144
- };
145
- return [spring, simulate];
146
- };
147
-
148
- // src/lib/createSpringChain.ts
149
- var import_mitt2 = __toESM(require("mitt"), 1);
150
- var createSpringChainImpl = (initial, links, config, options) => {
151
- const simulateFns = [];
152
- const springs = [];
153
- const targets = [];
154
- const values = [];
155
- const velocities = [];
156
- const states = [];
157
- let currentTarget = initial;
158
- let currentLinks = links;
159
- let state = "resting";
160
- const updateChainState = (target, links2) => {
161
- if (springs.length === 0) {
162
- const [spring, simulate2] = createSpringImpl(target, config, options);
163
- springs.push(spring);
164
- simulateFns.push(simulate2);
165
- targets.push(spring.target);
166
- values.push(spring.value);
167
- velocities.push(spring.velocity);
168
- states.push(spring.state);
169
- } else {
170
- springs[0].target = target;
171
- targets[0] = target;
172
- states[0] = springs[0].state;
173
- if (state === "resting" && springs[0].state === "moving") {
174
- state = "moving";
175
- }
176
- }
177
- for (const [index, link] of links2.entries()) {
178
- const previousSpring = springs[index];
179
- if (springs[index + 1]) {
180
- const spring = springs[index + 1];
181
- if (typeof link === "number") {
182
- spring.target = previousSpring.value + link;
183
- } else {
184
- spring.target = link(
185
- previousSpring.value,
186
- previousSpring.target,
187
- spring.value,
188
- spring.target
189
- );
190
- }
191
- targets[index + 1] = spring.target;
192
- states[index + 1] = spring.state;
193
- if (state === "resting" && spring.state === "moving") {
194
- state = "moving";
195
- }
196
- } else {
197
- const [spring, simulate2] = createSpringImpl(
198
- typeof link === "number" ? previousSpring.value + link : link(previousSpring.value, previousSpring.target),
199
- config,
200
- options
201
- );
202
- springs.push(spring);
203
- simulateFns.push(simulate2);
204
- targets[index + 1] = spring.target;
205
- values[index + 1] = spring.value;
206
- velocities[index + 1] = spring.velocity;
207
- states[index + 1] = spring.state;
208
- }
209
- }
210
- const cutoff = springs.length + 1;
211
- if (springs.length > cutoff) {
212
- const deleteCount = springs.length - cutoff;
213
- springs.splice(cutoff, deleteCount);
214
- simulateFns.splice(cutoff, deleteCount);
215
- targets.splice(cutoff, deleteCount);
216
- values.splice(cutoff, deleteCount);
217
- velocities.splice(cutoff, deleteCount);
218
- states.splice(cutoff, deleteCount);
219
- }
220
- };
221
- updateChainState(currentTarget, currentLinks);
222
- const emitter = (0, import_mitt2.default)();
223
- const { emit, ...emitterApi } = emitter;
224
- const simulate = (delta) => {
225
- if (state === "moving") {
226
- let moving = false;
227
- for (let i = 0; i < springs.length; i++) {
228
- simulateFns[i](delta);
229
- if (i < springs.length - 1) {
230
- const link = links[i];
231
- springs[i + 1].target = typeof link === "number" ? springs[i].value + link : link(springs[i].value, springs[i].target, springs[i + 1].value);
232
- }
233
- targets[i] = springs[i].target;
234
- values[i] = springs[i].value;
235
- velocities[i] = springs[i].velocity;
236
- states[i] = springs[i].state;
237
- moving = moving || springs[i].state === "moving";
238
- }
239
- emit("update:value", values);
240
- if (!moving) {
241
- state = "resting";
242
- emit("update:state", state);
243
- }
244
- }
245
- };
246
- return [
247
- {
248
- ...emitterApi,
249
- get target() {
250
- return targets[0];
251
- },
252
- set target(val) {
253
- currentTarget = val;
254
- updateChainState(currentTarget, currentLinks);
255
- },
256
- getTarget: () => targets[0],
257
- get targets() {
258
- return targets;
259
- },
260
- getTargets: () => targets,
261
- get links() {
262
- return links;
263
- },
264
- set links(val) {
265
- currentLinks = val;
266
- updateChainState(currentTarget, currentLinks);
267
- },
268
- get config() {
269
- return config;
270
- },
271
- getConfig: () => config,
272
- get values() {
273
- return values;
274
- },
275
- getValues: () => values,
276
- get velocities() {
277
- return velocities;
278
- },
279
- getVelocities: () => velocities,
280
- get state() {
281
- return state;
282
- },
283
- getState: () => state,
284
- get states() {
285
- return states;
286
- },
287
- getStates: () => states,
288
- freeze: () => {
289
- for (const spring of springs) {
290
- spring.freeze();
291
- }
292
- state = "frozen";
293
- emit("update:state", state);
294
- },
295
- unfreeze: () => {
296
- let moving = false;
297
- for (const spring of springs) {
298
- spring.unfreeze();
299
- moving = moving || spring.state === "moving";
300
- }
301
- state = moving ? "moving" : "resting";
302
- emit("update:state", state);
303
- }
304
- },
305
- simulate
306
- ];
307
- };
308
-
309
- // src/createSpring.ts
310
- var createSpring = (initial, config, options) => {
311
- const [spring, simulate] = createSpringImpl(initial, config, options);
312
- return Object.assign(spring, { simulate });
313
- };
314
-
315
- // src/createSpringChain.ts
316
- var createSpringChain = (initial, links, config, options) => {
317
- const [chain, simulate] = createSpringChainImpl(initial, links, config, options);
318
- return Object.assign(chain, { simulate });
319
- };
320
-
321
- // src/createSpringSystem.ts
322
- var import_mitt3 = __toESM(require("mitt"), 1);
323
- var createSpringSystem = () => {
324
- const springs = /* @__PURE__ */ new Map();
325
- const { emit, ...emitterApi } = (0, import_mitt3.default)();
326
- return {
327
- ...emitterApi,
328
- createSpring: (initial, config, options) => {
329
- const [spring, simulate] = createSpringImpl(initial, config, options);
330
- springs.set(spring, simulate);
331
- return spring;
332
- },
333
- createSpringChain: (initial, links, config, options) => {
334
- const [chain, simulate] = createSpringChainImpl(initial, links, config, options);
335
- springs.set(chain, simulate);
336
- return chain;
337
- },
338
- cleanup: (spring) => {
339
- springs.delete(spring);
340
- spring.off("*");
341
- spring.off("update:state");
342
- spring.off("update:value");
343
- },
344
- simulate: (delta) => {
345
- for (const simulate of springs.values()) {
346
- simulate(delta);
347
- }
348
- emit("simulate:after", delta);
349
- }
350
- };
351
- };
352
- // Annotate the CommonJS export names for ESM import in node:
353
- 0 && (module.exports = {
354
- createSpring,
355
- createSpringChain,
356
- createSpringSystem
357
- });