coily 0.2.0 → 0.3.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 +28 -3
- package/dist/index.d.ts +13 -0
- package/dist/index.js +24 -3
- package/package.json +5 -5
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 =
|
|
109
|
-
const dampingForce =
|
|
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();
|
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,13 +44,20 @@ 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
|
}
|
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 =
|
|
75
|
-
const dampingForce =
|
|
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();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "coily",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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.
|
|
42
|
-
"tsup": "^6.
|
|
43
|
-
"vitest": "^0.
|
|
41
|
+
"@vitest/ui": "^0.29.2",
|
|
42
|
+
"tsup": "^6.6.3",
|
|
43
|
+
"vitest": "^0.29.2"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"mitt": "^3.0.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "f6f52d538e85578e1d734a729e4161f54c5b900f"
|
|
49
49
|
}
|