From 3a52634929d4307ead68d2deac2e1c6d9ad551d0 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Sun, 6 Dec 2026 09:59:54 +0000 Subject: [PATCH] Show the tool section as the SQLStack mark, with SQLBuild, SQLTide and SQLLoad as its brim --- index.html | 6 +- stack.js | 192 ++++++++++++++++++++-- styles.css | 22 ++- tests/website.spec.mjs | 21 ++- tools.js | 360 ++++++----------------------------------- 5 files changed, 261 insertions(+), 340 deletions(-) diff --git a/index.html b/index.html index 2c98fb4..e7f7c8f 100644 --- a/index.html +++ b/index.html @@ -28,9 +28,9 @@ - - - + + +
Skip to tools diff --git a/stack.js b/stack.js index a29d60a..893f8e1 100644 --- a/stack.js +++ b/stack.js @@ -139,6 +139,17 @@ dots: flowDots(strip), })); + // In the tool section (data-labels-beside) the mark behaves like a tower of tools: the picked + // layer's neighbours move apart to make room for it, and a click knocks the layer it hits, the + // push running on through the others. Both are moves on screen, so each layer is drawn shifted. + const order = [...discParts.map((part) => part.layer), ...stripParts.map((part) => part.layer)]; // top to bottom + const OPEN = 0.14; // extra room above and below the picked layer, in column radii + const kicks = []; + let bouncing = false; + for (const layer of layers) Object.assign(layer, { o: { x: 0, y: 0, vx: 0, vy: 0 }, tx: 0, ty: 0, spans: [] }); + for (const part of discParts) part.layer.spans.push(part.span); + for (const part of stripParts) part.layer.spans.push(...part.runs.map((run) => run.span)); + let width = 0; let scale = 0; let cx = 0; @@ -146,6 +157,11 @@ let dpr = 1; let ink = [23, 26, 31]; let background = [255, 255, 255]; + let line = [200, 200, 200]; + // With data-labels-beside, every layer's name sits in a column to the right of the mark, joined to + // its layer by a dotted leader, instead of under the stack while it is pointed at. + const beside = root.hasAttribute('data-labels-beside'); + let leaders = []; let angle = 0.6; let last = 0; let frame = 0; @@ -157,6 +173,7 @@ const style = getComputedStyle(document.documentElement); ink = hexToRgb(style.getPropertyValue('--ink')); background = hexToRgb(style.getPropertyValue('--background')); + line = hexToRgb(style.getPropertyValue('--line')); for (const layer of layers) { layer.accent = hexToRgb(getComputedStyle(layer.link).getPropertyValue('--accent')); } @@ -205,10 +222,20 @@ canvas.width = Math.round(width * dpr); canvas.height = Math.round(root.clientHeight * dpr); const floor = width * 0.86; - scale = Math.min((width * 0.94) / (REFERENCE[1] - REFERENCE[0]), (width * 0.8) / (REFERENCE[3] - REFERENCE[2])); - cx = width / 2 - (scale * (REFERENCE[0] + REFERENCE[1])) / 2; - // Stand every stack on the same floor, whatever its height. - cy = floor - scale * own[3]; + let room = 0; + if (beside) { + room = width - Math.max(0, ...links.map((link) => link.querySelector('.label').offsetWidth)) - 44; + const tall = root.clientHeight; + // Inset, so a layer knocked left isn't cut off at the canvas edge. + scale = Math.min((room - 32) / (own[1] - own[0]), (tall * 0.94) / (own[3] - own[2])); + cx = room / 2 - (scale * (own[0] + own[1])) / 2; + cy = tall / 2 - (scale * (own[2] + own[3])) / 2; + } else { + scale = Math.min((width * 0.94) / (REFERENCE[1] - REFERENCE[0]), (width * 0.8) / (REFERENCE[3] - REFERENCE[2])); + cx = width / 2 - (scale * (REFERENCE[0] + REFERENCE[1])) / 2; + // Stand every stack on the same floor, whatever its height. + cy = floor - scale * own[3]; + } const caption = floor + 14; root.style.setProperty('--caption-y', `${caption}px`); for (const link of links) { @@ -227,6 +254,61 @@ for (const [part, run] of runs(false)) run.path = place(run.span, runOutline(part.strip, run)); for (const part of [...discParts].reverse()) part.path = place(part.span, discOutline(part)); for (const [part, run] of runs(true)) run.path = place(run.span, runOutline(part.strip, run)); + if (beside) placeBeside(room); + } + + // Each layer's anchor is its right-hand edge. The names keep the stack's order, top disc to + // bottom strip, at least a line apart, in one column past the widest anchor. + function placeBeside(room) { + const anchors = [ + ...discParts.map((part) => { + const y = cy - scale * (part.top - height / 2) * COS; + part.layer.centre = [cx, y]; + return { layer: part.layer, x: cx + scale, y }; + }), + ...stripParts.map((part) => { + let best = null; + for (let i = 0; i <= SEGMENTS; i += 1) { + const u = i / SEGMENTS; + const [x, y] = project(brimPoint(part.strip, u, BRIM_INNER + widthAt(u, part.strip), -BRIM_THICK / 2)); + if (!best || x > best.x) best = { layer: part.layer, x, y }; + } + part.layer.centre = project(brimPoint(part.strip, 0.5, BRIM_INNER + widthAt(0.5, part.strip) / 2, -BRIM_THICK / 2)); + return best; + }), + ]; + const column = Math.max(room, ...anchors.map((a) => a.x)) + 28; + const gap = 30; + let previous = -Infinity; + for (const anchor of anchors) { + anchor.labelY = Math.max(anchor.y, previous + gap); + previous = anchor.labelY; + } + // Centre the names on their anchors as a group, so pushing some down doesn't drag them all. + const drift = anchors.reduce((sum, a) => sum + a.labelY - a.y, 0) / anchors.length; + for (const anchor of anchors) { + anchor.labelY -= drift; + anchor.labelX = column; + anchor.layer.link.style.setProperty('--label-x', `${column}px`); + anchor.layer.link.style.setProperty('--label-y', `${anchor.labelY + anchor.layer.ty}px`); + anchor.layer.labelY = anchor.labelY; + // A knock towards the names eases to a stop within a few pixels, so no layer reaches the leaders. + anchor.layer.room = 8; + } + leaders = anchors; + } + + function paintLeaders() { + ctx.setLineDash([1, 3]); + ctx.lineWidth = 1; + for (const { layer, x, y, labelX, labelY } of leaders) { + ctx.beginPath(); + ctx.moveTo(x + 8 + layer.tx, y + layer.ty); + ctx.lineTo(labelX - 8, labelY + layer.ty); + ctx.strokeStyle = `rgb(${mix(line, layer.accent, layer.glow)})`; + ctx.stroke(); + } + ctx.setLineDash([]); } // Dots on one strip. The swoosh holds still; each dot slides along it by `phase`, a fraction of @@ -325,21 +407,28 @@ const paintStrips = (front) => { for (let k = stripParts.length - 1; k >= 0; k -= 1) { const part = stripParts[k]; + ctx.save(); + ctx.translate(part.layer.tx, part.layer.ty); ctx.fillStyle = `rgb(${background})`; for (const run of part.runs) if (run.front === front) ctx.fill(run.path); const points = flows[k] .filter(({ point }) => (point[2] >= 0) === front) .map(({ point, light, visible }) => ({ at: project(point), light, visible })); paintDots(part.layer, points, lit); + ctx.restore(); } }; paintStrips(false); for (let i = discParts.length - 1; i >= 0; i -= 1) { + ctx.save(); + ctx.translate(discParts[i].layer.tx, discParts[i].layer.ty); ctx.fillStyle = `rgb(${background})`; ctx.fill(discParts[i].path); paintDisc(discParts[i], lit); + ctx.restore(); } paintStrips(true); + if (beside) paintLeaders(); } function moving() { @@ -353,8 +442,8 @@ function tick(now) { frame = 0; const elapsed = last ? Math.min(now - last, 100) : 0; - // Thirty frames a second is plenty for a slow turn. - if (last && elapsed < 32) { + // Thirty frames a second is plenty for a slow turn; a knock gets sixty. + if (last && elapsed < (bouncing ? 15 : 32)) { frame = requestAnimationFrame(tick); return; } @@ -362,22 +451,95 @@ if (moving()) angle += (elapsed / 1000) * (TAU / TURN_SECONDS); const step = reduced.matches ? 1 : Math.min(1, elapsed / 160) || 0.2; for (const layer of layers) layer.glow += (layer.target - layer.glow) * step; + if (beside) bouncing = move(now, elapsed); draw(); - if (moving() || settling()) frame = requestAnimationFrame(tick); + if (moving() || settling() || bouncing) frame = requestAnimationFrame(tick); else last = 0; } + // Knocked layers spring back with a little overshoot; the rest open around the picked one. + function move(now, elapsed) { + while (kicks.length && kicks[0].at <= now) { + const k = kicks.shift(); + k.o.vx += k.vx; + k.o.vy += k.vy; + } + let still = kicks.length === 0; + const sub = elapsed / 1000 / 4; + for (const { o } of order) { + for (let i = 0; i < 4; i += 1) { + o.vx += (-90 * o.x - 6 * o.vx) * sub; + o.vy += (-90 * o.y - 6 * o.vy) * sub; + o.x += o.vx * sub; + o.y += o.vy * sub; + } + if (Math.abs(o.x) + Math.abs(o.y) + (Math.abs(o.vx) + Math.abs(o.vy)) / 20 > 0.05) still = false; + else o.x = o.y = o.vx = o.vy = 0; + } + order.forEach((layer, i) => { + const shift = order.reduce((sum, other, j) => sum + (j === i ? 0 : (j > i ? 1 : -1) * OPEN * other.glow), 0); + const room = layer.room ?? 12; + layer.tx = layer.o.x > 0 ? room * Math.tanh(layer.o.x / room) : layer.o.x; + layer.ty = layer.o.y - scale * shift * COS; + const transform = `translate(${layer.tx.toFixed(1)}px, ${layer.ty.toFixed(1)}px)`; + for (const span of layer.spans) span.style.transform = transform; + if (layer.labelY !== undefined) layer.link.style.setProperty('--label-y', `${layer.labelY + layer.ty}px`); + }); + return !still; + } + + // The clicked layer is pushed away from the click, and the push runs on through the others. + function knock(clicked, event) { + const box = root.getBoundingClientRect(); + const x = event.clientX - box.left; + const y = event.clientY - box.top; + const near = order.indexOf(clicked); + const [mx, my] = [clicked.centre[0] + clicked.tx, clicked.centre[1] + clicked.ty]; + const reach = Math.max(0.35, 1 - Math.max(0, Math.hypot(x - mx, y - my) - scale * 1.1) / (scale * 2)); + const dx = mx - x; + const dy = (my - y) * 0.5; + const len = Math.hypot(dx, dy) || 1; + const at = performance.now(); + const speed = scale * 3; + order.forEach((layer, i) => { + const f = reach * 0.55 ** Math.abs(i - near); + kicks.push({ o: layer.o, at: at + 60 * Math.abs(i - near), vx: (dx / len) * speed * f, vy: (dy / len) * speed * f }); + }); + kicks.sort((a, b) => a.at - b.at); + bouncing = true; + wake(); + } + function wake() { if (!frame) frame = requestAnimationFrame(tick); } - // The tool being pointed at, in this stack or the other one. - document.addEventListener('stack:active', ({ detail }) => { + // The tool being pointed at, in this stack or the other one, or else the one picked for this + // stack with a stack:pick event on it. + let hovered = null; + let picked = null; + const retarget = () => { + // In the tool section only the picked tool lights, so the pointer passing over the mark on its + // way to the card doesn't flash other layers. + const tool = beside ? picked : hovered ?? picked; for (const layer of layers) { - layer.target = layer.link.dataset.layer === detail.tool ? 1 : 0; - layer.link.classList.toggle('is-active', layer.link === detail.link); + layer.target = layer.link.dataset.layer === tool ? 1 : 0; + layer.link.classList.toggle('is-on', layer.link.dataset.layer === tool); } wake(); + }; + document.addEventListener('stack:active', ({ detail }) => { + hovered = detail.tool; + for (const layer of layers) layer.link.classList.toggle('is-active', layer.link === detail.link); + retarget(); + }); + root.addEventListener('stack:pick', ({ detail }) => { + picked = detail; + retarget(); + }); + document.fonts?.ready.then(() => { + layout(); + draw(); }); const announce = (link) => { @@ -396,6 +558,13 @@ layer.link.addEventListener('click', (event) => { const tap = tapped; tapped = null; + // In the tool section a click on a layer picks its tool and knocks it; its name opens the site. + if (beside && event.target.classList?.contains('hit')) { + event.preventDefault(); + root.dispatchEvent(new CustomEvent('stack:choose', { detail: layer.link.dataset.layer })); + if (!reduced.matches) knock(layer, event); + return; + } if (tap !== layer.link) return; event.preventDefault(); // Safari doesn't focus a tapped link; the tool cards pick a focused layer straight away. @@ -431,6 +600,7 @@ wake(); } + window.SQLStackMark = init; document.addEventListener('DOMContentLoaded', () => { document.querySelectorAll('[data-stack]').forEach(init); }); diff --git a/styles.css b/styles.css index 5d9c998..7bd1241 100644 --- a/styles.css +++ b/styles.css @@ -270,18 +270,14 @@ h1 { margin: 0; max-width: 11ch; font-size: clamp(44px, 5.4vw, 80px); line-heigh /* The tool stack (tools.js): the hero's dotted discs, each named beside it, next to the picked tool's card. */ .tool-x { display: grid; grid-template-columns: minmax(0, .9fr) minmax(0, 1fr); align-items: center; gap: 48px; } .tool-x-stack { display: grid; } -/* Clipped, so names placed for the previous width can't widen the page in the frame before tools.js moves them. */ -.tool-x-discs { position: relative; width: 100%; max-width: 520px; justify-self: center; overflow: clip; overflow-clip-margin: 8px; } -.tool-x-discs canvas { position: absolute; inset: 0; width: 100%; height: 100%; -webkit-tap-highlight-color: transparent; } -.tool-x-chips { position: absolute; inset: 0; pointer-events: none; } -/* Names in the stack's order; tools.js places each beside its disc and keeps it within the gap between discs. */ -.tool-x-chip { position: absolute; left: var(--label-x); display: inline-flex; align-items: center; gap: 8px; min-height: 32px; padding: 0 2px; color: var(--muted); font: 700 16px "Space Grotesk", sans-serif; text-decoration: none; white-space: nowrap; pointer-events: auto; transform: translateY(-50%); transition: color .2s ease; } -.tool-x-chip::before { content: ""; position: absolute; right: calc(100% + 6px); top: 50%; width: 10px; border-top: 1px dotted var(--line); } -.tool-x-chip:hover { color: var(--ink); } -.tool-x-chip.is-on { color: var(--accent); } -.tool-x-chip.is-on::before { width: 14px; border-top-color: var(--accent); } -.tool-x-chip img { width: 20px; height: 20px; } -.tool-x-chip img.wide { width: 24px; } +/* Clipped, so names placed for the previous width can't widen the page in the frame before stack.js moves them. */ +.tool-mark { width: 100%; max-width: 520px; justify-self: center; aspect-ratio: 5 / 4; overflow: clip; overflow-clip-margin: 8px; } +.tool-mark .stack-labels .label { flex-direction: row; gap: 8px; transform: translateY(-50%); opacity: 1; pointer-events: auto; } +.tool-mark .stack-labels .name { color: var(--muted); font-size: 16px; transition: color .2s ease; } +.tool-mark .stack-labels a:hover .name { color: var(--ink); } +.tool-mark .stack-labels a.is-on .name { color: var(--accent); } +.tool-mark .stack-labels img { width: 20px; height: 20px; } +.tool-mark .stack-labels img.wide { width: 24px; } /* Every card sits in the same cell, so the section is as tall as the longest card and nothing below jumps; each card keeps its own height and starts at the top, so a short one has no empty terminal. */ /* With scripts on, the plain grid would show for a frame and then squash into the right-hand column. @@ -300,5 +296,5 @@ h1 { margin: 0; max-width: 11ch; font-size: clamp(44px, 5.4vw, 80px); line-heigh .is-exploded .tool:not(.is-active) { display: none; } } @media (prefers-reduced-motion: reduce) { - .tool-x-chip, .is-exploded .tool, .is-exploded .tool.is-active { transition: none; } + .tool-mark .stack-labels .name, .is-exploded .tool, .is-exploded .tool.is-active { transition: none; } } diff --git a/tests/website.spec.mjs b/tests/website.spec.mjs index a6c4342..368da1b 100644 --- a/tests/website.spec.mjs +++ b/tests/website.spec.mjs @@ -83,7 +83,7 @@ test('tools, stack layers, sharing assets and responsive layout work in both the const batch = page.getByRole('navigation', { name: 'Batch tools' }); const streaming = page.getByRole('navigation', { name: 'Streaming tools' }); for (const [name, website] of tools) { - // One card shows at a time; focusing a tool's name beside the discs picks it, and the name links to its site. + // One card shows at a time; focusing a tool's name beside the mark picks it, and the name links to its site. const label = names.getByRole('link', { name, exact: true }); await expect(label).toHaveAttribute('href', website); await label.focus(); @@ -223,13 +223,13 @@ for (const input of ['touch', 'mouse']) { }); } -test('a tool name beside the discs shows its card when pointed at and opens its site on a click', async ({ browser }) => { +test('a tool name beside the mark shows its card when pointed at and opens its site on a click, and a click on a layer picks it', async ({ browser }) => { const context = await browser.newContext({ viewport: { width: 1280, height: 800 } }); await context.route('https://sqltide.io/**', (route) => route.fulfill({ body: 'SQLTide' })); const page = await context.newPage(); await page.goto('/#tools'); const name = page.getByRole('navigation', { name: 'All tools' }).getByRole('link', { name: 'SQLTide', exact: true }); - const box = await name.boundingBox(); + const box = await name.locator('.label').boundingBox(); await page.mouse.move(box.x + box.width / 2, box.y - 40); await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2, { steps: 4 }); await expect(name).toHaveClass(/is-on/); @@ -239,7 +239,20 @@ test('a tool name beside the discs shows its card when pointed at and opens its await names.first().focus(); for (let i = 0; i < 6; i += 1) await page.keyboard.press('Tab', { delay: 20 }); await expect(page.getByRole('link', { name: 'SQLLoad website', exact: true })).toBeFocused(); - await name.click(); + // A click on a layer of the mark picks its tool and stays on the page. + const disc = await page.locator('.tool-mark a[data-layer="fensu"]').evaluate((link) => { + const box = link.getBoundingClientRect(); + for (let y = box.top + 1; y < box.bottom; y += 3) { + for (let x = box.left + 1; x < box.right; x += 3) { + if (document.elementFromPoint(x, y)?.matches('.hit') && document.elementFromPoint(x, y).parentElement === link) return [x, y]; + } + } + throw new Error('no visible point on the Fensu disc'); + }); + await page.mouse.click(...disc); + await expect(page.locator('article.tool.fensu')).toHaveClass(/is-active/); + await expect(page).toHaveURL(/#tools$/); + await name.locator('.label').click(); await page.waitForURL('https://sqltide.io/'); await context.close(); }); diff --git a/tools.js b/tools.js index b5675f5..2de6ae7 100644 --- a/tools.js +++ b/tools.js @@ -1,11 +1,13 @@ -// The tool section as the hero's dotted discs, opened up: one disc per tool, with its name beside -// it, next to the card for the tool that is picked. The picked disc lights in its tool's colour -// and the stack opens around it; a click on the stack knocks the disc it hits. Each name links to -// its tool's site. Without this script the cards show as a plain grid. +// The tool section as the SQLStack mark (stack.js), opened up: SQLBuild, SQLTide and SQLLoad as the +// brim's strips, the other tools as the discs above them, each named beside its layer, next to the +// card for the tool that is picked. The picked layer lights in its tool's colour and the stack opens +// around it; a click on the mark knocks the layer it hits. Each name links to its tool's site. +// Without this script the cards show as a plain grid. (() => { - const TOWER = ['sqlload', 'sqltide', 'sqlbuild', 'sqlbabel', 'fensu', 'sqlclean']; // bottom to top + const TOWER = ['sqlclean', 'fensu', 'sqlbabel', 'sqlbuild', 'sqltide', 'sqlload']; // top to bottom + const BRIM = ['sqlbuild', 'sqltide', 'sqlload']; const CYCLE_MS = 4500; - const DWELL_MS = 50; // how long the pointer stays on a disc before it takes over + const DWELL_MS = 50; // how long the pointer stays on a layer before it takes over function init(section) { const grid = section.querySelector('.tool-grid'); @@ -38,310 +40,56 @@ layout.className = 'tool-x'; const stack = document.createElement('div'); stack.className = 'tool-x-stack'; - const tower = document.createElement('div'); - tower.className = 'tool-x-discs'; + const mark = document.createElement('div'); + mark.className = 'stack-visual tool-mark'; + mark.setAttribute('data-labels-beside', ''); const canvas = document.createElement('canvas'); canvas.setAttribute('aria-hidden', 'true'); - tower.append(canvas); - const ctx = canvas.getContext('2d'); - const TILT = 0.5; - const SIN = Math.sin(TILT); - const COS = Math.cos(TILT); - const H = 0.12; // disc thickness - const G = 0.3; // gap between discs at rest - const OPEN = 0.14; // extra room above and below the picked disc - const SPOKES = 96; - const ROWS = 3; - const RINGS = 5; - const TURN_SECONDS = 48; - const LIGHT = [-0.62, -0.55, 0.56].map((c, _, v) => c / Math.hypot(...v)); - const n = TOWER.length; - const total = n * H + (n - 1) * G; - // Top to bottom, as drawn; a column of discs on a wider base, like the hero. - const discs = [...TOWER].reverse().map((key, t) => ({ - key, - t, - radius: t === n - 1 ? 1 : 0.8 + (0.06 * t) / (n - 2), - top: -total / 2 + t * (H + G), - glow: 0, - target: 0, - shift: 0, - o: { x: 0, y: 0, vx: 0, vy: 0 }, - })); - const kicks = []; - let width = 0; - let height = 0; - let scale = 0; - let cx = 0; - let cy = 0; - let dpr = 1; - let ink = [23, 26, 31]; - let bg = [255, 255, 255]; - // On a light page the dots are drawn larger and more opaque, or the discs fade into the white. - let weight = { size: 1, alpha: 0.3 }; - let angle = 0.6; - let last = 0; - let frame = 0; - let onScreen = true; - const rgb = (value) => { - let hex = value.trim().replace('#', ''); - if (hex.length === 3) hex = hex.replace(/./g, '$&$&'); - const v = parseInt(hex, 16); - return [(v >> 16) & 255, (v >> 8) & 255, v & 255]; - }; - const mix = (a, b, k) => a.map((c, i) => Math.round(c + (b[i] - c) * k)); - - // Each tool's name beside its disc, top to bottom, links to its site. Pointing at it or - // focusing it lights the disc and shows the card; clicking it opens the site. - const chips = document.createElement('nav'); - chips.className = 'tool-x-chips'; - chips.setAttribute('aria-label', 'All tools'); - for (const disc of discs) { - const tool = byKey(disc.key); - const chip = document.createElement('a'); - chip.href = tool.article.querySelector('.tool-head').getAttribute('href'); - chip.className = `tool-x-chip ${tool.key}`; - chip.dataset.tool = tool.key; - chip.append(...logos(tool), tool.name); - chip.addEventListener('focus', () => pick(tool.key, true)); - chip.addEventListener('pointermove', (event) => { - if (event.pointerType === 'mouse' && moved) aim(tool.key, event); - }); - chips.append(chip); + const names = document.createElement('nav'); + names.className = 'stack-labels'; + names.setAttribute('aria-label', 'All tools'); + for (const key of TOWER) { + const tool = byKey(key); + const link = document.createElement('a'); + link.href = tool.article.querySelector('.tool-head').getAttribute('href'); + link.className = key; + link.dataset.layer = key; + if (BRIM.includes(key)) link.setAttribute('data-brim', ''); + const label = document.createElement('span'); + label.className = 'label'; + const name = document.createElement('span'); + name.className = 'name'; + name.textContent = tool.name; + label.append(...logos(tool), name); + link.append(label); + names.append(link); } - tower.append(chips); - stack.append(tower); + mark.append(canvas, names); + stack.append(mark); + grid.before(layout); + layout.append(stack, grid); + section.classList.add('is-exploded'); + window.SQLStackMark(mark); - function colours() { - const style = getComputedStyle(document.documentElement); - ink = rgb(style.getPropertyValue('--ink')); - bg = rgb(style.getPropertyValue('--background')); - weight = bg[0] + bg[1] + bg[2] > 3 * 128 ? { size: 1.3, alpha: 0.75 } : { size: 1, alpha: 0.3 }; - for (const disc of discs) disc.accent = rgb(getComputedStyle(chips.querySelector(`[data-tool="${disc.key}"]`)).getPropertyValue('--accent')); - } - function sizeDiscs() { - width = tower.clientWidth; - if (!width) return; - // Centre the discs and their names together, and shrink the discs when the names need room. - const names = Math.max(...[...chips.children].map((chip) => chip.offsetWidth)); - scale = Math.max(40, Math.min(170, width * 0.3, (width - names - 38) / 2.06)); - cx = Math.max(0, (width - (scale * 2.06 + 30 + names)) / 2) + scale * 1.03; - height = Math.ceil(scale * ((total + 2 * OPEN) * COS + 2 * SIN) + 24); - cy = height / 2; - tower.style.height = `${height}px`; - dpr = Math.min(devicePixelRatio || 1, 2); - canvas.width = Math.round(width * dpr); - canvas.height = Math.round(height * dpr); - // Far enough out that even the lit leader stays clear of the widest disc's dots. - tower.style.setProperty('--label-x', `${cx + scale * 1.03 + 30}px`); - } - function placeLabels() { - // Each name is no taller than the gap between discs, so neighbouring names never overlap. - const room = `${Math.min(32, scale * (H + G) * COS - 2)}px`; - for (const disc of discs) { - const chip = chips.querySelector(`[data-tool="${disc.key}"]`); - chip.style.minHeight = room; - chip.style.top = `${cy + disc.o.y + scale * (disc.top + disc.shift + H / 2) * COS}px`; - } - } - function dots(points, size, colour, alpha) { - ctx.beginPath(); - for (let p = 0; p < points.length; p += 2) { - ctx.moveTo(points[p] + size, points[p + 1]); - ctx.arc(points[p], points[p + 1], size, 0, Math.PI * 2); - } - ctx.globalAlpha = alpha; - ctx.fillStyle = `rgb(${colour})`; - ctx.fill(); - ctx.globalAlpha = 1; - } - function draw() { - if (!width) return; - ctx.setTransform(dpr, 0, 0, dpr, 0, 0); - ctx.clearRect(0, 0, width, height); - const dot = scale / 150; - const lit = Math.max(...discs.map((d) => d.glow)); - const baseCx = cx; - const baseCy = cy; - for (let t = n - 1; t >= 0; t -= 1) { - const d = discs[t]; - const r = d.radius * scale; - const top = d.top + d.shift; - // Towards the names, a knocked disc eases to a stop short of their dashes. - const room = scale * (1.03 - d.radius) + 6; - const cx = baseCx + (d.o.x > 0 ? room * Math.tanh(d.o.x / room) : d.o.x); - const cy = baseCy + d.o.y; - const topY = cy + scale * top * COS; - const botY = cy + scale * (top + H) * COS; - ctx.beginPath(); - ctx.ellipse(cx, topY, r, r * SIN, 0, Math.PI, Math.PI * 2); - ctx.lineTo(cx + r, botY); - ctx.ellipse(cx, botY, r, r * SIN, 0, 0, Math.PI); - ctx.closePath(); - ctx.fillStyle = `rgb(${bg})`; - ctx.fill(); - const base = mix(ink, d.accent, 0.35); // each disc faintly in its tool's colour - const colour = mix(base, d.accent, d.glow); - const fade = 1 - 0.5 * (lit - d.glow); - const buckets = [[], [], [], [], []]; - const add = (x, y, light) => buckets[Math.min(4, Math.floor(light * 5))].push(x, y); - for (let s = 0; s < SPOKES; s += 1) { - const th = (s / SPOKES) * Math.PI * 2 + angle; - const nx = Math.cos(th); - const nz = Math.sin(th); - if (nz < -0.05) continue; - const light = 0.2 + 0.8 * Math.max(0, nx * LIGHT[0] + nz * LIGHT[2]); - for (let row = 0; row < ROWS; row += 1) add(cx + r * nx, cy + scale * (top + ((row + 0.5) / ROWS) * H) * COS + r * nz * SIN, light); - } - const cap = 0.25 + 0.75 * Math.max(0, -LIGHT[1]); - for (let ring = 1; ring <= RINGS; ring += 1) { - const rho = ring / RINGS; - const m = Math.round(SPOKES * rho); - for (let s = 0; s < m; s += 1) { - const th = (s / m) * Math.PI * 2 + angle; - const nx = Math.cos(th); - const nz = Math.sin(th); - add(cx + r * rho * nx, topY + r * rho * nz * SIN, cap * (0.75 + (0.25 * rho * (nx * LIGHT[0] + nz * LIGHT[2] + 1)) / 2)); - } - } - buckets.forEach((pts, b) => { - if (!pts.length) return; - const l = (b + 0.5) / 5; - dots(pts, dot * weight.size * (0.45 + 1.1 * l) * (1 + 0.25 * d.glow), colour, Math.min(1, (weight.alpha + (1 - weight.alpha) * l) * fade)); - }); - } - } - // Which disc a point is on, as drawn: the upper discs are drawn last, so they win where two - // overlap. The picked disc never moves and its neighbours move away from it, so this can't flicker. - function discAt(x, y) { - for (let t = 0; t < n; t += 1) { - const d = discs[t]; - const r = d.radius * scale; - const dx = x - cx; - if (Math.abs(dx) > r) continue; - const e = r * SIN * Math.sqrt(1 - (dx / r) ** 2); - const top = d.top + d.shift; - if (y >= cy + scale * top * COS - e && y <= cy + scale * (top + H) * COS + e) return d.key; - } - return null; - } - function tick(t) { - frame = 0; - const dt = last ? Math.min(t - last, 100) : 0; - if (last && dt < 15) { - frame = requestAnimationFrame(tick); - return; - } - last = t; - const moving = !reduced.matches && onScreen && !document.hidden; - if (moving) angle += (dt / 1000) * ((Math.PI * 2) / TURN_SECONDS); - const step = reduced.matches ? 1 : Math.min(1, dt / 90) || 0.3; - for (const d of discs) d.glow += (d.target - d.glow) * step; - // Discs above the picked one rise and the ones below it sink, by how lit it is. - for (const d of discs) { - d.shift = discs.reduce((sum, o) => sum + (o.t === d.t ? 0 : (o.t > d.t ? -1 : 1) * OPEN * o.glow), 0); - } - // Knocked discs spring back, with a little overshoot. - while (kicks.length && kicks[0].at <= t) { - const k = kicks.shift(); - k.o.vx += k.vx; - k.o.vy += k.vy; - } - let bouncing = kicks.length > 0; - const sub = dt / 1000 / 4; - for (const d of discs) { - const o = d.o; - for (let i = 0; i < 4; i += 1) { - o.vx += (-90 * o.x - 6 * o.vx) * sub; - o.vy += (-90 * o.y - 6 * o.vy) * sub; - o.x += o.vx * sub; - o.y += o.vy * sub; - } - if (Math.abs(o.x) + Math.abs(o.y) + (Math.abs(o.vx) + Math.abs(o.vy)) / 20 > 0.05) bouncing = true; - else o.x = o.y = o.vx = o.vy = 0; - } - placeLabels(); - draw(); - const settling = discs.some((d) => Math.abs(d.glow - d.target) > 0.005); - if (moving || settling || bouncing) frame = requestAnimationFrame(tick); - else last = 0; - } - const wake = () => { - if (!frame) frame = requestAnimationFrame(tick); - }; - const point = (event) => { - const box = canvas.getBoundingClientRect(); - return [event.clientX - box.left, event.clientY - box.top]; - }; - canvas.addEventListener('pointermove', (event) => { - const key = discAt(...point(event)); - canvas.style.cursor = key ? 'pointer' : ''; + // A layer or name of the mark under the pointer, if any. + const layerAt = (x, y) => document.elementFromPoint(x, y)?.closest('.tool-mark a[data-layer]')?.dataset.layer; + mark.addEventListener('pointermove', (event) => { + const key = event.target.closest?.('a[data-layer]')?.dataset.layer; if (key && event.pointerType === 'mouse' && moved) aim(key, event); }); - canvas.addEventListener('click', (event) => { - const [x, y] = point(event); - const key = discAt(x, y); - if (key) pick(key, true); - if (reduced.matches) return; - knock(x, y); - wake(); - }); - // The disc nearest the click is pushed away from it, and the push runs on through the others. - function knock(x, y) { - const at = performance.now(); - const V = scale * 3; - const mid = discs.map((d) => cy + scale * (d.top + d.shift + H / 2) * COS); - const near = mid.reduce((b, m, i) => (Math.abs(m - y) < Math.abs(mid[b] - y) ? i : b), 0); - const reach = Math.max(0.35, 1 - Math.max(0, Math.hypot(x - cx, y - mid[near]) - scale * 1.1) / (scale * 2)); - const dx = cx - x; - const dy = (mid[near] - y) * 0.5; - const len = Math.hypot(dx, dy) || 1; - discs.forEach((d, i) => { - const f = reach * 0.55 ** Math.abs(i - near); - kicks.push({ o: d.o, at: at + 60 * Math.abs(i - near), vx: (dx / len) * V * f, vy: (dy / len) * V * f }); - }); - kicks.sort((a, b) => a.at - b.at); - } - const discUnder = (x, y) => { - const box = canvas.getBoundingClientRect(); - return discAt(x - box.left, y - box.top); - }; - // Colours are read once the stack is on the page; a detached element has no styles. - new ResizeObserver(() => { - colours(); - sizeDiscs(); - placeLabels(); - draw(); - }).observe(tower); - // The names' font can arrive after the first layout and change how much room they need. - document.fonts?.ready.then(() => { - sizeDiscs(); - placeLabels(); - draw(); + mark.addEventListener('pointerleave', () => { + clearTimeout(dwell); + candidate = null; + from = null; }); - new IntersectionObserver(([entry]) => { - onScreen = entry.isIntersecting; - wake(); - }).observe(tower); - new MutationObserver(() => { - colours(); - draw(); - }).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] }); - document.addEventListener('visibilitychange', wake); - reduced.addEventListener('change', wake); - const lightUp = (key) => { - for (const d of discs) d.target = d.key === key ? 1 : 0; - wake(); - }; - grid.before(layout); - layout.append(stack, grid); - section.classList.add('is-exploded'); + // A click on a layer picks its tool (stack.js knocks it). + mark.addEventListener('stack:choose', ({ detail }) => pick(detail, true)); let current = null; let timer = null; let visible = false; - // Hover picks a disc once the pointer has stayed on it for DWELL_MS, and not while the pointer + // Hover picks a layer once the pointer has stayed on it for DWELL_MS, and not while the pointer // is heading for the card, so moving across to the card doesn't switch tools on the way. let candidate = null; let since = 0; @@ -386,21 +134,14 @@ // Check again shortly: by then the pointer has either stopped here or moved on. dwell = setTimeout(() => { from = null; - const under = discUnder(lastX, lastY) || document.elementFromPoint(lastX, lastY)?.closest('.tool-x-chip')?.dataset.tool; - if (candidate && under === candidate) pick(candidate, true); + if (candidate && layerAt(lastX, lastY) === candidate) pick(candidate, true); }, heading ? DWELL_MS * 2 : due); } - canvas.addEventListener('pointerleave', () => { - clearTimeout(dwell); - candidate = null; - from = null; - }); function pick(key, byPerson) { if (byPerson) stopCycle(); if (key === current) return; current = key; - lightUp(key); - for (const chip of chips.children) chip.classList.toggle('is-on', chip.dataset.tool === key); + mark.dispatchEvent(new CustomEvent('stack:pick', { detail: key })); for (const tool of tools) { tool.article.classList.toggle('is-active', tool.key === key); // A card fading out stays visible for a moment; inert keeps Tab from landing in it. @@ -422,11 +163,12 @@ } reduced.addEventListener('change', () => { if (reduced.matches) stopCycle(); }); new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; }, { threshold: 0.4 }).observe(layout); - // Pointing at a layer in the hero shows that tool here too. + // Focusing a name here, or pointing at a layer in the hero, shows that tool. Pointing at this + // mark goes through aim() above instead. document.addEventListener('stack:active', ({ detail }) => { if (!detail.tool || !byKey(detail.tool)) return; if (detail.link === document.activeElement) pick(detail.tool, true); - else ifPointerMoved(() => pick(detail.tool, true)); + else if (!mark.contains(detail.link)) ifPointerMoved(() => pick(detail.tool, true)); }); pick(tools[0].key, false); }