@dxos/react-ui-graph 0.8.4-main.fffef41 → 0.9.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/LICENSE +102 -5
- package/README.md +5 -7
- package/dist/lib/browser/chunk-EASNZZ7B.mjs +52 -0
- package/dist/lib/browser/chunk-EASNZZ7B.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +2729 -592
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +16 -16
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-JSE2X4ML.mjs +54 -0
- package/dist/lib/node-esm/chunk-JSE2X4ML.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +2729 -592
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +16 -16
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/components/Graph/Graph.d.ts +23 -6
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
- package/dist/types/src/components/Graph/Graph.stories.d.ts +6 -6
- package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
- package/dist/types/src/components/SVG/FPS.d.ts +20 -0
- package/dist/types/src/components/SVG/FPS.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Root.d.ts +8 -4
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
- package/dist/types/src/components/SVG/SVG.d.ts +17 -18
- package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
- package/dist/types/src/fx/fx.stories.d.ts +3 -3
- package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
- package/dist/types/src/fx/pulsar.d.ts.map +1 -1
- package/dist/types/src/graph/markers.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts +47 -0
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts +65 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-force-projector.d.ts +1 -1
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +1 -1
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts +23 -0
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.d.ts +67 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts +2 -0
- package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts +10 -2
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +7 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts +1 -1
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-swarm-projector.d.ts +72 -0
- package/dist/types/src/graph/projector/graph-swarm-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/index.d.ts +5 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -1
- package/dist/types/src/graph/projector/projector.d.ts +10 -1
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +41 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/index.d.ts +1 -0
- package/dist/types/src/graph/renderer/index.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/labels.d.ts +33 -0
- package/dist/types/src/graph/renderer/labels.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/renderer.d.ts +12 -1
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/graph/types.d.ts +15 -1
- package/dist/types/src/graph/types.d.ts.map +1 -1
- package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
- package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
- package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
- package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
- package/dist/types/src/stories/experimental.stories.d.ts +7 -0
- package/dist/types/src/stories/experimental.stories.d.ts.map +1 -0
- package/dist/types/src/stories/hooks.stories.d.ts +4 -3
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
- package/dist/types/src/testing/data.d.ts +5 -5
- package/dist/types/src/testing/data.d.ts.map +1 -1
- package/dist/types/src/testing/hooks.d.ts +2 -16
- package/dist/types/src/testing/hooks.d.ts.map +1 -1
- package/dist/types/src/testing/model.d.ts +4 -8
- package/dist/types/src/testing/model.d.ts.map +1 -1
- package/dist/types/src/util/events.d.ts.map +1 -1
- package/dist/types/src/util/fraction.d.ts +1 -1
- package/dist/types/src/util/fraction.d.ts.map +1 -1
- package/dist/types/src/util/points.d.ts.map +1 -1
- package/dist/types/src/util/scale.d.ts.map +1 -1
- package/dist/types/src/util/screen.d.ts.map +1 -1
- package/dist/types/src/util/vector.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -35
- package/src/components/Graph/Graph.stories.tsx +45 -39
- package/src/components/Graph/Graph.tsx +126 -23
- package/src/components/Mesh/Mesh.stories.tsx +8 -7
- package/src/components/Mesh/Mesh.tsx +43 -35
- package/src/components/SVG/FPS.tsx +91 -0
- package/src/components/SVG/Grid.tsx +1 -1
- package/src/components/SVG/Root.tsx +42 -9
- package/src/components/SVG/SVG.tsx +14 -3
- package/src/components/SVG/Svg.stories.tsx +4 -5
- package/src/fx/fx.stories.tsx +7 -8
- package/src/graph/projector/graph-bundle-projector.ts +210 -0
- package/src/graph/projector/graph-cluster-projector.ts +337 -0
- package/src/graph/projector/graph-drag.ts +0 -1
- package/src/graph/projector/graph-force-projector.ts +7 -3
- package/src/graph/projector/graph-hierarchical-projector.ts +4 -4
- package/src/graph/projector/graph-lattice-projector.ts +86 -0
- package/src/graph/projector/graph-plexus-projector.test.ts +102 -0
- package/src/graph/projector/graph-plexus-projector.ts +309 -0
- package/src/graph/projector/graph-projector.ts +24 -4
- package/src/graph/projector/graph-radial-projector.ts +31 -4
- package/src/graph/projector/graph-relational-projector.ts +4 -4
- package/src/graph/projector/graph-swarm-projector.ts +373 -0
- package/src/graph/projector/index.ts +5 -0
- package/src/graph/projector/projector.ts +12 -3
- package/src/graph/renderer/graph-renderer.ts +289 -31
- package/src/graph/renderer/index.ts +1 -0
- package/src/graph/renderer/labels.ts +138 -0
- package/src/graph/renderer/renderer.ts +15 -4
- package/src/graph/types.ts +15 -1
- package/src/stories/experimental.stories.tsx +446 -0
- package/src/stories/hooks.stories.tsx +21 -10
- package/src/testing/data.ts +14 -14
- package/src/testing/hooks.ts +3 -4
- package/src/testing/model.ts +6 -6
- package/src/util/fraction.ts +1 -1
- package/styles/graph.css +341 -43
- package/dist/lib/browser/chunk-XWA2XJDX.mjs +0 -955
- package/dist/lib/browser/chunk-XWA2XJDX.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-3RLSFEEV.mjs +0 -957
- package/dist/lib/node-esm/chunk-3RLSFEEV.mjs.map +0 -7
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Timer, timer } from 'd3';
|
|
6
|
+
|
|
7
|
+
import { type Graph } from '@dxos/graph';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
|
|
10
|
+
import { type GraphLayoutNode } from '../types';
|
|
11
|
+
import { GraphProjector, type GraphProjectorOptions } from './graph-projector';
|
|
12
|
+
|
|
13
|
+
// Boids swarming simulation rendered through the react-ui-graph SVG renderer.
|
|
14
|
+
// Mirrors the canvas Swarm component's tick (alignment / cohesion / separation)
|
|
15
|
+
// but mutates `x/y` on `GraphLayoutNode` instances directly and emits 'positions'
|
|
16
|
+
// each frame so `GraphRenderer.applyPositions` can fast-path SVG transforms,
|
|
17
|
+
// exactly like `GraphForceProjector`.
|
|
18
|
+
//
|
|
19
|
+
// https://en.wikipedia.org/wiki/Boids
|
|
20
|
+
|
|
21
|
+
const SWARMMATE_RADIUS = 60;
|
|
22
|
+
const SEPARATION_DISTANCE = 30;
|
|
23
|
+
|
|
24
|
+
/** Per-frame state stored on a layout node by the projector. */
|
|
25
|
+
export type SwarmNode = GraphLayoutNode & {
|
|
26
|
+
vx?: number;
|
|
27
|
+
vy?: number;
|
|
28
|
+
ax?: number;
|
|
29
|
+
ay?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Shallow ring buffer of recent absolute positions (oldest first). One position is
|
|
32
|
+
* pushed per tick BEFORE integration, so after a tick the most recent entry is the
|
|
33
|
+
* pre-tick position. Cleared whenever the boid wraps a viewBox edge so the trail
|
|
34
|
+
* doesn't streak across the canvas.
|
|
35
|
+
*/
|
|
36
|
+
history?: Array<{ x: number; y: number }>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type GraphSwarmProjectorOptions = GraphProjectorOptions & {
|
|
40
|
+
/** Initial spread (px) of new boids around the origin. */
|
|
41
|
+
spawnRadius?: number;
|
|
42
|
+
/** Hard cap on per-tick velocity. */
|
|
43
|
+
maxVelocity?: number;
|
|
44
|
+
/** Per-force weights — match Swarm's normalisation (divided by 100 on apply). */
|
|
45
|
+
alignment?: number;
|
|
46
|
+
cohesion?: number;
|
|
47
|
+
separation?: number;
|
|
48
|
+
/** Pixel radius (in SVG model coords) of the invisible repel zone tracking the pointer. 0 disables. */
|
|
49
|
+
cursorRepel?: number;
|
|
50
|
+
/** Avoidance force weight (divided by 10 on apply, matching canvas Swarm). */
|
|
51
|
+
avoidance?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Number of previous positions retained per boid (one per simulation tick). The visible
|
|
54
|
+
* tail spans roughly `trailLength * maxVelocity` pixels — at 60 fps a value of 30 is
|
|
55
|
+
* about a half-second streak. Dense per-tick sampling avoids the "snap" artifact a
|
|
56
|
+
* coarser sample interval introduces when only the head moves between samples.
|
|
57
|
+
*/
|
|
58
|
+
trailLength?: number;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const DEFAULTS: Required<
|
|
62
|
+
Pick<
|
|
63
|
+
GraphSwarmProjectorOptions,
|
|
64
|
+
| 'spawnRadius'
|
|
65
|
+
| 'maxVelocity'
|
|
66
|
+
| 'alignment'
|
|
67
|
+
| 'cohesion'
|
|
68
|
+
| 'separation'
|
|
69
|
+
| 'cursorRepel'
|
|
70
|
+
| 'avoidance'
|
|
71
|
+
| 'trailLength'
|
|
72
|
+
>
|
|
73
|
+
> = {
|
|
74
|
+
spawnRadius: 200,
|
|
75
|
+
maxVelocity: 1.2,
|
|
76
|
+
alignment: 3,
|
|
77
|
+
cohesion: 3,
|
|
78
|
+
separation: 3,
|
|
79
|
+
cursorRepel: 120,
|
|
80
|
+
avoidance: 3,
|
|
81
|
+
trailLength: 30,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Boids projector. One simulation timer (d3 `timer`) drives the tick; nodes are
|
|
86
|
+
* the boids, with `vx/vy/ax/ay` carried alongside `x/y`. Edges are kept in the
|
|
87
|
+
* layout so the renderer still draws them between connected boids, but they do
|
|
88
|
+
* NOT exert a link force — boid motion is governed purely by alignment, cohesion
|
|
89
|
+
* and separation, with viewBox-wrap at the SVG bounds.
|
|
90
|
+
*/
|
|
91
|
+
export class GraphSwarmProjector<NodeData = any> extends GraphProjector<NodeData, GraphSwarmProjectorOptions> {
|
|
92
|
+
#timer?: Timer;
|
|
93
|
+
/** Cursor in SVG model coordinates, or null when the pointer is outside the surface. */
|
|
94
|
+
#cursor: { x: number; y: number } | null = null;
|
|
95
|
+
|
|
96
|
+
override findNode(): GraphLayoutNode<NodeData> | undefined {
|
|
97
|
+
// Boids have no spatial index — drag isn't supported in this variant.
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Tell the projector where the pointer is, in SVG model coordinates (the same space
|
|
103
|
+
* `node.x/y` live in — center origin, post-zoom). Pass `null` (or omit args) on
|
|
104
|
+
* pointer-leave to disable cursor avoidance.
|
|
105
|
+
*/
|
|
106
|
+
setCursor(x?: number | null, y?: number | null): void {
|
|
107
|
+
if (x == null || y == null) {
|
|
108
|
+
this.#cursor = null;
|
|
109
|
+
} else {
|
|
110
|
+
this.#cursor = { x, y };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
protected override onUpdate(graph?: Graph.Any): void {
|
|
115
|
+
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
116
|
+
this.mergeData(graph);
|
|
117
|
+
this.initializeNodes();
|
|
118
|
+
// Bind enter/exit once before motion starts; subsequent ticks emit 'positions'.
|
|
119
|
+
this.emitUpdate('topology');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
protected override async onStart(): Promise<void> {
|
|
123
|
+
this.startTimer();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
protected override async onStop(): Promise<void> {
|
|
127
|
+
this.stopTimer();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
protected override onRefresh(): void {
|
|
131
|
+
if (!this.#timer) {
|
|
132
|
+
this.startTimer();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
#opt<K extends keyof typeof DEFAULTS>(key: K): number {
|
|
137
|
+
return (this.options[key] as number | undefined) ?? DEFAULTS[key];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private initializeNodes(): void {
|
|
141
|
+
const spawnRadius = this.#opt('spawnRadius');
|
|
142
|
+
const maxVelocity = this.#opt('maxVelocity');
|
|
143
|
+
for (const node of this.layout.graph.nodes as SwarmNode[]) {
|
|
144
|
+
if (!node.initialized) {
|
|
145
|
+
const theta = Math.random() * 2 * Math.PI;
|
|
146
|
+
const r = Math.random() * spawnRadius;
|
|
147
|
+
node.x = Math.cos(theta) * r;
|
|
148
|
+
node.y = Math.sin(theta) * r;
|
|
149
|
+
// Random initial velocity bounded by maxVelocity.
|
|
150
|
+
const phi = Math.random() * 2 * Math.PI;
|
|
151
|
+
const speed = (0.3 + Math.random() * 0.7) * maxVelocity;
|
|
152
|
+
node.vx = Math.cos(phi) * speed;
|
|
153
|
+
node.vy = Math.sin(phi) * speed;
|
|
154
|
+
node.ax = 0;
|
|
155
|
+
node.ay = 0;
|
|
156
|
+
node.initialized = true;
|
|
157
|
+
} else {
|
|
158
|
+
// Make sure velocity / acceleration exist on nodes inherited from a prior layout.
|
|
159
|
+
node.vx ??= 0;
|
|
160
|
+
node.vy ??= 0;
|
|
161
|
+
node.ax ??= 0;
|
|
162
|
+
node.ay ??= 0;
|
|
163
|
+
}
|
|
164
|
+
// Default render radius — matches the force projector default.
|
|
165
|
+
node.r ??= 6;
|
|
166
|
+
// Reset the trail history. On fresh entry (`prev` from force/lattice/...) the field
|
|
167
|
+
// is undefined; on re-entry from a prior swarm session the layout node is reused and
|
|
168
|
+
// still carries stale absolute positions which would draw the tail back to wherever
|
|
169
|
+
// the boid was last time. Seed with the current position so the tail grows from the
|
|
170
|
+
// boid's actual starting point.
|
|
171
|
+
node.history = [{ x: node.x ?? 0, y: node.y ?? 0 }];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private startTimer(): void {
|
|
176
|
+
this.stopTimer();
|
|
177
|
+
this.#timer = timer(() => this.tick());
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private stopTimer(): void {
|
|
181
|
+
this.#timer?.stop();
|
|
182
|
+
this.#timer = undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* One simulation step. Modeled on `tick` in Swarm.tsx, but operating in the
|
|
187
|
+
* SVG centered-origin coordinate space and writing to `node.x/y` directly so
|
|
188
|
+
* the existing `GraphRenderer.applyPositions` reads them unchanged.
|
|
189
|
+
*/
|
|
190
|
+
private tick(): void {
|
|
191
|
+
const size = this.context.size;
|
|
192
|
+
if (!size) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const { width, height } = size;
|
|
196
|
+
// SVG.Root with `centered: true` (the default) uses a viewBox where origin is
|
|
197
|
+
// the center and bounds are [-w/2, w/2] x [-h/2, h/2].
|
|
198
|
+
const halfW = width / 2;
|
|
199
|
+
const halfH = height / 2;
|
|
200
|
+
|
|
201
|
+
const nodes = this.layout.graph.nodes as SwarmNode[];
|
|
202
|
+
const maxVelocity = this.#opt('maxVelocity');
|
|
203
|
+
// Match Swarm's force normalisation so weight semantics line up across variants.
|
|
204
|
+
const alignmentW = this.#opt('alignment') / 100;
|
|
205
|
+
const cohesionW = this.#opt('cohesion') / 100;
|
|
206
|
+
const separationW = this.#opt('separation') / 100;
|
|
207
|
+
const avoidanceW = this.#opt('avoidance') / 10;
|
|
208
|
+
const cursorRepel = this.#opt('cursorRepel');
|
|
209
|
+
const cursor = cursorRepel > 0 ? this.#cursor : null;
|
|
210
|
+
|
|
211
|
+
// Two-pass per tick: (1) compute acceleration from neighbours, (2) integrate.
|
|
212
|
+
for (const b1 of nodes) {
|
|
213
|
+
let ax = 0;
|
|
214
|
+
let ay = 0;
|
|
215
|
+
let alignX = 0;
|
|
216
|
+
let alignY = 0;
|
|
217
|
+
let alignActive = false;
|
|
218
|
+
let cohX = 0;
|
|
219
|
+
let cohY = 0;
|
|
220
|
+
let cohActive = false;
|
|
221
|
+
let sepX = 0;
|
|
222
|
+
let sepY = 0;
|
|
223
|
+
let sepActive = false;
|
|
224
|
+
let avoidX = 0;
|
|
225
|
+
let avoidY = 0;
|
|
226
|
+
let avoidActive = false;
|
|
227
|
+
|
|
228
|
+
// Cursor repulsion: when the cursor is within the repel zone, push the boid
|
|
229
|
+
// directly away. Mirrors the canvas Swarm's `repel` (treats the cursor as a
|
|
230
|
+
// SwarmObstacle with radius = cursorRepel).
|
|
231
|
+
if (cursor) {
|
|
232
|
+
const dx = cursor.x - (b1.x ?? 0);
|
|
233
|
+
const dy = cursor.y - (b1.y ?? 0);
|
|
234
|
+
const distSq = dx * dx + dy * dy;
|
|
235
|
+
if (distSq > 0 && distSq < cursorRepel * cursorRepel) {
|
|
236
|
+
const dist = Math.sqrt(distSq);
|
|
237
|
+
// Inverse-distance push, matching the canvas variant's `diff.scaleTo(-1 / distance)`.
|
|
238
|
+
avoidX = (-dx / dist) * (1 / dist);
|
|
239
|
+
avoidY = (-dy / dist) * (1 / dist);
|
|
240
|
+
avoidActive = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Mirror the canvas variant: while a boid is being avoided, ignore swarmmate
|
|
245
|
+
// forces so it commits fully to escaping the cursor.
|
|
246
|
+
if (!avoidActive) {
|
|
247
|
+
for (const b2 of nodes) {
|
|
248
|
+
if (b1 === b2) {
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const dx = (b2.x ?? 0) - (b1.x ?? 0);
|
|
253
|
+
const dy = (b2.y ?? 0) - (b1.y ?? 0);
|
|
254
|
+
const distSq = dx * dx + dy * dy;
|
|
255
|
+
if (distSq === 0) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const dist = Math.sqrt(distSq);
|
|
259
|
+
|
|
260
|
+
if (dist < SEPARATION_DISTANCE) {
|
|
261
|
+
// Push away — inverse-distance weighting matches the canvas variant.
|
|
262
|
+
sepX += (-dx / dist) * (1 / dist);
|
|
263
|
+
sepY += (-dy / dist) * (1 / dist);
|
|
264
|
+
sepActive = true;
|
|
265
|
+
}
|
|
266
|
+
if (dist < SWARMMATE_RADIUS) {
|
|
267
|
+
cohX += dx;
|
|
268
|
+
cohY += dy;
|
|
269
|
+
cohActive = true;
|
|
270
|
+
alignX += b2.vx ?? 0;
|
|
271
|
+
alignY += b2.vy ?? 0;
|
|
272
|
+
alignActive = true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const applyForce = (fx: number, fy: number, weight: number, active: boolean): void => {
|
|
278
|
+
if (!active || weight === 0) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
// Steer = target_velocity - current_velocity, then truncate to weight.
|
|
282
|
+
const len = Math.sqrt(fx * fx + fy * fy);
|
|
283
|
+
if (len === 0) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const sx = (fx * maxVelocity) / len - (b1.vx ?? 0);
|
|
287
|
+
const sy = (fy * maxVelocity) / len - (b1.vy ?? 0);
|
|
288
|
+
const slen = Math.sqrt(sx * sx + sy * sy);
|
|
289
|
+
if (slen > weight) {
|
|
290
|
+
ax += (sx * weight) / slen;
|
|
291
|
+
ay += (sy * weight) / slen;
|
|
292
|
+
} else {
|
|
293
|
+
ax += sx;
|
|
294
|
+
ay += sy;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
applyForce(alignX, alignY, alignmentW, alignActive);
|
|
299
|
+
applyForce(cohX, cohY, cohesionW, cohActive);
|
|
300
|
+
applyForce(sepX, sepY, separationW, sepActive);
|
|
301
|
+
applyForce(avoidX, avoidY, avoidanceW, avoidActive);
|
|
302
|
+
|
|
303
|
+
b1.ax = ax;
|
|
304
|
+
b1.ay = ay;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Integrate + viewBox-wrap + trail bookkeeping.
|
|
308
|
+
const trailLength = Math.max(0, Math.round(this.#opt('trailLength')));
|
|
309
|
+
for (const b of nodes) {
|
|
310
|
+
let vx = (b.vx ?? 0) + (b.ax ?? 0);
|
|
311
|
+
let vy = (b.vy ?? 0) + (b.ay ?? 0);
|
|
312
|
+
const vlen = Math.sqrt(vx * vx + vy * vy);
|
|
313
|
+
if (vlen > maxVelocity) {
|
|
314
|
+
vx = (vx * maxVelocity) / vlen;
|
|
315
|
+
vy = (vy * maxVelocity) / vlen;
|
|
316
|
+
}
|
|
317
|
+
b.vx = vx;
|
|
318
|
+
b.vy = vy;
|
|
319
|
+
|
|
320
|
+
// Push the pre-integration position into history every tick. Dense sampling means
|
|
321
|
+
// the path-end shifts by a sub-pixel amount per frame — the curve evolves
|
|
322
|
+
// continuously and the tail-end gradient stop tracks smoothly, avoiding the snap
|
|
323
|
+
// / flicker a sparser sample interval would introduce.
|
|
324
|
+
if (trailLength > 0) {
|
|
325
|
+
const history = b.history ?? (b.history = []);
|
|
326
|
+
history.push({ x: b.x ?? 0, y: b.y ?? 0 });
|
|
327
|
+
if (history.length > trailLength) {
|
|
328
|
+
history.splice(0, history.length - trailLength);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let x = (b.x ?? 0) + vx;
|
|
333
|
+
let y = (b.y ?? 0) + vy;
|
|
334
|
+
// Wrap at the centered viewBox edges so boids leaving one side reappear on the opposite.
|
|
335
|
+
// On wrap we clear the trail so the line doesn't streak across the canvas (matches
|
|
336
|
+
// the canvas Swarm's wrap-aware trail rendering, but simpler).
|
|
337
|
+
let wrapped = false;
|
|
338
|
+
if (x > halfW) {
|
|
339
|
+
x -= width;
|
|
340
|
+
wrapped = true;
|
|
341
|
+
} else if (x < -halfW) {
|
|
342
|
+
x += width;
|
|
343
|
+
wrapped = true;
|
|
344
|
+
}
|
|
345
|
+
if (y > halfH) {
|
|
346
|
+
y -= height;
|
|
347
|
+
wrapped = true;
|
|
348
|
+
} else if (y < -halfH) {
|
|
349
|
+
y += height;
|
|
350
|
+
wrapped = true;
|
|
351
|
+
}
|
|
352
|
+
if (wrapped && b.history) {
|
|
353
|
+
b.history.length = 0;
|
|
354
|
+
}
|
|
355
|
+
b.x = x;
|
|
356
|
+
b.y = y;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Edge geometry: quadratic Bézier with control at the origin so every edge bends
|
|
360
|
+
// toward the center — visually echoing the bundle projector's centre-routed
|
|
361
|
+
// curves while staying cheap to compute. The renderer's `applyPositions` reads
|
|
362
|
+
// `edge.path` directly when present.
|
|
363
|
+
for (const edge of this.layout.graph.edges) {
|
|
364
|
+
const sx = edge.source.x ?? 0;
|
|
365
|
+
const sy = edge.source.y ?? 0;
|
|
366
|
+
const tx = edge.target.x ?? 0;
|
|
367
|
+
const ty = edge.target.y ?? 0;
|
|
368
|
+
edge.path = `M ${sx} ${sy} Q 0 0 ${tx} ${ty}`;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
this.emitUpdate('positions');
|
|
372
|
+
}
|
|
373
|
+
}
|
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './graph-bundle-projector';
|
|
6
|
+
export * from './graph-cluster-projector';
|
|
5
7
|
export * from './graph-drag';
|
|
6
8
|
export * from './graph-projector';
|
|
7
9
|
export * from './graph-force-projector';
|
|
8
10
|
export * from './graph-hierarchical-projector';
|
|
11
|
+
export * from './graph-lattice-projector';
|
|
12
|
+
export * from './graph-plexus-projector';
|
|
9
13
|
export * from './graph-relational-projector';
|
|
10
14
|
export * from './graph-radial-projector';
|
|
15
|
+
export * from './graph-swarm-projector';
|
|
11
16
|
export * from './projector';
|
|
@@ -10,11 +10,20 @@ export type ProjectorOptions = {
|
|
|
10
10
|
idAccessor?: IdAccessor;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Update kinds signal what changed since the previous render.
|
|
15
|
+
* - `topology`: nodes/edges added or removed, or per-node attributes may have changed.
|
|
16
|
+
* The renderer should perform a full enter/exit join and re-run attribute callbacks.
|
|
17
|
+
* - `positions`: only `x/y` (and similar transient layout state) changed.
|
|
18
|
+
* The renderer can fast-path: just update transforms and edge geometry.
|
|
19
|
+
*/
|
|
20
|
+
export type ProjectorUpdateKind = 'topology' | 'positions';
|
|
21
|
+
|
|
13
22
|
/**
|
|
14
23
|
* Generates a layout to be rendered.
|
|
15
24
|
*/
|
|
16
25
|
export abstract class Projector<NodeData, Layout, Options extends ProjectorOptions> {
|
|
17
|
-
public readonly updated = new EventEmitter<{ layout: Layout }>();
|
|
26
|
+
public readonly updated = new EventEmitter<{ layout: Layout; kind: ProjectorUpdateKind }>();
|
|
18
27
|
|
|
19
28
|
private readonly _options: Options;
|
|
20
29
|
|
|
@@ -48,8 +57,8 @@ export abstract class Projector<NodeData, Layout, Options extends ProjectorOptio
|
|
|
48
57
|
this.onUpdate(data);
|
|
49
58
|
}
|
|
50
59
|
|
|
51
|
-
emitUpdate() {
|
|
52
|
-
this.updated.emit({ layout: this.layout });
|
|
60
|
+
emitUpdate(kind: ProjectorUpdateKind = 'topology') {
|
|
61
|
+
this.updated.emit({ layout: this.layout, kind });
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
async clear() {
|