@graphty/layout 1.1.0 → 1.2.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.
Files changed (57) hide show
  1. package/.env.example +11 -0
  2. package/.github/workflows/ci.yml +89 -14
  3. package/.releaserc.json +22 -0
  4. package/CHANGELOG.md +31 -0
  5. package/CLAUDE.md +104 -0
  6. package/CONTRIBUTING.md +1 -0
  7. package/DEPLOYMENT.md +59 -0
  8. package/README.md +662 -93
  9. package/dist/layout-helpers.d.ts +123 -0
  10. package/dist/layout-helpers.js +458 -0
  11. package/dist/layout-helpers.js.map +1 -0
  12. package/dist/layout.d.ts +275 -0
  13. package/dist/layout.js +2304 -0
  14. package/dist/layout.js.map +1 -0
  15. package/dist/vitest.config.d.ts +2 -0
  16. package/dist/vitest.config.js +30 -0
  17. package/dist/vitest.config.js.map +1 -0
  18. package/examples/3d-force-directed.html +611 -0
  19. package/examples/3d-kamada-kawai.html +394 -0
  20. package/examples/3d-layout-comparison.html +448 -0
  21. package/examples/3d-spherical-layout.html +319 -0
  22. package/examples/arf-layout.html +13 -1
  23. package/examples/bfs-layout.html +49 -39
  24. package/examples/bipartite-layout.html +89 -69
  25. package/examples/circular-layout.html +26 -35
  26. package/examples/forceatlas2-layout.html +134 -28
  27. package/examples/index.html +75 -0
  28. package/examples/kamada-kawai-layout.html +13 -1
  29. package/examples/multipartite-layout.html +73 -88
  30. package/examples/planar-layout.html +13 -1
  31. package/examples/random-layout.html +13 -1
  32. package/examples/shell-layout.html +65 -34
  33. package/examples/spectral-layout.html +13 -1
  34. package/examples/spiral-layout.html +13 -1
  35. package/examples/spring-layout.html +25 -3
  36. package/layout-helpers.ts +560 -0
  37. package/layout.ts +316 -14
  38. package/package.json +20 -6
  39. package/test/arf-layout.test.ts +443 -0
  40. package/test/bfs-layout.test.ts +427 -0
  41. package/test/bipartite-layout.test.ts +344 -0
  42. package/test/circular-layout.test.ts +442 -0
  43. package/test/forceatlas2-layout.test.ts +405 -0
  44. package/test/fruchterman-reingold-layout.test.ts +477 -0
  45. package/test/graph-generators.test.ts +450 -0
  46. package/test/kamada-kawai-layout.test.ts +623 -0
  47. package/test/multipartite-layout.test.ts +404 -0
  48. package/test/planar-layout.test.ts +266 -0
  49. package/test/random-layout.test.ts +254 -0
  50. package/test/rescale-layout.test.ts +373 -0
  51. package/test/shell-layout.test.ts +347 -0
  52. package/test/spectral-layout.test.ts +378 -0
  53. package/test/spiral-layout.test.ts +338 -0
  54. package/test/spring-layout.test.ts +241 -0
  55. package/vite.config.js +36 -0
  56. package/vitest.config.ts +30 -0
  57. package/.releaserc +0 -3
