@weasel-js/simulation 1.9.4 → 1.10.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/README.md +1 -2
- package/dist/{chunk-WKNSC4F6.js → chunk-RA6U67OH.js} +2 -2
- package/dist/chunk-RA6U67OH.js.map +1 -0
- package/dist/index.d.ts +3 -8
- package/dist/index.js +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +1 -1
- package/dist/{types-DMxo2jGG.d.ts → types-CmLrFukG.d.ts} +6 -14
- package/package.json +2 -2
- package/dist/chunk-WKNSC4F6.js.map +0 -1
package/README.md
CHANGED
|
@@ -20,8 +20,7 @@ once — a layout pass, a pre-warm — runs it to settle and reads the answer.
|
|
|
20
20
|
|
|
21
21
|
Forces are pluggable functions matching **d3-force's protocol**, so d3-force's
|
|
22
22
|
bundled forces (`forceManyBody`, `forceLink`, `forceCenter`, …) work here
|
|
23
|
-
without translation.
|
|
24
|
-
force signature.
|
|
23
|
+
without translation. Don't invent a parallel force signature.
|
|
25
24
|
|
|
26
25
|
## The kit doesn't write to your scene
|
|
27
26
|
|
|
@@ -102,5 +102,5 @@ function createSimulation(opts) {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
export { DEFAULT_ALPHA_DECAY, DEFAULT_ALPHA_MIN, DEFAULT_VELOCITY_DECAY, createSimulation };
|
|
105
|
-
//# sourceMappingURL=chunk-
|
|
106
|
-
//# sourceMappingURL=chunk-
|
|
105
|
+
//# sourceMappingURL=chunk-RA6U67OH.js.map
|
|
106
|
+
//# sourceMappingURL=chunk-RA6U67OH.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/createSimulation.ts"],"names":[],"mappings":";AA2GO,IAAM,sBAAsB,CAAA,GAAI,IAAA,CAAK,GAAA,CAAI,IAAA,EAAO,IAAI,GAAG;AAEvD,IAAM,iBAAA,GAAoB;AAG1B,IAAM,sBAAA,GAAyB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AC1C,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,SAAA,GAAY;AACV,MAAA,OAAO,KAAA,GAAQ,YAAY,WAAA,KAAgB,CAAA;AAAA,IAC7C;AAAA,GACF;AAEA,EAAA,OAAO,IAAA;AACT","file":"chunk-RA6U67OH.js","sourcesContent":["/**\n * Matches d3-force's node shape exactly so d3-force's bundled forces work\n * without translation. Consumers extend this with additional fields.\n */\nexport interface SimulationNode {\n x: number;\n y: number;\n vx?: number;\n vy?: number;\n /** Pin x to this value when non-null. The integrator skips x/vx for the node. */\n fx?: number | null;\n fy?: number | null;\n /** Array index, written by `initialize`. Some forces (e.g. forceLink) rely on it. */\n index?: number;\n}\n\n/**\n * Force protocol — contract-compatible with d3-force. A force is a function\n * called once per tick with the current alpha; it mutates `vx`/`vy` on nodes.\n * Optional `initialize` lets the force compute setup (link indices, etc.).\n */\nexport interface SimulationForce<TNode extends SimulationNode = SimulationNode> {\n (alpha: number): void;\n initialize?(nodes: TNode[], random?: () => number): void;\n}\n\n/** Options for `useSimulation`: everything {@link SimulationOptions} carries,\n * plus the loop's own callbacks and clock. */\nexport interface UseSimulationOptions<TNode extends SimulationNode>\n extends SimulationOptions<TNode> {\n /** Fired after each integration step. */\n onTick?: (nodes: TNode[]) => void;\n /** Fired once when the sim settles (alpha < alphaMin && alphaTarget === 0). */\n onEnd?: () => void;\n\n // Test injection (mirrors useAnimator):\n /** Optional rAF injection. Defaults to window.requestAnimationFrame. */\n requestFrame?: (cb: (t: number) => void) => number;\n cancelFrame?: (handle: number) => void;\n}\n\n/**\n * A force simulation over a set of nodes, with no clock of its own.\n *\n * Each tick applies every force and then integrates velocities, scaled by\n * `alpha` — a temperature that decays toward `alphaTarget` so the layout\n * settles. Raise `alphaTarget` to reheat it.\n *\n * Nodes are mutated in place; the simulation holds the array, not a copy.\n * {@link Simulation} adds the frame loop.\n */\nexport interface SimulationCore<TNode extends SimulationNode> {\n readonly nodes: TNode[];\n\n setNodes(nodes: TNode[]): this;\n setForces(forces: SimulationForce<TNode>[]): this;\n\n alpha(): number;\n alpha(value: number): this;\n alphaTarget(): number;\n alphaTarget(value: number): this;\n alphaDecay(): number;\n alphaDecay(value: number): this;\n alphaMin(): number;\n alphaMin(value: number): this;\n velocityDecay(): number;\n velocityDecay(value: number): this;\n\n /** Run the tick body synchronously `iterations` times. Does NOT fire\n * onTick or onEnd (matches d3-force). */\n tick(iterations?: number): this;\n\n isSettled(): boolean;\n}\n\n/** A {@link SimulationCore} running on a frame loop. */\nexport interface Simulation<TNode extends SimulationNode> extends SimulationCore<TNode> {\n /** Resume the RAF loop. If alpha < alphaMin, reset alpha to 1. */\n restart(): this;\n /** Cancel the RAF loop. Does not change alpha. */\n stop(): this;\n}\n\n/** What {@link SimulationCore} needs: the nodes, the forces acting on them,\n * and the cooling schedule. {@link UseSimulationOptions} adds the loop's. */\nexport interface SimulationOptions<TNode extends SimulationNode> {\n /** Mutable array of nodes. Kit + forces mutate vx/vy/x/y in place. */\n nodes: TNode[];\n /** Forces applied per tick. Each may implement `initialize` for setup. */\n forces?: SimulationForce<TNode>[];\n\n /** Initial alpha. Default 1. */\n alpha?: number;\n /** Settle threshold. When alpha drops below and alphaTarget is 0, onEnd fires. Default 0.001. */\n alphaMin?: number;\n /** Per-tick decay rate toward alphaTarget. Default 1 - 0.001^(1/300) ≈ 0.0228. */\n alphaDecay?: number;\n /** Target alpha. Set above alphaMin to keep sim warm (e.g. during drag). Default 0. */\n alphaTarget?: number;\n /** Friction multiplier on velocity per tick: vx *= (1 - velocityDecay). Default 0.4. */\n velocityDecay?: number;\n /** Handed to each force's `initialize`. Default `Math.random`; pass a seeded\n * source to make a run reproducible. */\n random?: () => number;\n}\n\n/** Default alpha decay: such that alpha drops from 1 to alphaMin (0.001) in 300 ticks. */\nexport const DEFAULT_ALPHA_DECAY = 1 - Math.pow(0.001, 1 / 300);\n/** Alpha below which a cooling simulation is considered settled. */\nexport const DEFAULT_ALPHA_MIN = 0.001;\n/** Fraction of velocity shed each tick — friction. Higher settles sooner and\n * overshoots less. */\nexport const DEFAULT_VELOCITY_DECAY = 0.4;\n","/**\n * The integrator, with no clock attached.\n *\n * Velocity-Verlet over a pluggable force list, contract-compatible with\n * d3-force so d3's own bundled forces drop in untranslated. Nothing here\n * advances time: `tick()` is the only thing that moves a node, and the caller\n * decides how often to call it.\n */\nimport {\n DEFAULT_ALPHA_DECAY,\n DEFAULT_ALPHA_MIN,\n DEFAULT_VELOCITY_DECAY,\n type SimulationCore,\n type SimulationForce,\n type SimulationNode,\n type SimulationOptions,\n} from './types';\n\n/**\n * A d3-force-compatible simulation with no clock: nothing moves until `tick()`.\n * Nodes are mutated in place, and primed with `index`, `vx` and `vy` on creation.\n */\nexport function createSimulation<TNode extends SimulationNode>(\n opts: SimulationOptions<TNode>,\n): SimulationCore<TNode> {\n let nodes = opts.nodes;\n let forces = opts.forces ?? [];\n let alpha = opts.alpha ?? 1;\n let alphaMin = opts.alphaMin ?? DEFAULT_ALPHA_MIN;\n let alphaDecay = opts.alphaDecay ?? DEFAULT_ALPHA_DECAY;\n let alphaTarget = opts.alphaTarget ?? 0;\n let velocityDecay = opts.velocityDecay ?? DEFAULT_VELOCITY_DECAY;\n const random = opts.random ?? Math.random;\n\n /** `index` is what `forceLink` and friends address nodes by, and a d3 force\n * does `n.vx += …` without checking, so both must exist before the first\n * tick or the first tick produces NaN. */\n function prime(list: TNode[]): void {\n list.forEach((n, i) => {\n n.index = i;\n if (n.vx == null) n.vx = 0;\n if (n.vy == null) n.vy = 0;\n });\n }\n\n prime(nodes);\n for (const f of forces) f.initialize?.(nodes, random);\n\n function step(): void {\n alpha += (alphaTarget - alpha) * alphaDecay;\n for (const f of forces) f(alpha);\n\n const friction = 1 - velocityDecay;\n for (const n of nodes) {\n if (n.fx == null) {\n n.vx = (n.vx ?? 0) * friction;\n // A non-finite velocity — force overflow, or two nodes exactly\n // coincident — would reach the renderer as geometry and cost\n // pathological tessellation work without throwing anywhere.\n if (!Number.isFinite(n.vx)) n.vx = 0;\n n.x += n.vx;\n if (!Number.isFinite(n.x)) n.x = 0;\n } else {\n n.x = n.fx;\n n.vx = 0;\n }\n if (n.fy == null) {\n n.vy = (n.vy ?? 0) * friction;\n if (!Number.isFinite(n.vy)) n.vy = 0;\n n.y += n.vy;\n if (!Number.isFinite(n.y)) n.y = 0;\n } else {\n n.y = n.fy;\n n.vy = 0;\n }\n }\n }\n\n /* eslint-disable @typescript-eslint/no-explicit-any */\n const core: any = {\n get nodes() {\n return nodes;\n },\n setNodes(next: TNode[]) {\n nodes = next;\n prime(nodes);\n for (const f of forces) f.initialize?.(nodes, random);\n return core;\n },\n setForces(next: SimulationForce<TNode>[]) {\n const old = forces;\n forces = next;\n for (const f of next) {\n if (!old.includes(f)) f.initialize?.(nodes, random);\n }\n return core;\n },\n alpha(value?: number) {\n if (value === undefined) return alpha;\n alpha = value;\n return core;\n },\n alphaTarget(value?: number) {\n if (value === undefined) return alphaTarget;\n alphaTarget = value;\n return core;\n },\n alphaDecay(value?: number) {\n if (value === undefined) return alphaDecay;\n alphaDecay = value;\n return core;\n },\n alphaMin(value?: number) {\n if (value === undefined) return alphaMin;\n alphaMin = value;\n return core;\n },\n velocityDecay(value?: number) {\n if (value === undefined) return velocityDecay;\n velocityDecay = value;\n return core;\n },\n tick(iterations: number = 1) {\n for (let i = 0; i < iterations; i++) step();\n return core;\n },\n isSettled() {\n return alpha < alphaMin && alphaTarget === 0;\n },\n };\n /* eslint-enable @typescript-eslint/no-explicit-any */\n return core as SimulationCore<TNode>;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as SimulationNode, a as SimulationOptions, b as SimulationCore } from './types-
|
|
2
|
-
export { D as DEFAULT_ALPHA_DECAY, c as DEFAULT_ALPHA_MIN, d as DEFAULT_VELOCITY_DECAY, e as Simulation, f as SimulationForce, U as UseSimulationOptions } from './types-
|
|
1
|
+
import { S as SimulationNode, a as SimulationOptions, b as SimulationCore } from './types-CmLrFukG.js';
|
|
2
|
+
export { D as DEFAULT_ALPHA_DECAY, c as DEFAULT_ALPHA_MIN, d as DEFAULT_VELOCITY_DECAY, e as Simulation, f as SimulationForce, U as UseSimulationOptions } from './types-CmLrFukG.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The integrator, with no clock attached.
|
|
@@ -7,12 +7,7 @@ export { D as DEFAULT_ALPHA_DECAY, c as DEFAULT_ALPHA_MIN, d as DEFAULT_VELOCITY
|
|
|
7
7
|
* Velocity-Verlet over a pluggable force list, contract-compatible with
|
|
8
8
|
* d3-force so d3's own bundled forces drop in untranslated. Nothing here
|
|
9
9
|
* advances time: `tick()` is the only thing that moves a node, and the caller
|
|
10
|
-
* decides how often to call it.
|
|
11
|
-
* layout pass calls it a few hundred times in one go and reads the result.
|
|
12
|
-
*
|
|
13
|
-
* Splitting it out is what lets a *pure* function run a relaxation — a React
|
|
14
|
-
* hook cannot, and the alternative was a second integrator drifting away from
|
|
15
|
-
* this one.
|
|
10
|
+
* decides how often to call it.
|
|
16
11
|
*/
|
|
17
12
|
|
|
18
13
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { DEFAULT_ALPHA_DECAY, DEFAULT_ALPHA_MIN, DEFAULT_VELOCITY_DECAY, createSimulation } from './chunk-
|
|
1
|
+
export { DEFAULT_ALPHA_DECAY, DEFAULT_ALPHA_MIN, DEFAULT_VELOCITY_DECAY, createSimulation } from './chunk-RA6U67OH.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SimulationNode, U as UseSimulationOptions, e as Simulation } from './types-
|
|
1
|
+
import { S as SimulationNode, U as UseSimulationOptions, e as Simulation } from './types-CmLrFukG.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Continuous N-body simulation primitive: `createSimulation`'s integrator on a
|
package/dist/react.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* Consumers extend this with arbitrary additional fields (id, label, etc.).
|
|
2
|
+
* Matches d3-force's node shape exactly so d3-force's bundled forces work
|
|
3
|
+
* without translation. Consumers extend this with additional fields.
|
|
6
4
|
*/
|
|
7
5
|
interface SimulationNode {
|
|
8
6
|
x: number;
|
|
@@ -40,14 +38,10 @@ interface UseSimulationOptions<TNode extends SimulationNode> extends SimulationO
|
|
|
40
38
|
*
|
|
41
39
|
* Each tick applies every force and then integrates velocities, scaled by
|
|
42
40
|
* `alpha` — a temperature that decays toward `alphaTarget` so the layout
|
|
43
|
-
* settles
|
|
44
|
-
* which is what a drag does.
|
|
41
|
+
* settles. Raise `alphaTarget` to reheat it.
|
|
45
42
|
*
|
|
46
43
|
* Nodes are mutated in place; the simulation holds the array, not a copy.
|
|
47
|
-
*
|
|
48
|
-
* Nothing here advances time. {@link Simulation} adds the frame loop; a caller
|
|
49
|
-
* that wants the whole relaxation at once — a layout pass, a pre-warm, a test
|
|
50
|
-
* — drives {@link SimulationCore.tick} itself.
|
|
44
|
+
* {@link Simulation} adds the frame loop.
|
|
51
45
|
*/
|
|
52
46
|
interface SimulationCore<TNode extends SimulationNode> {
|
|
53
47
|
readonly nodes: TNode[];
|
|
@@ -63,10 +57,8 @@ interface SimulationCore<TNode extends SimulationNode> {
|
|
|
63
57
|
alphaMin(value: number): this;
|
|
64
58
|
velocityDecay(): number;
|
|
65
59
|
velocityDecay(value: number): this;
|
|
66
|
-
/**
|
|
67
|
-
*
|
|
68
|
-
* onTick or onEnd (matches d3-force). Used for offscreen pre-warm and tests.
|
|
69
|
-
*/
|
|
60
|
+
/** Run the tick body synchronously `iterations` times. Does NOT fire
|
|
61
|
+
* onTick or onEnd (matches d3-force). */
|
|
70
62
|
tick(iterations?: number): this;
|
|
71
63
|
isSettled(): boolean;
|
|
72
64
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weasel-js/simulation",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "A velocity-Verlet N-body integrator that takes d3-force's forces, and a hook that runs it on a frame loop. The root entry has no React and no DOM.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"react": { "optional": true }
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@weasel-js/react": "1.
|
|
29
|
+
"@weasel-js/react": "1.10.0"
|
|
30
30
|
},
|
|
31
31
|
"author": "orochi235",
|
|
32
32
|
"homepage": "https://orochi235.github.io/weasel/",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts","../src/createSimulation.ts"],"names":[],"mappings":";AAmHO,IAAM,sBAAsB,CAAA,GAAI,IAAA,CAAK,GAAA,CAAI,IAAA,EAAO,IAAI,GAAG;AAEvD,IAAM,iBAAA,GAAoB;AAG1B,IAAM,sBAAA,GAAyB;;;AC7F/B,SAAS,iBACd,IAAA,EACuB;AACvB,EAAA,IAAI,QAAQ,IAAA,CAAK,KAAA;AACjB,EAAA,IAAI,MAAA,GAAS,IAAA,CAAK,MAAA,IAAU,EAAC;AAC7B,EAAA,IAAI,KAAA,GAAQ,KAAK,KAAA,IAAS,CAAA;AAC1B,EAAA,IAAI,QAAA,GAAW,KAAK,QAAA,IAAY,iBAAA;AAChC,EAAA,IAAI,UAAA,GAAa,KAAK,UAAA,IAAc,mBAAA;AACpC,EAAA,IAAI,WAAA,GAAc,KAAK,WAAA,IAAe,CAAA;AACtC,EAAA,IAAI,aAAA,GAAgB,KAAK,aAAA,IAAiB,sBAAA;AAC1C,EAAA,MAAM,MAAA,GAAS,IAAA,CAAK,MAAA,IAAU,IAAA,CAAK,MAA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ACT,CAAA;AAAA,IACA,YAAY,KAAA,EAAgB;AAC1B,MAAA,IAAI,KAAA,KAAU,QAAW,OAAO,WAAA;AAChC,MAAA,WAAA,GAAc,KAAA;AACd,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,WAAW,KAAA,EAAgB;AACzB,MAAA,IAAI,KAAA,KAAU,QAAW,OAAO,UAAA;AAChC,MAAA,UAAA,GAAa,KAAA;AACb,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,SAAS,KAAA,EAAgB;AACvB,MAAA,IAAI,KAAA,KAAU,QAAW,OAAO,QAAA;AAChC,MAAA,QAAA,GAAW,KAAA;AACX,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,cAAc,KAAA,EAAgB;AAC5B,MAAA,IAAI,KAAA,KAAU,QAAW,OAAO,aAAA;AAChC,MAAA,aAAA,GAAgB,KAAA;AAChB,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,IAAA,CAAK,aAAqB,CAAA,EAAG;AAC3B,MAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,UAAA,EAAY,KAAK,IAAA,EAAK;AAC1C,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,SAAA,GAAY;AACV,MAAA,OAAO,KAAA,GAAQ,YAAY,WAAA,KAAgB,CAAA;AAAA,IAC7C;AAAA,GACF;AAEA,EAAA,OAAO,IAAA;AACT","file":"chunk-WKNSC4F6.js","sourcesContent":["/**\n * Minimal node interface for the simulation primitive. Matches d3-force's\n * node shape exactly so d3-force's bundled forces work without translation.\n *\n * Consumers extend this with arbitrary additional fields (id, label, etc.).\n */\nexport interface SimulationNode {\n x: number;\n y: number;\n vx?: number;\n vy?: number;\n /** Pin x to this value when non-null. The integrator skips x/vx for the node. */\n fx?: number | null;\n fy?: number | null;\n /** Array index, written by `initialize`. Some forces (e.g. forceLink) rely on it. */\n index?: number;\n}\n\n/**\n * Force protocol — contract-compatible with d3-force. A force is a function\n * called once per tick with the current alpha; it mutates `vx`/`vy` on nodes.\n * Optional `initialize` lets the force compute setup (link indices, etc.).\n */\nexport interface SimulationForce<TNode extends SimulationNode = SimulationNode> {\n (alpha: number): void;\n initialize?(nodes: TNode[], random?: () => number): void;\n}\n\n/** Options for `useSimulation`: everything {@link SimulationOptions} carries,\n * plus the loop's own callbacks and clock. */\nexport interface UseSimulationOptions<TNode extends SimulationNode>\n extends SimulationOptions<TNode> {\n /** Fired after each integration step. */\n onTick?: (nodes: TNode[]) => void;\n /** Fired once when the sim settles (alpha < alphaMin && alphaTarget === 0). */\n onEnd?: () => void;\n\n // Test injection (mirrors useAnimator):\n /** Optional rAF injection. Defaults to window.requestAnimationFrame. */\n requestFrame?: (cb: (t: number) => void) => number;\n cancelFrame?: (handle: number) => void;\n}\n\n/**\n * A force simulation over a set of nodes, with no clock of its own.\n *\n * Each tick applies every force and then integrates velocities, scaled by\n * `alpha` — a temperature that decays toward `alphaTarget` so the layout\n * settles instead of jittering forever. Raise `alphaTarget` to reheat it,\n * which is what a drag does.\n *\n * Nodes are mutated in place; the simulation holds the array, not a copy.\n *\n * Nothing here advances time. {@link Simulation} adds the frame loop; a caller\n * that wants the whole relaxation at once — a layout pass, a pre-warm, a test\n * — drives {@link SimulationCore.tick} itself.\n */\nexport interface SimulationCore<TNode extends SimulationNode> {\n readonly nodes: TNode[];\n\n setNodes(nodes: TNode[]): this;\n setForces(forces: SimulationForce<TNode>[]): this;\n\n alpha(): number;\n alpha(value: number): this;\n alphaTarget(): number;\n alphaTarget(value: number): this;\n alphaDecay(): number;\n alphaDecay(value: number): this;\n alphaMin(): number;\n alphaMin(value: number): this;\n velocityDecay(): number;\n velocityDecay(value: number): this;\n\n /**\n * Run the tick body synchronously `iterations` times. Does NOT fire\n * onTick or onEnd (matches d3-force). Used for offscreen pre-warm and tests.\n */\n tick(iterations?: number): this;\n\n isSettled(): boolean;\n}\n\n/** A {@link SimulationCore} running on a frame loop. */\nexport interface Simulation<TNode extends SimulationNode> extends SimulationCore<TNode> {\n /** Resume the RAF loop. If alpha < alphaMin, reset alpha to 1. */\n restart(): this;\n /** Cancel the RAF loop. Does not change alpha. */\n stop(): this;\n}\n\n/** What {@link SimulationCore} needs: the nodes, the forces acting on them,\n * and the cooling schedule. {@link UseSimulationOptions} adds the loop's. */\nexport interface SimulationOptions<TNode extends SimulationNode> {\n /** Mutable array of nodes. Kit + forces mutate vx/vy/x/y in place. */\n nodes: TNode[];\n /** Forces applied per tick. Each may implement `initialize` for setup. */\n forces?: SimulationForce<TNode>[];\n\n /** Initial alpha. Default 1. */\n alpha?: number;\n /** Settle threshold. When alpha drops below and alphaTarget is 0, onEnd fires. Default 0.001. */\n alphaMin?: number;\n /** Per-tick decay rate toward alphaTarget. Default 1 - 0.001^(1/300) ≈ 0.0228. */\n alphaDecay?: number;\n /** Target alpha. Set above alphaMin to keep sim warm (e.g. during drag). Default 0. */\n alphaTarget?: number;\n /** Friction multiplier on velocity per tick: vx *= (1 - velocityDecay). Default 0.4. */\n velocityDecay?: number;\n /** Handed to each force's `initialize`. Default `Math.random`; pass a seeded\n * source to make a run reproducible. */\n random?: () => number;\n}\n\n/** Default alpha decay: such that alpha drops from 1 to alphaMin (0.001) in 300 ticks. */\nexport const DEFAULT_ALPHA_DECAY = 1 - Math.pow(0.001, 1 / 300);\n/** Alpha below which a cooling simulation is considered settled. */\nexport const DEFAULT_ALPHA_MIN = 0.001;\n/** Fraction of velocity shed each tick — friction. Higher settles sooner and\n * overshoots less. */\nexport const DEFAULT_VELOCITY_DECAY = 0.4;\n","/**\n * The integrator, with no clock attached.\n *\n * Velocity-Verlet over a pluggable force list, contract-compatible with\n * d3-force so d3's own bundled forces drop in untranslated. Nothing here\n * advances time: `tick()` is the only thing that moves a node, and the caller\n * decides how often to call it. `useSimulation` calls it once a frame; a\n * layout pass calls it a few hundred times in one go and reads the result.\n *\n * Splitting it out is what lets a *pure* function run a relaxation — a React\n * hook cannot, and the alternative was a second integrator drifting away from\n * this one.\n */\nimport {\n DEFAULT_ALPHA_DECAY,\n DEFAULT_ALPHA_MIN,\n DEFAULT_VELOCITY_DECAY,\n type SimulationCore,\n type SimulationForce,\n type SimulationNode,\n type SimulationOptions,\n} from './types';\n\n/**\n * A d3-force-compatible simulation with no clock: nothing moves until `tick()`.\n * Nodes are mutated in place, and primed with `index`, `vx` and `vy` on creation.\n */\nexport function createSimulation<TNode extends SimulationNode>(\n opts: SimulationOptions<TNode>,\n): SimulationCore<TNode> {\n let nodes = opts.nodes;\n let forces = opts.forces ?? [];\n let alpha = opts.alpha ?? 1;\n let alphaMin = opts.alphaMin ?? DEFAULT_ALPHA_MIN;\n let alphaDecay = opts.alphaDecay ?? DEFAULT_ALPHA_DECAY;\n let alphaTarget = opts.alphaTarget ?? 0;\n let velocityDecay = opts.velocityDecay ?? DEFAULT_VELOCITY_DECAY;\n const random = opts.random ?? Math.random;\n\n /** `index` is what `forceLink` and friends address nodes by, and a d3 force\n * does `n.vx += …` without checking, so both must exist before the first\n * tick or the first tick produces NaN. */\n function prime(list: TNode[]): void {\n list.forEach((n, i) => {\n n.index = i;\n if (n.vx == null) n.vx = 0;\n if (n.vy == null) n.vy = 0;\n });\n }\n\n prime(nodes);\n for (const f of forces) f.initialize?.(nodes, random);\n\n function step(): void {\n alpha += (alphaTarget - alpha) * alphaDecay;\n for (const f of forces) f(alpha);\n\n const friction = 1 - velocityDecay;\n for (const n of nodes) {\n if (n.fx == null) {\n n.vx = (n.vx ?? 0) * friction;\n // A non-finite velocity — force overflow, or two nodes exactly\n // coincident — would reach the renderer as geometry and cost\n // pathological tessellation work without throwing anywhere.\n if (!Number.isFinite(n.vx)) n.vx = 0;\n n.x += n.vx;\n if (!Number.isFinite(n.x)) n.x = 0;\n } else {\n n.x = n.fx;\n n.vx = 0;\n }\n if (n.fy == null) {\n n.vy = (n.vy ?? 0) * friction;\n if (!Number.isFinite(n.vy)) n.vy = 0;\n n.y += n.vy;\n if (!Number.isFinite(n.y)) n.y = 0;\n } else {\n n.y = n.fy;\n n.vy = 0;\n }\n }\n }\n\n /* eslint-disable @typescript-eslint/no-explicit-any */\n const core: any = {\n get nodes() {\n return nodes;\n },\n setNodes(next: TNode[]) {\n nodes = next;\n prime(nodes);\n for (const f of forces) f.initialize?.(nodes, random);\n return core;\n },\n setForces(next: SimulationForce<TNode>[]) {\n const old = forces;\n forces = next;\n for (const f of next) {\n if (!old.includes(f)) f.initialize?.(nodes, random);\n }\n return core;\n },\n alpha(value?: number) {\n if (value === undefined) return alpha;\n alpha = value;\n return core;\n },\n alphaTarget(value?: number) {\n if (value === undefined) return alphaTarget;\n alphaTarget = value;\n return core;\n },\n alphaDecay(value?: number) {\n if (value === undefined) return alphaDecay;\n alphaDecay = value;\n return core;\n },\n alphaMin(value?: number) {\n if (value === undefined) return alphaMin;\n alphaMin = value;\n return core;\n },\n velocityDecay(value?: number) {\n if (value === undefined) return velocityDecay;\n velocityDecay = value;\n return core;\n },\n tick(iterations: number = 1) {\n for (let i = 0; i < iterations; i++) step();\n return core;\n },\n isSettled() {\n return alpha < alphaMin && alphaTarget === 0;\n },\n };\n /* eslint-enable @typescript-eslint/no-explicit-any */\n return core as SimulationCore<TNode>;\n}\n"]}
|