commit
Make the relationship map a rotating 3D graph
Everyone sat on one ring, so links crossed the middle and the map turned into a tangle. Now a deterministic 3D force layout clusters each couple and polycule, turns slowly, can be dragged or turned with the arrow keys, and highlights someone's links on hover or focus. Crushes are dashed instead of labelled. A plain list of pairs sits underneath.
diff --git a/src/lib/components/RelationshipGraph.svelte b/src/lib/components/RelationshipGraph.svelte
new file mode 100644
index 0000000..d9710b4
--- /dev/null
+++ b/src/lib/components/RelationshipGraph.svelte
@@ -0,0 +1,330 @@
+<!-- src/lib/components/RelationshipGraph.svelte
+ Copyright (c) 2026 Clove Nytrix Doughmination Twilight
+ Licensed under the DASL-1.0 Licence.
+ See LICENCE.md in the project root for full licence information.
+-->
+
+<script lang="ts">
+ import { onMount } from "svelte";
+ import type { PluralMember, Relationship } from "doughmination-api/server";
+ import { fallbackAvatar, memberName } from "#lib";
+ import { normalizeColor } from "#lib/colour.ts";
+ import { layout, project } from "#lib/graph.ts";
+
+ let {
+ members,
+ relationships,
+ }: {
+ members: PluralMember[];
+ relationships: Relationship[];
+ } = $props();
+
+ const SIZE = 720;
+ const CENTER = SIZE / 2;
+ const RADIUS = 285;
+ const NODE_RADIUS = 26;
+ const SPIN_PER_FRAME = 0.0025;
+ const DRAG_SPEED = 0.008;
+
+ // Everyone in a relationship, once each, plus the links between them by index
+ const graph = $derived.by(() => {
+ const find = (identifier: string) =>
+ members.find((member) => member.id === identifier || member.name === identifier);
+
+ const nodes: PluralMember[] = [];
+ const indexOf = (identifier: string): number | null => {
+ const member = find(identifier);
+ if (!member) return null;
+ let index = nodes.findIndex((node) => node.id === member.id);
+ if (index === -1) index = nodes.push(member) - 1;
+ return index;
+ };
+
+ const links = relationships.flatMap((edge) => {
+ const a = indexOf(edge.members[0]);
+ const b = indexOf(edge.members[1]);
+ return a === null || b === null || a === b ? [] : [{ edge, a, b }];
+ });
+
+ return {
+ nodes,
+ links,
+ points: layout(
+ nodes.length,
+ links.map((link) => [link.a, link.b] as [number, number]),
+ RADIUS,
+ ),
+ };
+ });
+
+ // A slight tilt from the start, so it reads as 3D before anything moves
+ let yaw = $state(0.6);
+ let pitch = $state(-0.35);
+ let hovered = $state<number | null>(null);
+ let dragging = $state(false);
+ let reduceMotion = $state(false);
+
+ const projected = $derived(graph.points.map((point) => project(point, yaw, pitch, RADIUS)));
+
+ // Painted back to front, so nearer things cover further ones
+ const nodeOrder = $derived(
+ graph.nodes.map((_, index) => index).sort((a, b) => projected[a]!.depth - projected[b]!.depth),
+ );
+ const linkOrder = $derived(
+ [...graph.links].sort(
+ (one, two) =>
+ projected[one.a]!.depth + projected[one.b]!.depth - (projected[two.a]!.depth + projected[two.b]!.depth),
+ ),
+ );
+
+ function linked(index: number): boolean {
+ if (hovered === null) return true;
+ if (index === hovered) return true;
+ return graph.links.some(
+ (link) => (link.a === hovered && link.b === index) || (link.b === hovered && link.a === index),
+ );
+ }
+
+ function screen(index: number) {
+ const point = projected[index]!;
+ return {
+ x: CENTER + point.x,
+ y: CENTER + point.y,
+ r: NODE_RADIUS * point.scale,
+ // Further away is dimmer, but never invisible
+ fade: 0.45 + 0.55 * Math.min(1, Math.max(0, point.depth)),
+ };
+ }
+
+ // Drag to spin. A drag that moved isn't a click, so it doesn't open a member
+ let last: { x: number; y: number } | null = null;
+ let moved = 0;
+
+ function pointerDown(event: PointerEvent) {
+ last = {
+ x: event.clientX,
+ y: event.clientY,
+ };
+ moved = 0;
+ dragging = true;
+ (event.currentTarget as Element).setPointerCapture(event.pointerId);
+ }
+
+ function pointerMove(event: PointerEvent) {
+ if (!last) return;
+ const dx = event.clientX - last.x;
+ const dy = event.clientY - last.y;
+ moved += Math.abs(dx) + Math.abs(dy);
+ yaw += dx * DRAG_SPEED;
+ pitch = Math.max(-1.3, Math.min(1.3, pitch - dy * DRAG_SPEED));
+ last = {
+ x: event.clientX,
+ y: event.clientY,
+ };
+ }
+
+ function pointerUp() {
+ last = null;
+ dragging = false;
+ }
+
+ function swallowDragClick(event: MouseEvent) {
+ if (moved > 4) event.preventDefault();
+ }
+
+ // Arrow keys spin it too
+ function keydown(event: KeyboardEvent) {
+ const step = 0.15;
+ if (event.key === "ArrowLeft") yaw -= step;
+ else if (event.key === "ArrowRight") yaw += step;
+ else if (event.key === "ArrowUp") pitch = Math.max(-1.3, pitch - step);
+ else if (event.key === "ArrowDown") pitch = Math.min(1.3, pitch + step);
+ else return;
+ event.preventDefault();
+ }
+
+ // A slow turn while nobody is interacting
+ onMount(() => {
+ const motion = matchMedia("(prefers-reduced-motion: reduce)");
+ reduceMotion = motion.matches;
+
+ let frame = 0;
+ const tick = () => {
+ if (!reduceMotion && !dragging && hovered === null) yaw += SPIN_PER_FRAME;
+ frame = requestAnimationFrame(tick);
+ };
+ frame = requestAnimationFrame(tick);
+ return () => cancelAnimationFrame(frame);
+ });
+</script>
+
+<!-- Interactive: drag and the arrow keys turn it -->
+<!-- svelte-ignore a11y_no_noninteractive_tabindex, a11y_no_noninteractive_element_interactions -->
+<svg
+ viewBox="0 0 {SIZE} {SIZE}"
+ class="graph"
+ class:dragging
+ role="application"
+ aria-roledescription="rotatable map"
+ aria-label="Relationship map. Drag or use the arrow keys to turn it."
+ tabindex="0"
+ onpointerdown={pointerDown}
+ onpointermove={pointerMove}
+ onpointerup={pointerUp}
+ onpointercancel={pointerUp}
+ onkeydown={keydown}
+>
+ <defs>
+ <radialGradient id="graph-glow">
+ <stop offset="0%" stop-color="var(--accent)" stop-opacity="0.12" />
+ <stop offset="100%" stop-color="var(--accent)" stop-opacity="0" />
+ </radialGradient>
+ {#each graph.nodes as node, index (node.id)}
+ {@const at = screen(index)}
+ <clipPath id="avatar-{node.id}">
+ <circle cx={at.x} cy={at.y} r={at.r} />
+ </clipPath>
+ {/each}
+ </defs>
+
+ <!-- A soft floor of light, so the cluster has something to sit in -->
+ <ellipse cx={CENTER} cy={CENTER} rx={RADIUS + 60} ry={RADIUS + 60} fill="url(#graph-glow)" />
+
+ {#each linkOrder as link (link.edge.id)}
+ {@const from = screen(link.a)}
+ {@const to = screen(link.b)}
+ {@const lit = hovered !== null && (link.a === hovered || link.b === hovered)}
+ <line
+ x1={from.x}
+ y1={from.y}
+ x2={to.x}
+ y2={to.y}
+ class="link"
+ class:crush={link.edge.type === "crush"}
+ class:lit
+ class:dim={hovered !== null && !lit}
+ stroke-opacity={(from.fade + to.fade) / 2}
+ >
+ <title>{memberName(graph.nodes[link.a]!)} & {memberName(graph.nodes[link.b]!)}: {link.edge.type}</title>
+ </line>
+ {/each}
+
+ {#each nodeOrder as index (graph.nodes[index]!.id)}
+ {@const node = graph.nodes[index]!}
+ {@const at = screen(index)}
+ <a
+ href="/{node.name ?? node.id}"
+ class="node"
+ class:dim={!linked(index)}
+ onclick={swallowDragClick}
+ onpointerenter={() => (hovered = index)}
+ onpointerleave={() => (hovered = null)}
+ onfocus={() => (hovered = index)}
+ onblur={() => (hovered = null)}
+ >
+ <g opacity={at.fade}>
+ <circle cx={at.x} cy={at.y} r={at.r + 3} fill="var(--bg)" stroke={normalizeColor(node.color) ?? "var(--accent)"} stroke-width="2.5" />
+ <image
+ href={node.avatar_url || fallbackAvatar}
+ x={at.x - at.r}
+ y={at.y - at.r}
+ width={at.r * 2}
+ height={at.r * 2}
+ clip-path="url(#avatar-{node.id})"
+ preserveAspectRatio="xMidYMid slice"
+ />
+ <text x={at.x} y={at.y + at.r + 16} text-anchor="middle" font-size={12 * projected[index]!.scale + 1}>
+ {memberName(node)}
+ </text>
+ </g>
+ </a>
+ {/each}
+</svg>
+
+<p class="legend muted small">
+ <span><svg width="28" height="8" aria-hidden="true"><line x1="0" y1="4" x2="28" y2="4" class="link" /></svg> partner</span>
+ <span><svg width="28" height="8" aria-hidden="true"><line x1="0" y1="4" x2="28" y2="4" class="link crush" /></svg> crush</span>
+ <span>Drag to turn · hover someone to see their links</span>
+</p>
+
+<style>
+ .graph {
+ display: block;
+ width: 100%;
+ max-width: 720px;
+ margin: 0 auto;
+ cursor: grab;
+ touch-action: none;
+ user-select: none;
+ }
+
+ .graph:focus-visible {
+ border-radius: 1rem;
+ outline: 2px solid var(--accent);
+ }
+
+ .dragging {
+ cursor: grabbing;
+ }
+
+ .link {
+ stroke: var(--accent);
+ stroke-width: 2;
+ stroke-linecap: round;
+ transition: stroke-width 0.15s ease;
+ }
+
+ .crush {
+ stroke: var(--warn);
+ stroke-dasharray: 5 6;
+ }
+
+ .lit {
+ stroke-width: 3.5;
+ }
+
+ .dim {
+ opacity: 0.15;
+ }
+
+ .link.dim {
+ stroke-opacity: 0.12;
+ }
+
+ .node text {
+ fill: var(--text);
+ paint-order: stroke;
+ stroke: var(--bg);
+ stroke-width: 4px;
+ stroke-linejoin: round;
+ }
+
+ .node:focus-visible {
+ outline: none;
+ }
+
+ .node:focus-visible circle {
+ stroke: var(--text);
+ stroke-width: 4;
+ }
+
+ .legend {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 0.5rem 1.25rem;
+ justify-content: center;
+ margin: 0;
+ }
+
+ .legend span {
+ display: inline-flex;
+ gap: 0.4rem;
+ align-items: center;
+ }
+
+ @media (prefers-reduced-motion: reduce) {
+ .link {
+ transition: none;
+ }
+ }
+</style>
diff --git a/src/lib/graph.ts b/src/lib/graph.ts
new file mode 100644
index 0000000..06edfd5
--- /dev/null
+++ b/src/lib/graph.ts
@@ -0,0 +1,139 @@
+/* src/lib/graph.ts
+ * Copyright (c) 2026 Clove Nytrix Doughmination Twilight
+ * Licensed under the DASL-1.0 Licence.
+ * See LICENCE.md in the project root for full licence information.
+ */
+
+// A small 3D force layout for the relationship map. Deterministic: no
+// randomness, so the server render and the browser start from the same picture
+
+export interface Point3 {
+ x: number;
+ y: number;
+ z: number;
+}
+
+export interface Projected {
+ x: number;
+ y: number;
+ // 0 is the back of the scene, 1 the front
+ depth: number;
+ scale: number;
+}
+
+const ITERATIONS = 400;
+const REST_LENGTH = 120;
+const SPRING = 0.04;
+const REPULSION = 60000;
+const GRAVITY = 0.06;
+const START_RADIUS = 220;
+const CAMERA = 900;
+
+/** Spreads the starting points evenly over a sphere (a Fibonacci lattice) */
+function sphere(count: number): Point3[] {
+ const golden = Math.PI * (3 - Math.sqrt(5));
+ return Array.from({ length: count }, (_, index) => {
+ const y = count === 1 ? 0 : 1 - (index / (count - 1)) * 2;
+ const ring = Math.sqrt(1 - y * y);
+ const angle = golden * index;
+ return {
+ x: Math.cos(angle) * ring * START_RADIUS,
+ y: y * START_RADIUS,
+ z: Math.sin(angle) * ring * START_RADIUS,
+ };
+ });
+}
+
+/**
+ * Linked nodes pull together, every node pushes every other away, and a weak
+ * pull to the centre keeps separate couples from drifting off. Returns points
+ * centred on the origin and scaled to fit `radius`.
+ */
+export function layout(count: number, links: [number, number][], radius: number): Point3[] {
+ const points = sphere(count);
+
+ for (let step = 0; step < ITERATIONS; step++) {
+ // Big moves early, settling later
+ const cooling = 1 - step / ITERATIONS;
+ const force = points.map(() => ({ x: 0, y: 0, z: 0 }));
+
+ for (let a = 0; a < count; a++) {
+ for (let b = a + 1; b < count; b++) {
+ const dx = points[a]!.x - points[b]!.x;
+ const dy = points[a]!.y - points[b]!.y;
+ const dz = points[a]!.z - points[b]!.z;
+ const distanceSq = Math.max(dx * dx + dy * dy + dz * dz, 1);
+ const push = REPULSION / distanceSq / Math.sqrt(distanceSq);
+ force[a]!.x += dx * push;
+ force[a]!.y += dy * push;
+ force[a]!.z += dz * push;
+ force[b]!.x -= dx * push;
+ force[b]!.y -= dy * push;
+ force[b]!.z -= dz * push;
+ }
+ }
+
+ for (const [a, b] of links) {
+ const dx = points[b]!.x - points[a]!.x;
+ const dy = points[b]!.y - points[a]!.y;
+ const dz = points[b]!.z - points[a]!.z;
+ const distance = Math.max(Math.hypot(dx, dy, dz), 1);
+ const pull = (SPRING * (distance - REST_LENGTH)) / distance;
+ force[a]!.x += dx * pull;
+ force[a]!.y += dy * pull;
+ force[a]!.z += dz * pull;
+ force[b]!.x -= dx * pull;
+ force[b]!.y -= dy * pull;
+ force[b]!.z -= dz * pull;
+ }
+
+ points.forEach((point, index) => {
+ const f = force[index]!;
+ point.x += (f.x - point.x * GRAVITY) * cooling;
+ point.y += (f.y - point.y * GRAVITY) * cooling;
+ point.z += (f.z - point.z * GRAVITY) * cooling;
+ });
+ }
+
+ // Centre, then scale so the furthest node sits on `radius`
+ const centre = points.reduce(
+ (sum, point) => ({
+ x: sum.x + point.x / count,
+ y: sum.y + point.y / count,
+ z: sum.z + point.z / count,
+ }),
+ { x: 0, y: 0, z: 0 },
+ );
+ const furthest = Math.max(
+ 1,
+ ...points.map((p) => Math.hypot(p.x - centre.x, p.y - centre.y, p.z - centre.z)),
+ );
+ const fit = radius / furthest;
+
+ return points.map((point) => ({
+ x: (point.x - centre.x) * fit,
+ y: (point.y - centre.y) * fit,
+ z: (point.z - centre.z) * fit,
+ }));
+}
+
+/** Rotates by yaw (round the vertical axis) then pitch, and projects with perspective */
+export function project(point: Point3, yaw: number, pitch: number, radius: number): Projected {
+ const cosYaw = Math.cos(yaw);
+ const sinYaw = Math.sin(yaw);
+ const x1 = point.x * cosYaw - point.z * sinYaw;
+ const z1 = point.x * sinYaw + point.z * cosYaw;
+
+ const cosPitch = Math.cos(pitch);
+ const sinPitch = Math.sin(pitch);
+ const y2 = point.y * cosPitch - z1 * sinPitch;
+ const z2 = point.y * sinPitch + z1 * cosPitch;
+
+ const scale = CAMERA / (CAMERA + z2);
+ return {
+ x: x1 * scale,
+ y: y2 * scale,
+ depth: 1 - (z2 + radius) / (2 * radius),
+ scale,
+ };
+}
diff --git a/src/routes/relationships/+page.svelte b/src/routes/relationships/+page.svelte
index 7dcfc77..cc012ee 100644
--- a/src/routes/relationships/+page.svelte
+++ b/src/routes/relationships/+page.svelte
@@ -5,46 +5,24 @@
-->
<script lang="ts">
- import type { PluralMember } from "doughmination-api/server";
- import { fallbackAvatar, memberName } from "#lib";
- import { normalizeColor } from "#lib/colour.ts";
+ import { avatarFallback, fallbackAvatar, memberName } from "#lib";
import PageHead from "#lib/components/PageHead.svelte";
+ import RelationshipGraph from "#lib/components/RelationshipGraph.svelte";
let { data } = $props();
- const SIZE = 720;
- const CENTER = SIZE / 2;
- const RING_RADIUS = 280;
- const NODE_RADIUS = 34;
-
- const byId = $derived.by(() => {
- const lookup = new Map<string, PluralMember>();
- for (const member of data.members) {
- if (member.id) lookup.set(member.id, member);
- if (member.name) lookup.set(member.name, member);
- }
- return lookup;
- });
-
- // Everyone in a relationship, spaced evenly round a ring
- const nodes = $derived.by(() => {
- const involved = new Set(data.relationships.flatMap((edge) => edge.members));
- const resolved = [...involved].map((id) => byId.get(id)).filter((m): m is PluralMember => Boolean(m));
- const unique = [...new Map(resolved.map((m) => [m.id, m])).values()];
-
- return unique.map((member, index) => {
- const angle = (index / Math.max(unique.length, 1)) * Math.PI * 2 - Math.PI / 2;
- return {
- member,
- x: CENTER + RING_RADIUS * Math.cos(angle),
- y: CENTER + RING_RADIUS * Math.sin(angle),
- };
- });
- });
-
- function nodeFor(identifier: string) {
- return nodes.find((node) => node.member.id === identifier || node.member.name === identifier);
+ function find(identifier: string) {
+ return data.members.find((member) => member.id === identifier || member.name === identifier);
}
+
+ // The same links as the map, as a plain list
+ const pairs = $derived(
+ data.relationships.flatMap((edge) => {
+ const a = find(edge.members[0]);
+ const b = find(edge.members[1]);
+ return a && b ? [{ edge, a, b }] : [];
+ }),
+ );
</script>
<svelte:head>
@@ -62,63 +40,68 @@
}}
/>
- {#if nodes.length === 0}
+ {#if pairs.length === 0}
<p class="panel muted">No relationships to show yet</p>
{:else}
- <section class="panel map">
- <svg viewBox="0 0 {SIZE} {SIZE}" width="100%" role="img" aria-label="Relationship map">
- <defs>
- {#each nodes as node (node.member.id)}
- <clipPath id="clip-{node.member.id}">
- <circle cx={node.x} cy={node.y} r={NODE_RADIUS} />
- </clipPath>
- {/each}
- </defs>
-
- {#each data.relationships as edge (edge.id)}
- {@const from = nodeFor(edge.members[0])}
- {@const to = nodeFor(edge.members[1])}
- {#if from && to}
- <line x1={from.x} y1={from.y} x2={to.x} y2={to.y} stroke="var(--accent)" stroke-width="2" stroke-opacity="0.55" />
- <text x={(from.x + to.x) / 2} y={(from.y + to.y) / 2 - 4} text-anchor="middle" font-size="11" fill="var(--muted)">
- {edge.type}
- </text>
- {/if}
- {/each}
+ <section class="panel">
+ <RelationshipGraph members={data.members} relationships={data.relationships} />
+ </section>
- {#each nodes as node (node.member.id)}
- <a href="/{node.member.name ?? node.member.id}">
- <circle
- cx={node.x}
- cy={node.y}
- r={NODE_RADIUS + 2}
- fill="var(--bg)"
- stroke={normalizeColor(node.member.color) ?? "var(--accent)"}
- stroke-width="2"
- />
- <image
- href={node.member.avatar_url || fallbackAvatar}
- x={node.x - NODE_RADIUS}
- y={node.y - NODE_RADIUS}
- width={NODE_RADIUS * 2}
- height={NODE_RADIUS * 2}
- clip-path="url(#clip-{node.member.id})"
- preserveAspectRatio="xMidYMid slice"
- />
- <text x={node.x} y={node.y + NODE_RADIUS + 16} text-anchor="middle" font-size="13" fill="var(--text)">
- {memberName(node.member)}
- </text>
- </a>
+ <section class="panel">
+ <h2>Everyone, in pairs</h2>
+ <ul class="pairs">
+ {#each pairs as pair (pair.edge.id)}
+ <li>
+ <a class="who" href="/{pair.a.name ?? pair.a.id}">
+ <img class="member-avatar avatar-sm" src={pair.a.avatar_url || fallbackAvatar} alt="" onerror={avatarFallback} />{memberName(pair.a)}
+ </a>
+ <span class="badge" class:accent={pair.edge.type !== "crush"}>{pair.edge.type}</span>
+ <a class="who" href="/{pair.b.name ?? pair.b.id}">
+ <img class="member-avatar avatar-sm" src={pair.b.avatar_url || fallbackAvatar} alt="" onerror={avatarFallback} />{memberName(pair.b)}
+ </a>
+ {#if pair.edge.since}<span class="muted small">since {pair.edge.since}</span>{/if}
+ </li>
{/each}
- </svg>
+ </ul>
</section>
{/if}
</div>
<style>
- .map svg {
- display: block;
- max-width: 720px;
- margin: 0 auto;
+ h2 {
+ margin: 0;
+ font-size: 1.05rem;
+ }
+
+ .pairs {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(22rem, 1fr));
+ gap: 0.6rem 1.5rem;
+ margin: 0;
+ padding: 0;
+ list-style: none;
+ }
+
+ .pairs li {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 0.5rem;
+ align-items: center;
+ }
+
+ .who {
+ display: inline-flex;
+ gap: 0.45rem;
+ align-items: center;
+ text-decoration: none;
+ }
+
+ .who:hover {
+ color: var(--accent);
+ }
+
+ .avatar-sm {
+ width: 1.75rem;
+ height: 1.75rem;
}
</style>