-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce-directed.html
More file actions
192 lines (175 loc) · 8.31 KB
/
Copy pathforce-directed.html
File metadata and controls
192 lines (175 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GraphJS — Force-Directed Layout</title>
<meta name="description" content="Interactive force-directed graph layout. Drag nodes, watch the simulation settle.">
<style>
:root{
--bg:#eef1f5; --panel:#f6f8fa; --card:#ffffff; --border:#d0d7de;
--ink:#1f2328; --muted:#57606a; --accent:#1f6feb; --accent-soft:#ddeafe;
--mono:ui-monospace,"SF Mono",Menlo,Consolas,monospace;
--sans:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;
}
*{box-sizing:border-box;margin:0;padding:0}
html,body{height:100%}
body{background:var(--bg);color:var(--ink);font-family:var(--sans);overflow:hidden}
header{position:fixed;inset:0 0 auto 0;height:54px;z-index:5;display:flex;align-items:center;gap:14px;
padding:0 18px;background:color-mix(in srgb,var(--panel) 88%,transparent);backdrop-filter:blur(10px);
border-bottom:1px solid var(--border)}
header .brand{font-weight:800;font-size:15px;letter-spacing:-.01em}
header .tag{font-family:var(--mono);font-size:10px;letter-spacing:.18em;color:var(--accent);
background:var(--accent-soft);padding:4px 8px;border-radius:5px;text-transform:uppercase}
header .spacer{flex:1}
header nav{display:flex;gap:8px;align-items:center}
header nav a{font-size:12px;color:var(--muted);text-decoration:none;padding:6px 10px;border-radius:6px;
border:1px solid transparent}
header nav a:hover{color:var(--ink);border-color:var(--border);background:var(--card)}
header nav a[aria-current]{color:var(--accent);border-color:var(--border);background:var(--card);font-weight:600}
canvas{position:fixed;inset:54px 0 0 0;display:block;cursor:grab}
canvas.dragging{cursor:grabbing}
.dock{position:fixed;left:50%;bottom:22px;transform:translateX(-50%);z-index:5;display:flex;gap:8px;
padding:8px;background:color-mix(in srgb,var(--card) 92%,transparent);backdrop-filter:blur(10px);
border:1px solid var(--border);border-radius:12px;box-shadow:0 6px 24px rgba(31,35,40,.10)}
.dock button{font-family:var(--sans);font-size:12.5px;font-weight:600;color:var(--ink);cursor:pointer;
background:var(--card);border:1px solid var(--border);border-radius:8px;padding:8px 14px;display:flex;align-items:center;gap:7px}
.dock button:hover{border-color:var(--accent);color:var(--accent)}
.dock button:active{transform:translateY(1px)}
.dock .dot{width:8px;height:8px;border-radius:50%;background:var(--accent)}
.hint{position:fixed;left:50%;bottom:74px;transform:translateX(-50%);z-index:4;font-family:var(--mono);
font-size:10.5px;letter-spacing:.06em;color:var(--muted);background:color-mix(in srgb,var(--card) 80%,transparent);
border:1px solid var(--border);padding:5px 12px;border-radius:20px;white-space:nowrap}
.hint b{color:var(--accent)}
</style>
</head>
<body>
<header>
<span class="brand">GraphJS</span>
<span class="tag">Force-Directed</span>
<span class="spacer"></span>
<nav>
<a href="force-directed.html" aria-current="page">Force</a>
<a href="radial.html">Radial</a>
<a href="org-chart.html">Org chart</a>
<a href="https://github.com/MarcMouries/GraphJS">GitHub ↗</a>
</nav>
</header>
<canvas id="c"></canvas>
<div class="hint"><b>Drag</b> any node · release to let the physics take over</div>
<div class="dock">
<button id="reheat"><span class="dot"></span>Reheat</button>
<button id="toggle">Pause</button>
</div>
<script type="module">
import { Graph, ForceDirected } from "./graphjs.esm.js";
const RM = matchMedia && matchMedia("(prefers-reduced-motion: reduce)").matches;
// A small social network with two loosely-connected clusters.
const data = {
nodes: [
{ id: "ana", name: "Ana" }, { id: "ben", name: "Ben" }, { id: "cara", name: "Cara" },
{ id: "dan", name: "Dan" }, { id: "eve", name: "Eve" }, { id: "finn", name: "Finn" },
{ id: "gina", name: "Gina" }, { id: "hugo", name: "Hugo" }, { id: "ivy", name: "Ivy" },
{ id: "jon", name: "Jon" }, { id: "kim", name: "Kim" }, { id: "leo", name: "Leo" },
],
edges: [
{ source: "ana", target: "ben" }, { source: "ana", target: "cara" }, { source: "ben", target: "cara" },
{ source: "cara", target: "dan" }, { source: "dan", target: "eve" }, { source: "eve", target: "finn" },
{ source: "dan", target: "finn" }, { source: "finn", target: "gina" }, { source: "gina", target: "hugo" },
{ source: "hugo", target: "ivy" }, { source: "ivy", target: "jon" }, { source: "jon", target: "kim" },
{ source: "kim", target: "gina" }, { source: "leo", target: "jon" }, { source: "leo", target: "ana" },
{ source: "eve", target: "kim" },
],
};
const c = document.getElementById("c"), ctx = c.getContext("2d");
let W, H, DPR = 1;
const nameOf = (n) => (n.data && n.data.name) || n.id;
const R = 17;
const graph = new Graph();
graph.loadJSON(data);
let layout = new ForceDirected(graph, {
center: { x: 0, y: 0 },
linkDistance: 120, repulsion: 6500, gravity: 0.06, collisionRadius: R + 16,
});
// Adjacency, for hover highlighting.
const adj = new Map(data.nodes.map((n) => [n.id, new Set()]));
for (const e of data.edges) { adj.get(e.source).add(e.target); adj.get(e.target).add(e.source); }
function resize() {
W = innerWidth; H = innerHeight - 54; DPR = Math.min(devicePixelRatio || 1, 2);
c.width = W * DPR; c.height = H * DPR; c.style.width = W + "px"; c.style.height = H + "px";
ctx.setTransform(DPR, 0, 0, DPR, 0, 0);
layout.center = { x: W / 2, y: H / 2 };
// keep the graph within the visible canvas (leave room for the node radius + label)
layout.options.bounds = { minX: R, minY: R, maxX: W - R, maxY: H - R - 16 };
}
addEventListener("resize", resize);
let hover = null, dragging = null;
layout.on("tick", draw);
layout.on("end", draw);
function draw() {
ctx.clearRect(0, 0, W, H);
const nodes = graph.getNodes();
const links = graph.linkList || [];
const lit = hover ? new Set([hover.id, ...adj.get(hover.id)]) : null;
// Links
ctx.lineCap = "round";
for (const l of links) {
const active = lit && (l.source.id === hover.id || l.target.id === hover.id);
ctx.beginPath();
ctx.moveTo(l.source.x, l.source.y); ctx.lineTo(l.target.x, l.target.y);
ctx.strokeStyle = active ? "#1f6feb" : "#c2ccd6";
ctx.lineWidth = active ? 2.4 : 1.4;
ctx.globalAlpha = lit && !active ? 0.4 : 1;
ctx.stroke();
}
ctx.globalAlpha = 1;
// Nodes
for (const n of nodes) {
const dim = lit && !lit.has(n.id);
ctx.globalAlpha = dim ? 0.35 : 1;
ctx.beginPath(); ctx.arc(n.x, n.y, R, 0, 7);
ctx.fillStyle = n === hover ? "#0b57d0" : "#1f6feb"; ctx.fill();
ctx.lineWidth = 2.5; ctx.strokeStyle = "#ffffff"; ctx.stroke();
ctx.fillStyle = "#1f2328"; ctx.font = "600 12px " + "-apple-system, Segoe UI, sans-serif";
ctx.textAlign = "center"; ctx.textBaseline = "top";
ctx.fillText(nameOf(n), n.x, n.y + R + 4);
}
ctx.globalAlpha = 1;
}
function nodeAt(px, py) {
for (const n of graph.getNodes()) { const dx = px - n.x, dy = py - n.y; if (dx * dx + dy * dy <= (R + 3) ** 2) return n; }
return null;
}
const pos = (e) => { const r = c.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; };
c.addEventListener("mousemove", (e) => {
const p = pos(e);
if (dragging) { layout.pinNode(dragging.id, p.x, p.y); layout.restart(0.35); draw(); return; }
const h = nodeAt(p.x, p.y);
if (h !== hover) { hover = h; draw(); }
});
c.addEventListener("mousedown", (e) => {
const p = pos(e); const n = nodeAt(p.x, p.y);
if (n) { dragging = n; c.classList.add("dragging"); layout.pinNode(n.id, p.x, p.y); layout.restart(0.5); }
});
addEventListener("mouseup", () => {
if (dragging) { layout.unpinNode(dragging.id); dragging = null; c.classList.remove("dragging"); layout.restart(0.2); }
});
// Controls
const toggleBtn = document.getElementById("toggle");
let running = true;
document.getElementById("reheat").onclick = () => { layout.restart(1); if (!running) toggleBtn.click(); };
toggleBtn.onclick = () => {
running = !running;
if (running) { layout.restart(0.4); toggleBtn.textContent = "Pause"; }
else { layout.stop(); toggleBtn.textContent = "Play"; }
};
resize();
if (RM) { // settle instantly, no animation
for (let i = 0; i < 400 && layout.alpha > layout.options.alphaMin; i++) layout.tick();
running = false; toggleBtn.textContent = "Play"; draw();
} else {
layout.start();
}
</script>
</body>
</html>