@graphty/layout 1.1.1 → 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.
@@ -0,0 +1,394 @@
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 Kamada-Kawai 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
+ #status {
58
+ position: absolute;
59
+ bottom: 10px;
60
+ left: 10px;
61
+ color: white;
62
+ background: rgba(0, 0, 0, 0.7);
63
+ padding: 10px;
64
+ border-radius: 5px;
65
+ }
66
+ </style>
67
+ </head>
68
+ <body>
69
+ <div id="container"></div>
70
+ <div id="info">
71
+ <h2>3D Kamada-Kawai Layout</h2>
72
+ <p>Force-directed layout optimizing graph-theoretic distances</p>
73
+ <p>Drag to rotate • Scroll to zoom</p>
74
+ </div>
75
+ <div id="controls">
76
+ <label>
77
+ Graph Type:
78
+ <select id="graphType">
79
+ <option value="grid">Grid Graph</option>
80
+ <option value="complete">Complete Graph</option>
81
+ <option value="cycle">Cycle Graph</option>
82
+ <option value="star">Star Graph</option>
83
+ <option value="wheel">Wheel Graph</option>
84
+ <option value="bipartite">Bipartite Graph</option>
85
+ <option value="scalefree">Scale-Free Graph</option>
86
+ </select>
87
+ </label>
88
+ <label>
89
+ Graph Size: <span id="sizeLabel">16</span>
90
+ <input type="range" id="graphSize" min="4" max="30" value="16">
91
+ </label>
92
+ <label>
93
+ Layout Scale: <span id="scaleLabel">1</span>
94
+ <input type="range" id="scale" min="0.5" max="3" step="0.1" value="1">
95
+ </label>
96
+ <button id="regenerate">Regenerate Graph</button>
97
+ <button id="relayout">Re-run Layout</button>
98
+ <label>
99
+ <input type="checkbox" id="showAxes" checked> Show Axes
100
+ </label>
101
+ <label>
102
+ <input type="checkbox" id="animateNodes"> Animate Nodes
103
+ </label>
104
+ </div>
105
+ <div id="status">
106
+ <div>Nodes: <span id="nodeCount">0</span></div>
107
+ <div>Edges: <span id="edgeCount">0</span></div>
108
+ <div>Stress: <span id="stressValue">-</span></div>
109
+ </div>
110
+
111
+ <!-- Import map for module resolution -->
112
+ <script type="importmap">
113
+ {
114
+ "imports": {
115
+ "three": "https://unpkg.com/three@0.157.0/build/three.module.js",
116
+ "three/addons/": "https://unpkg.com/three@0.157.0/examples/jsm/"
117
+ }
118
+ }
119
+ </script>
120
+
121
+ <script type="module">
122
+ import * as THREE from 'https://unpkg.com/three@0.157.0/build/three.module.js';
123
+ import { OrbitControls } from 'https://unpkg.com/three@0.157.0/examples/jsm/controls/OrbitControls.js';
124
+ import {
125
+ kamadaKawaiLayout,
126
+ completeGraph,
127
+ cycleGraph,
128
+ starGraph,
129
+ wheelGraph,
130
+ gridGraph,
131
+ bipartiteGraph,
132
+ scaleFreeGraph
133
+ } from './layout.js';
134
+
135
+ // Three.js setup
136
+ const scene = new THREE.Scene();
137
+ scene.background = new THREE.Color(0x0a0a0a);
138
+
139
+ const camera = new THREE.PerspectiveCamera(
140
+ 75,
141
+ window.innerWidth / window.innerHeight,
142
+ 0.1,
143
+ 2000
144
+ );
145
+ camera.position.set(400, 300, 500);
146
+
147
+ const renderer = new THREE.WebGLRenderer({ antialias: true });
148
+ renderer.setSize(window.innerWidth, window.innerHeight);
149
+ document.getElementById('container').appendChild(renderer.domElement);
150
+
151
+ const controls = new OrbitControls(camera, renderer.domElement);
152
+ controls.enableDamping = true;
153
+ controls.dampingFactor = 0.05;
154
+
155
+ // Lighting
156
+ const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
157
+ scene.add(ambientLight);
158
+ const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
159
+ directionalLight.position.set(1, 1, 1);
160
+ scene.add(directionalLight);
161
+
162
+ // Graph visualization
163
+ let nodeGroup = new THREE.Group();
164
+ let edgeGroup = new THREE.Group();
165
+ scene.add(nodeGroup);
166
+ scene.add(edgeGroup);
167
+
168
+ // Node animation
169
+ let nodeAnimations = [];
170
+
171
+ // Create graph based on type
172
+ function createGraph(type, size) {
173
+ switch (type) {
174
+ case 'complete':
175
+ return completeGraph(size);
176
+ case 'cycle':
177
+ return cycleGraph(size);
178
+ case 'star':
179
+ return starGraph(size);
180
+ case 'wheel':
181
+ return wheelGraph(size);
182
+ case 'grid':
183
+ const gridSize = Math.floor(Math.sqrt(size));
184
+ return gridGraph(gridSize, gridSize);
185
+ case 'bipartite':
186
+ const n1 = Math.floor(size / 2);
187
+ const n2 = size - n1;
188
+ return bipartiteGraph(n1, n2, 0.7, Date.now());
189
+ case 'scalefree':
190
+ return scaleFreeGraph(size, 2, Date.now());
191
+ default:
192
+ return completeGraph(size);
193
+ }
194
+ }
195
+
196
+ // Calculate layout stress (measure of quality)
197
+ function calculateStress(graph, positions) {
198
+ const nodes = graph.nodes();
199
+ let stress = 0;
200
+ let count = 0;
201
+
202
+ // Calculate actual vs ideal distances
203
+ for (let i = 0; i < nodes.length; i++) {
204
+ for (let j = i + 1; j < nodes.length; j++) {
205
+ const pos1 = positions[nodes[i]];
206
+ const pos2 = positions[nodes[j]];
207
+
208
+ const actualDist = Math.sqrt(
209
+ Math.pow(pos1[0] - pos2[0], 2) +
210
+ Math.pow(pos1[1] - pos2[1], 2) +
211
+ Math.pow(pos1[2] - pos2[2], 2)
212
+ );
213
+
214
+ // Ideal distance (graph distance) - simplified as 1 for connected nodes
215
+ const idealDist = areConnected(graph, nodes[i], nodes[j]) ? 1 : 2;
216
+
217
+ stress += Math.pow(actualDist - idealDist * 100, 2);
218
+ count++;
219
+ }
220
+ }
221
+
222
+ return Math.sqrt(stress / count);
223
+ }
224
+
225
+ function areConnected(graph, node1, node2) {
226
+ for (const [u, v] of graph.edges()) {
227
+ if ((u === node1 && v === node2) || (u === node2 && v === node1)) {
228
+ return true;
229
+ }
230
+ }
231
+ return false;
232
+ }
233
+
234
+ // Visualize graph in 3D
235
+ function visualizeGraph(graph, scale = 1) {
236
+ // Clear previous visualization
237
+ nodeGroup.clear();
238
+ edgeGroup.clear();
239
+ nodeAnimations = [];
240
+
241
+ // Get 3D Kamada-Kawai layout
242
+ const positions = kamadaKawaiLayout(graph, null, null, 'weight', scale * 200, [0, 0, 0], 3);
243
+
244
+ // Update stats
245
+ const nodes = graph.nodes();
246
+ const edges = graph.edges();
247
+ document.getElementById('nodeCount').textContent = nodes.length;
248
+ document.getElementById('edgeCount').textContent = edges.length;
249
+ document.getElementById('stressValue').textContent = calculateStress(graph, positions).toFixed(2);
250
+
251
+ // Create nodes with different colors based on degree
252
+ const nodeMeshes = {};
253
+ const degrees = {};
254
+
255
+ // Calculate degrees
256
+ nodes.forEach(node => {
257
+ degrees[node] = 0;
258
+ });
259
+ edges.forEach(([u, v]) => {
260
+ degrees[u] = (degrees[u] || 0) + 1;
261
+ degrees[v] = (degrees[v] || 0) + 1;
262
+ });
263
+
264
+ const maxDegree = Math.max(...Object.values(degrees));
265
+
266
+ nodes.forEach((node) => {
267
+ const degree = degrees[node];
268
+ const size = 5 + (degree / maxDegree) * 10;
269
+
270
+ const geometry = new THREE.SphereGeometry(size, 16, 16);
271
+ const color = new THREE.Color();
272
+ color.setHSL(0.6 - (degree / maxDegree) * 0.6, 0.8, 0.5);
273
+
274
+ const material = new THREE.MeshPhongMaterial({
275
+ color: color,
276
+ emissive: color,
277
+ emissiveIntensity: 0.2
278
+ });
279
+
280
+ const mesh = new THREE.Mesh(geometry, material);
281
+ const [x, y, z] = positions[node];
282
+ mesh.position.set(x, y, z);
283
+ mesh.userData = { originalPosition: new THREE.Vector3(x, y, z) };
284
+
285
+ nodeGroup.add(mesh);
286
+ nodeMeshes[node] = mesh;
287
+ nodeAnimations.push(mesh);
288
+ });
289
+
290
+ // Create edges with gradient based on node degrees
291
+ edges.forEach(([source, target]) => {
292
+ const sourcePos = positions[source];
293
+ const targetPos = positions[target];
294
+
295
+ const points = [
296
+ new THREE.Vector3(sourcePos[0], sourcePos[1], sourcePos[2]),
297
+ new THREE.Vector3(targetPos[0], targetPos[1], targetPos[2])
298
+ ];
299
+
300
+ const geometry = new THREE.BufferGeometry().setFromPoints(points);
301
+
302
+ // Color based on average degree
303
+ const avgDegree = (degrees[source] + degrees[target]) / 2;
304
+ const color = new THREE.Color();
305
+ color.setHSL(0.6 - (avgDegree / maxDegree) * 0.4, 0.5, 0.4);
306
+
307
+ const material = new THREE.LineBasicMaterial({
308
+ color: color,
309
+ transparent: true,
310
+ opacity: 0.7
311
+ });
312
+
313
+ const line = new THREE.Line(geometry, material);
314
+ edgeGroup.add(line);
315
+ });
316
+ }
317
+
318
+ // Initial graph
319
+ let currentGraph = createGraph('grid', 16);
320
+ visualizeGraph(currentGraph);
321
+
322
+ // Controls
323
+ const graphTypeSelect = document.getElementById('graphType');
324
+ const graphSizeSlider = document.getElementById('graphSize');
325
+ const sizeLabel = document.getElementById('sizeLabel');
326
+ const scaleSlider = document.getElementById('scale');
327
+ const scaleLabel = document.getElementById('scaleLabel');
328
+ const regenerateBtn = document.getElementById('regenerate');
329
+ const relayoutBtn = document.getElementById('relayout');
330
+ const animateNodesCheckbox = document.getElementById('animateNodes');
331
+
332
+ graphSizeSlider.addEventListener('input', (e) => {
333
+ sizeLabel.textContent = e.target.value;
334
+ });
335
+
336
+ scaleSlider.addEventListener('input', (e) => {
337
+ scaleLabel.textContent = e.target.value;
338
+ visualizeGraph(currentGraph, parseFloat(e.target.value));
339
+ });
340
+
341
+ regenerateBtn.addEventListener('click', () => {
342
+ const type = graphTypeSelect.value;
343
+ const size = parseInt(graphSizeSlider.value);
344
+ const scale = parseFloat(scaleSlider.value);
345
+
346
+ currentGraph = createGraph(type, size);
347
+ visualizeGraph(currentGraph, scale);
348
+ });
349
+
350
+ relayoutBtn.addEventListener('click', () => {
351
+ const scale = parseFloat(scaleSlider.value);
352
+ visualizeGraph(currentGraph, scale);
353
+ });
354
+
355
+ graphTypeSelect.addEventListener('change', () => {
356
+ regenerateBtn.click();
357
+ });
358
+
359
+
360
+ // Animation
361
+ let time = 0;
362
+ function animate() {
363
+ requestAnimationFrame(animate);
364
+
365
+ time += 0.01;
366
+
367
+ // Animate nodes
368
+ if (animateNodesCheckbox.checked) {
369
+ nodeAnimations.forEach((mesh, i) => {
370
+ const offset = Math.sin(time + i * 0.5) * 5;
371
+ mesh.position.copy(mesh.userData.originalPosition);
372
+ mesh.position.y += offset;
373
+ });
374
+ } else {
375
+ nodeAnimations.forEach((mesh) => {
376
+ mesh.position.copy(mesh.userData.originalPosition);
377
+ });
378
+ }
379
+
380
+ controls.update();
381
+ renderer.render(scene, camera);
382
+ }
383
+
384
+ // Handle window resize
385
+ window.addEventListener('resize', () => {
386
+ camera.aspect = window.innerWidth / window.innerHeight;
387
+ camera.updateProjectionMatrix();
388
+ renderer.setSize(window.innerWidth, window.innerHeight);
389
+ });
390
+
391
+ animate();
392
+ </script>
393
+ </body>
394
+ </html>