Repository navigation
Expand file tree
/
Copy pathstack.js
More file actions
265 lines (243 loc) · 9.78 KB
/
Copy pathstack.js
File metadata and controls
265 lines (243 loc) · 9.78 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// The hero stacks: dot-shaded discs turning slowly, one per tool.
// Each layer has a real link laid over it, so hover, click and keyboard focus
// all come from the links; the canvas only draws. A tool in both stacks lights
// up in both.
(() => {
const TILT = 0.42; // camera elevation in radians
const SIN = Math.sin(TILT);
const COS = Math.cos(TILT);
const HEIGHT = 0.12; // disc thickness, in units of the base radius
const GAP = 0.105;
const TURN_SECONDS = 48;
const SPOKES = 96;
const ROWS = 3;
const RINGS = 5;
const LIGHT = (() => {
const v = [-0.62, -0.55, 0.56];
const n = Math.hypot(...v);
return v.map((c) => c / n);
})();
function hexToRgb(value) {
const hex = value.trim().replace('#', '');
const full = hex.length === 3 ? hex.split('').map((c) => c + c).join('') : hex;
const n = parseInt(full, 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
function mix(a, b, t) {
return a.map((c, i) => Math.round(c + (b[i] - c) * t));
}
function init(root) {
const canvas = root.querySelector('canvas');
const links = [...root.querySelectorAll('[data-layer]')];
const ctx = canvas.getContext('2d');
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)');
const count = links.length;
const total = count * HEIGHT + (count - 1) * GAP;
const layers = links.map((link, i) => ({
link,
// Like the mark: a column of discs on a wider base.
radius: i === count - 1 ? 1 : 0.8 + (0.06 * i) / Math.max(1, count - 2),
top: -total / 2 + i * (HEIGHT + GAP),
glow: 0,
target: 0,
}));
let width = 0;
let height = 0;
let scale = 0;
let cx = 0;
let cy = 0;
let dpr = 1;
let ink = [23, 26, 31];
let background = [255, 255, 255];
let angle = 0.6;
let last = 0;
let frame = 0;
let onScreen = true;
function readColours() {
const style = getComputedStyle(document.documentElement);
ink = hexToRgb(style.getPropertyValue('--ink'));
background = hexToRgb(style.getPropertyValue('--background'));
for (const layer of layers) {
layer.accent = hexToRgb(getComputedStyle(layer.link).getPropertyValue('--accent'));
}
}
function layout() {
width = root.clientWidth;
height = root.clientHeight;
if (!width) return;
dpr = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = Math.round(width * dpr);
canvas.height = Math.round(height * dpr);
scale = width * 0.4;
cx = width / 2;
// Stand every stack on the same floor, whatever its height.
const floor = width * 0.86;
cy = floor - scale * ((total / 2) * COS + SIN);
const caption = floor + 14;
root.style.setProperty('--caption-y', `${caption}px`);
// Each link spans its layer, from where the layer above hides it to its own front edge.
let edge = cy + scale * (layers[0].top * COS - layers[0].radius * SIN);
for (const layer of layers) {
const bottom = cy + scale * ((layer.top + HEIGHT) * COS + layer.radius * SIN);
const half = layer.radius * scale;
Object.assign(layer.link.style, {
left: `${cx - half}px`,
width: `${half * 2}px`,
top: `${edge}px`,
height: `${bottom - edge}px`,
});
layer.link.style.setProperty('--label-x', `${half}px`);
layer.link.style.setProperty('--label-y', `${caption - edge}px`);
edge = bottom;
}
}
function draw() {
if (!width) return;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const lit = Math.max(...layers.map((layer) => layer.glow));
const dot = scale / 165;
for (let i = count - 1; i >= 0; i -= 1) {
const layer = layers[i];
const r = layer.radius * scale;
const topY = cy + scale * layer.top * COS;
const bottomY = cy + scale * (layer.top + HEIGHT) * COS;
// Hide whatever this layer covers before drawing its dots.
ctx.beginPath();
ctx.ellipse(cx, topY, r, r * SIN, 0, Math.PI, Math.PI * 2);
ctx.lineTo(cx + r, bottomY);
ctx.ellipse(cx, bottomY, r, r * SIN, 0, 0, Math.PI);
ctx.closePath();
ctx.fillStyle = `rgb(${background})`;
ctx.fill();
const colour = mix(ink, layer.accent, layer.glow);
const fade = 1 - 0.55 * (lit - layer.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 theta = (s / SPOKES) * Math.PI * 2 + angle;
const nx = Math.cos(theta);
const nz = Math.sin(theta);
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) {
const y = layer.top + ((row + 0.5) / ROWS) * HEIGHT;
add(cx + r * nx, cy + scale * (y * COS + layer.radius * nz * SIN), light);
}
}
const capLight = 0.25 + 0.75 * Math.max(0, -LIGHT[1]);
for (let ring = 1; ring <= RINGS; ring += 1) {
const rho = ring / RINGS;
const spokes = Math.round(SPOKES * rho);
for (let s = 0; s < spokes; s += 1) {
const theta = (s / spokes) * Math.PI * 2 + angle;
const nx = Math.cos(theta);
const nz = Math.sin(theta);
// A little shading towards the light keeps the cap from reading flat.
const light = capLight * (0.75 + 0.25 * rho * (nx * LIGHT[0] + nz * LIGHT[2] + 1) / 2);
add(cx + r * rho * nx, topY + r * rho * nz * SIN, light);
}
}
buckets.forEach((points, b) => {
if (!points.length) return;
const light = (b + 0.5) / 5;
const radius = dot * (0.45 + 1.1 * light) * (1 + 0.25 * layer.glow);
ctx.beginPath();
for (let p = 0; p < points.length; p += 2) {
ctx.moveTo(points[p] + radius, points[p + 1]);
ctx.arc(points[p], points[p + 1], radius, 0, Math.PI * 2);
}
ctx.globalAlpha = Math.min(1, (0.3 + 0.7 * light) * fade);
ctx.fillStyle = `rgb(${colour})`;
ctx.fill();
});
ctx.globalAlpha = 1;
}
}
function moving() {
return !reduced.matches && onScreen && !document.hidden;
}
function settling() {
return layers.some((layer) => Math.abs(layer.glow - layer.target) > 0.01);
}
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) {
frame = requestAnimationFrame(tick);
return;
}
last = now;
if (moving()) angle += (elapsed / 1000) * ((Math.PI * 2) / 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;
draw();
if (moving() || settling()) frame = requestAnimationFrame(tick);
else last = 0;
}
function wake() {
if (!frame) frame = requestAnimationFrame(tick);
}
// The tool being pointed at, in this stack or the other one.
document.addEventListener('stack:active', ({ detail }) => {
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);
}
wake();
});
const announce = (link) => {
document.dispatchEvent(new CustomEvent('stack:active', { detail: { tool: link?.dataset.layer ?? null, link } }));
};
// On a touch screen a tap picks a layer instead of opening its site: the layer lights up and
// the tool card below switches to it, and the card links to the site. Tapping anywhere else
// puts the layer out. A mouse click, Enter or a screen reader's activation still opens it.
let tapped = null;
for (const layer of layers) {
layer.link.addEventListener('pointerenter', (event) => { if (event.pointerType !== 'touch') announce(layer.link); });
layer.link.addEventListener('pointerleave', (event) => { if (event.pointerType !== 'touch') announce(null); });
layer.link.addEventListener('pointerdown', (event) => {
tapped = event.pointerType === 'touch' ? layer.link : null;
});
layer.link.addEventListener('click', (event) => {
const tap = tapped;
tapped = null;
if (tap !== layer.link) return;
event.preventDefault();
// Safari doesn't focus a tapped link; the tool cards pick a focused layer straight away.
layer.link.focus({ preventScroll: true });
announce(layer.link);
});
layer.link.addEventListener('focus', () => announce(layer.link));
layer.link.addEventListener('blur', () => announce(null));
}
document.addEventListener('pointerdown', (event) => {
if (event.pointerType !== 'touch' || event.target.closest?.('.stack-labels a')) return;
if (layers.some((layer) => layer.link.classList.contains('is-active'))) announce(null);
});
new ResizeObserver(() => {
layout();
draw();
}).observe(root);
new IntersectionObserver(([entry]) => {
onScreen = entry.isIntersecting;
wake();
}).observe(root);
new MutationObserver(() => {
readColours();
draw();
}).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
document.addEventListener('visibilitychange', wake);
reduced.addEventListener('change', wake);
readColours();
layout();
draw();
wake();
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('[data-stack]').forEach(init);
});
})();