@@ -0,0 +1,319 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <!-- Eruda Mobile Console -->
5
+ <script src="https://cdn.jsdelivr.net/npm/eruda@3/eruda.min.js"></script>
6
+ <script>
7
+ // Initialize Eruda console for mobile debugging
8
+ if (typeof eruda !== "undefined") {
9
+ eruda.init();
10
+ // Auto-show on mobile devices
11
+ if (/mobile|android|ios|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())) {
12
+ eruda.show();
13
+ }
14
+ }
15
+ </script>
16
+ <meta charset="UTF-8">
17
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
18
+ <title>3D Spherical Layout - @graphty/layout</title>
19
+ <style>
20
+ body {
21
+ margin: 0;
22
+ font-family: Arial, sans-serif;
23
+ overflow: hidden;
24
+ }
25
+ #container {
26
+ width: 100vw;
27
+ height: 100vh;
28
+ }
29
+ #info {
30
+ position: absolute;
31
+ top: 10px;
32
+ left: 10px;
33
+ color: white;
34
+ background: rgba(0, 0, 0, 0.7);
35
+ padding: 10px;
36
+ border-radius: 5px;
37
+ }
38
+ #controls {
39
+ position: absolute;
40
+ top: 10px;
41
+ right: 10px;
42
+ background: rgba(0, 0, 0, 0.7);
43
+ padding: 10px;
44
+ border-radius: 5px;
45
+ color: white;
46
+ }
47
+ button {
48
+ display: block;
49
+ margin: 5px 0;
50
+ padding: 5px 10px;
51
+ cursor: pointer;
52
+ }
53
+ label {
54
+ display: block;
55
+ margin: 5px 0;
56
+ }
57
+ </style>
58
+ </head>
59
+ <body>
60
+ <div id="container"></div>
61
+ <div id="info">
62
+ <h2>3D Spherical Layout</h2>
63
+ <p>Nodes distributed evenly on a sphere using Fibonacci spiral</p>
64
+ <p>Drag to rotate • Scroll to zoom</p>
65
+ </div>
66
+ <div id="controls">
67
+ <label>
68
+ Graph Type:
69
+ <select id="graphType">
70
+ <option value="complete">Complete Graph</option>
71
+ <option value="cycle">Cycle Graph</option>
72
+ <option value="star">Star Graph</option>
73
+ <option value="wheel">Wheel Graph</option>
74
+ <option value="grid">Grid Graph</option>
75
+ <option value="random">Random Graph</option>
76
+ </select>
77
+ </label>
78
+ <label>
79
+ Node Count: <span id="nodeCountLabel">20</span>
80
+ <input type="range" id="nodeCount" min="5" max="50" value="20">
81
+ </label>
82
+ <label>
83
+ Sphere Radius: <span id="radiusLabel">200</span>
84
+ <input type="range" id="radius" min="50" max="300" value="200">
85
+ </label>
86
+ <button id="regenerate">Regenerate Graph</button>
87
+ <label>
88
+ <input type="checkbox" id="showSphere" checked> Show Sphere
89
+ </label>
90
+ <label>
91
+ <input type="checkbox" id="animateRotation" checked> Auto Rotate
92
+ </label>
93
+ </div>
94
+
95
+ <!-- Import map for module resolution -->
96
+ <script type="importmap">
97
+ {
98
+ "imports": {
99
+ "three": "https://unpkg.com/three@0.157.0/build/three.module.js",
100
+ "three/addons/": "https://unpkg.com/three@0.157.0/examples/jsm/"
101
+ }
102
+ }
103
+ </script>
104
+
105
+ <script type="module">
106
+ import * as THREE from 'https://unpkg.com/three@0.157.0/build/three.module.js';
107
+ import { OrbitControls } from 'https://unpkg.com/three@0.157.0/examples/jsm/controls/OrbitControls.js';
108
+ import {
109
+ circularLayout,
110
+ completeGraph,
111
+ cycleGraph,
112
+ starGraph,
113
+ wheelGraph,
114
+ gridGraph,
115
+ randomGraph
116
+ } from './layout.js';
117
+
118
+ // Three.js setup
119
+ const scene = new THREE.Scene();
120
+ scene.background = new THREE.Color(0x0a0a0a);
121
+
122
+ const camera = new THREE.PerspectiveCamera(
123
+ 75,
124
+ window.innerWidth / window.innerHeight,
125
+ 0.1,
126
+ 2000
127
+ );
128
+ camera.position.set(400, 300, 500);
129
+
130
+ const renderer = new THREE.WebGLRenderer({ antialias: true });
131
+ renderer.setSize(window.innerWidth, window.innerHeight);
132
+ document.getElementById('container').appendChild(renderer.domElement);
133
+
134
+ const controls = new OrbitControls(camera, renderer.domElement);
135
+ controls.enableDamping = true;
136
+ controls.dampingFactor = 0.05;
137
+
138
+ // Lighting
139
+ const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
140
+ scene.add(ambientLight);
141
+ const directionalLight = new THREE.DirectionalLight(0xffffff, 0.4);
142
+ directionalLight.position.set(1, 1, 1);
143
+ scene.add(directionalLight);
144
+
145
+ // Graph visualization
146
+ let nodeGroup = new THREE.Group();
147
+ let edgeGroup = new THREE.Group();
148
+ let sphereMesh = null;
149
+ scene.add(nodeGroup);
150
+ scene.add(edgeGroup);
151
+
152
+ // Create sphere mesh
153
+ function createSphereMesh(radius) {
154
+ const geometry = new THREE.SphereGeometry(radius, 32, 32);
155
+ const material = new THREE.MeshBasicMaterial({
156
+ color: 0x444444,
157
+ wireframe: true,
158
+ transparent: true,
159
+ opacity: 0.2
160
+ });
161
+ return new THREE.Mesh(geometry, material);
162
+ }
163
+
164
+ // Create graph based on type
165
+ function createGraph(type, nodeCount) {
166
+ switch (type) {
167
+ case 'complete':
168
+ return completeGraph(nodeCount);
169
+ case 'cycle':
170
+ return cycleGraph(nodeCount);
171
+ case 'star':
172
+ return starGraph(nodeCount);
173
+ case 'wheel':
174
+ return wheelGraph(nodeCount);
175
+ case 'grid':
176
+ const size = Math.floor(Math.sqrt(nodeCount));
177
+ return gridGraph(size, size);
178
+ case 'random':
179
+ return randomGraph(nodeCount, 0.3, Date.now());
180
+ default:
181
+ return completeGraph(nodeCount);
182
+ }
183
+ }
184
+
185
+ // Visualize graph in 3D
186
+ function visualizeGraph(graph, radius) {
187
+ // Clear previous visualization
188
+ nodeGroup.clear();
189
+ edgeGroup.clear();
190
+
191
+ // Get 3D spherical layout
192
+ const positions = circularLayout(graph, radius, [0, 0, 0], 3);
193
+
194
+ // Create nodes
195
+ const nodeGeometry = new THREE.SphereGeometry(8, 16, 16);
196
+ const nodes = graph.nodes();
197
+ const nodeMeshes = {};
198
+
199
+ nodes.forEach((node, i) => {
200
+ const color = new THREE.Color();
201
+ color.setHSL(i / nodes.length, 0.7, 0.5);
202
+
203
+ const material = new THREE.MeshPhongMaterial({
204
+ color: color,
205
+ emissive: color,
206
+ emissiveIntensity: 0.3
207
+ });
208
+
209
+ const mesh = new THREE.Mesh(nodeGeometry, material);
210
+ const [x, y, z] = positions[node];
211
+ mesh.position.set(x, y, z);
212
+
213
+ nodeGroup.add(mesh);
214
+ nodeMeshes[node] = mesh;
215
+ });
216
+
217
+ // Create edges
218
+ const edges = graph.edges();
219
+ edges.forEach(([source, target]) => {
220
+ const sourcePos = positions[source];
221
+ const targetPos = positions[target];
222
+
223
+ const points = [
224
+ new THREE.Vector3(sourcePos[0], sourcePos[1], sourcePos[2]),
225
+ new THREE.Vector3(targetPos[0], targetPos[1], targetPos[2])
226
+ ];
227
+
228
+ const geometry = new THREE.BufferGeometry().setFromPoints(points);
229
+ const material = new THREE.LineBasicMaterial({
230
+ color: 0x4444ff,
231
+ transparent: true,
232
+ opacity: 0.6
233
+ });
234
+
235
+ const line = new THREE.Line(geometry, material);
236
+ edgeGroup.add(line);
237
+ });
238
+ }
239
+
240
+ // Initial graph
241
+ let currentGraph = createGraph('complete', 20);
242
+ visualizeGraph(currentGraph, 200);
243
+
244
+ // Add sphere
245
+ sphereMesh = createSphereMesh(200);
246
+ scene.add(sphereMesh);
247
+
248
+ // Controls
249
+ const graphTypeSelect = document.getElementById('graphType');
250
+ const nodeCountSlider = document.getElementById('nodeCount');
251
+ const nodeCountLabel = document.getElementById('nodeCountLabel');
252
+ const radiusSlider = document.getElementById('radius');
253
+ const radiusLabel = document.getElementById('radiusLabel');
254
+ const regenerateBtn = document.getElementById('regenerate');
255
+ const showSphereCheckbox = document.getElementById('showSphere');
256
+ const animateCheckbox = document.getElementById('animateRotation');
257
+
258
+ nodeCountSlider.addEventListener('input', (e) => {
259
+ nodeCountLabel.textContent = e.target.value;
260
+ });
261
+
262
+ radiusSlider.addEventListener('input', (e) => {
263
+ radiusLabel.textContent = e.target.value;
264
+ const radius = parseInt(e.target.value);
265
+ visualizeGraph(currentGraph, radius);
266
+ if (sphereMesh) {
267
+ scene.remove(sphereMesh);
268
+ sphereMesh = createSphereMesh(radius);
269
+ sphereMesh.visible = showSphereCheckbox.checked;
270
+ scene.add(sphereMesh);
271
+ }
272
+ });
273
+
274
+ regenerateBtn.addEventListener('click', () => {
275
+ const type = graphTypeSelect.value;
276
+ const nodeCount = parseInt(nodeCountSlider.value);
277
+ const radius = parseInt(radiusSlider.value);
278
+
279
+ currentGraph = createGraph(type, nodeCount);
280
+ visualizeGraph(currentGraph, radius);
281
+ });
282
+
283
+ graphTypeSelect.addEventListener('change', () => {
284
+ regenerateBtn.click();
285
+ });
286
+
287
+ showSphereCheckbox.addEventListener('change', (e) => {
288
+ if (sphereMesh) {
289
+ sphereMesh.visible = e.target.checked;
290
+ }
291
+ });
292
+
293
+ // Animation
294
+ function animate() {
295
+ requestAnimationFrame(animate);
296
+
297
+ if (animateCheckbox.checked) {
298
+ nodeGroup.rotation.y += 0.002;
299
+ edgeGroup.rotation.y += 0.002;
300
+ if (sphereMesh) {
301
+ sphereMesh.rotation.y += 0.002;
302
+ }
303
+ }
304
+
305
+ controls.update();
306
+ renderer.render(scene, camera);
307
+ }
308
+
309
+ // Handle window resize
310
+ window.addEventListener('resize', () => {
311
+ camera.aspect = window.innerWidth / window.innerHeight;
312
+ camera.updateProjectionMatrix();
313
+ renderer.setSize(window.innerWidth, window.innerHeight);
314
+ });
315
+
316
+ animate();
317
+ </script>
318
+ </body>
319
+ </html>
@@ -1,6 +1,18 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en">
3
3
  <head>
