Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
<link rel="preload" href="assets/fonts/space-grotesk-bold.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="assets/fonts/ibm-plex-mono-latin-400-normal.woff2" as="font" type="font/woff2" crossorigin>
<script src="theme.js?v=c4917c18"></script>
<link rel="stylesheet" href="styles.css?v=1f00e5fb">
<script src="stack.js?v=1e7638c2" defer></script>
<script src="tools.js?v=c7575a53" defer></script>
<link rel="stylesheet" href="styles.css?v=091cca93">
<script src="stack.js?v=5869395c" defer></script>
<script src="tools.js?v=61399069" defer></script>
</head>
<body>
<a class="skip-link" href="#tools">Skip to tools</a>
Expand Down
192 changes: 181 additions & 11 deletions stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,29 @@
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;
let cy = 0;
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;
Expand All @@ -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'));
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -353,31 +442,104 @@
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;
}
last = now;
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) => {
Expand All @@ -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.
Expand Down Expand Up @@ -431,6 +600,7 @@
wake();
}

window.SQLStackMark = init;
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('[data-stack]').forEach(init);
});
Expand Down
22 changes: 9 additions & 13 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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; }
}
21 changes: 17 additions & 4 deletions tests/website.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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/);
Expand All @@ -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();
});
Loading