4
+ <!-- Eruda Mobile Console -->
5
+ <script src="https://cdn.jsdelivr.net/npm/eruda@3/eruda.min.js"></script>
6
+ <script>
7
+ // Initialize Eruda console for mobile debugging
8
+ if (typeof eruda !== "undefined") {
9
+ eruda.init();
10
+ // Auto-show on mobile devices
11
+ if (/mobile|android|ios|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())) {
12
+ eruda.show();
13
+ }
14
+ }
15
+ </script>
4
16
  <meta charset="UTF-8">
5
17
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
18
  <title>ARF Layout Test</title>
@@ -178,7 +190,7 @@
178
190
  </div>
179
191
 
180
192
  <script type="module">
181
- import { arfLayout } from '../dist/layout.js';
193
+ import { arfLayout } from "./layout.js";
182
194
 
183
195
  let currentGraph = null;
184
196
  let currentSeed = Math.floor(Math.random() * 1000);
@@ -1,6 +1,18 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en">
3
3
  <head>
4
+ <!-- Eruda Mobile Console -->
5
+ <script src="https://cdn.jsdelivr.net/npm/eruda@3/eruda.min.js"></script>
6
+ <script>
7
+ // Initialize Eruda console for mobile debugging
8
+ if (typeof eruda !== "undefined") {
9
+ eruda.init();
10
+ // Auto-show on mobile devices
11
+ if (/mobile|android|ios|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase())) {
12
+ eruda.show();
13
+ }
14
+ }
15
+ </script>
4
16
  <meta charset="UTF-8">
5
17
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
18
  <title>BFS Layout Test</title>
@@ -129,6 +141,14 @@
129
141
  <h3>Layout Parameters</h3>
130
142
 
131
143
  <div class="control-group">
144
+ <label for="root-selection">Root selection:</label>
145
+ <select id="root-selection">
146
+ <option value="auto">Auto (best root)</option>
147
+ <option value="manual">Manual selection</option>
148
+ </select>
149
+ </div>
150
+
151
+ <div class="control-group" id="manual-node-group">
132
152
  <label for="start-node">Starting node:</label>
133
153
  <select id="start-node">
134
154
  <!-- Dynamically populated -->
@@ -149,55 +169,38 @@
149
169
  </div>
150
170
 
151
171
  <script type="module">
152
- import { bfsLayout } from '../dist/layout.js';
172
+ import {
173
+ bfsLayout,
174
+ starGraph,
175
+ gridGraph,
176
+ randomGraph,
177
+ scaleFreeGraph,
178
+ findBestRoot
179
+ } from "./layout.js";
153
180
 
154
181
  let currentGraph = null;
182
+ let bestRoot = null;
155
183
 
156
184
  function generateGraph(type, numNodes) {
157
- const nodes = Array.from({length: numNodes}, (_, i) => i);
158
- const edges = [];
159
-
160
185
  switch(type) {
161
186
  case 'tree':
162
- // Generate a connected tree
163
- for (let i = 1; i < numNodes; i++) {
164
- const parent = Math.floor(Math.random() * i);
165
- edges.push([parent, i]);
166
- }
167
- break;
187
+ // Generate tree-like structure using scale-free with low m
188
+ return scaleFreeGraph(numNodes, 1, Date.now());
168
189
 
169
190
  case 'connected':
170
- // First create a tree to ensure connectivity
171
- for (let i = 1; i < numNodes; i++) {
172
- const parent = Math.floor(Math.random() * i);
173
- edges.push([parent, i]);
174
- }
175
- // Add some extra edges
176
- const extraEdges = Math.floor(numNodes * 0.3);
177
- for (let i = 0; i < extraEdges; i++) {
178
- const a = Math.floor(Math.random() * numNodes);
179
- const b = Math.floor(Math.random() * numNodes);
180
- if (a !== b && !edges.some(([s, t]) =>
181
- (s === a && t === b) || (s === b && t === a))) {
182
- edges.push([a, b]);
183
- }
184
- }
185
- break;
191
+ // Generate connected random graph
192
+ return randomGraph(numNodes, 0.15, Date.now());
186
193
 
187
194
  case 'grid':
188
- // Create an approximate grid
189
- const gridSize = Math.ceil(Math.sqrt(numNodes));
190
- for (let i = 0; i < numNodes; i++) {
191
- const row = Math.floor(i / gridSize);
192
- const col = i % gridSize;
193
-
194
- // Connect to the right
195
- if (col < gridSize - 1 && i + 1 < numNodes) {
196
- edges.push([i, i + 1]);
197
- }
198
-
199
- // Connect below
200
- if (row < gridSize - 1 && i + gridSize < numNodes) {
195
+ // Create grid
196
+ const size = Math.ceil(Math.sqrt(numNodes));
197
+ return gridGraph(size, size);
198
+
199
+ case 'star':
200
+ return starGraph(numNodes);
201
+
202
+ default:
203
+ return randomGraph(numNodes, 0.2, Date.now());
201
204
  edges.push([i, i + gridSize]);
202
205
  }
203
206
  }
@@ -471,6 +474,13 @@
471
474
  };
472
475
  });
473
476
 
477
+ // Root selection handler
478
+ document.getElementById('root-selection').onchange = function() {
479
+ const manualGroup = document.getElementById('manual-node-group');
480
+ manualGroup.style.opacity = this.value === 'auto' ? '0.5' : '1';
481
+ manualGroup.style.pointerEvents = this.value === 'auto' ? 'none' : 'auto';
482
+ };
483
+
474
484
  document.getElementById('graph-type').onchange = null;
475
485
  document.getElementById('start-node').onchange = null;
476
486
  document.getElementById('alignment').onchange = null;