Repository navigation
Expand file tree
/
Copy pathsite.js
More file actions
363 lines (337 loc) · 11 KB
/
Copy pathsite.js
File metadata and controls
363 lines (337 loc) · 11 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"use strict";
// Progressive enhancement for the Device website. Everything here is optional:
// without JavaScript every section is still readable and every link works.
const root = document.documentElement;
root.classList.add("js");
const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
const $ = (s, el = document) => el.querySelector(s);
const $$ = (s, el = document) => [...el.querySelectorAll(s)];
/* ───────────── Mobile navigation ───────────── */
const menu = $(".menu-toggle");
const nav = $("#navigation");
const closeMenu = () => {
menu.setAttribute("aria-expanded", "false");
nav.dataset.open = "false";
};
menu.addEventListener("click", () => {
const open = menu.getAttribute("aria-expanded") !== "true";
menu.setAttribute("aria-expanded", String(open));
nav.dataset.open = String(open);
});
nav.addEventListener("click", (e) => {
if (e.target.closest("a")) closeMenu();
});
/* ───────────── Scroll: header playhead, product tilt ───────────── */
const ruler = $(".scroll-ruler");
const appWindow = $(".app-window");
let ticking = false;
function onScroll() {
ticking = false;
const max = root.scrollHeight - innerHeight;
ruler.style.setProperty(
"--progress",
max > 0 ? (scrollY / max).toFixed(4) : 0,
);
if (!reduceMotion) {
const r = appWindow.getBoundingClientRect();
// 1 while the window's top is near the bottom of the viewport, 0 once it's well in view.
const t = Math.min(
1,
Math.max(0, (r.top - innerHeight * 0.18) / (innerHeight * 0.62)),
);
appWindow.style.setProperty("--tilt", t.toFixed(3));
}
}
addEventListener(
"scroll",
() => {
if (!ticking) {
ticking = true;
requestAnimationFrame(onScroll);
}
},
{ passive: true },
);
addEventListener("resize", onScroll, { passive: true });
onScroll();
// Highlight the nav link of the section in view.
const navLinks = $$('#navigation a[href^="#"]');
const sectionObserver = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
navLinks.forEach((a) =>
a.getAttribute("href") === "#" + entry.target.id
? a.setAttribute("aria-current", "true")
: a.removeAttribute("aria-current"),
);
}
},
{ rootMargin: "-45% 0px -50% 0px" },
);
navLinks.forEach((a) => {
const target = $(a.getAttribute("href"));
if (target) sectionObserver.observe(target);
});
// The hero and the band between sections clear the highlight.
sectionObserver.observe($("#top"));
sectionObserver.observe($(".numbers"));
/* ───────────── Fade-in on scroll ───────────── */
const revealTargets = $$(
".section-head, .device-filter, .device-grid, .plugin-band, .stack, .deadline, .principles, .open-grid > *, .status-board, .platforms, .build-grid > *",
);
if (!reduceMotion && "IntersectionObserver" in window) {
const io = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
entry.target.classList.add("is-in");
io.unobserve(entry.target);
}
}
},
{ rootMargin: "0px 0px -10% 0px" },
);
revealTargets.forEach((el) => {
// Only hide what is still below the fold, so nothing flashes.
if (el.getBoundingClientRect().top > innerHeight) {
el.setAttribute("data-reveal", "");
io.observe(el);
}
});
}
/* ───────────── Product hotspots ───────────── */
const spots = $$(".hotspots li");
spots.forEach((li) =>
$("button", li).addEventListener("click", (e) => {
e.stopPropagation();
const open = !li.classList.contains("is-open");
spots.forEach((s) => s.classList.remove("is-open"));
li.classList.toggle("is-open", open);
}),
);
document.addEventListener("click", () =>
spots.forEach((s) => s.classList.remove("is-open")),
);
document.addEventListener("keydown", (e) => {
if (e.key !== "Escape") return;
spots.forEach((s) => s.classList.remove("is-open"));
if (menu.getAttribute("aria-expanded") === "true") {
closeMenu();
menu.focus();
}
});
/* ───────────── Count-up numbers ───────────── */
const counters = $$("[data-count]");
if (!reduceMotion) {
const countIo = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
countIo.unobserve(entry.target);
const el = entry.target;
const end = Number(el.dataset.count);
if (!end) continue;
const t0 = performance.now();
const step = (now) => {
const p = Math.min(1, (now - t0) / 1100);
el.textContent = Math.round(end * (1 - Math.pow(1 - p, 3)));
if (p < 1) requestAnimationFrame(step);
};
requestAnimationFrame(step);
}
});
counters.forEach((el) => {
if (el.getBoundingClientRect().top > innerHeight) countIo.observe(el);
});
}
/* ───────────── Tour chapters ───────────── */
const chapters = $$(".chapter");
const media = $$(".media-frame img");
const progress = $(".chapter-media");
function activate(chapter) {
const name = chapter.dataset.chapter;
chapters.forEach((c) => c.classList.toggle("is-active", c === chapter));
media.forEach((img) => {
const on = img.dataset.media === name;
if (on && img.loading === "lazy") img.loading = "eager";
img.classList.toggle("is-active", on);
});
progress.style.setProperty("--step", chapters.indexOf(chapter) + 1);
progress.style.setProperty(
"--active",
getComputedStyle(chapter).getPropertyValue("--c"),
);
}
const chapterIo = new IntersectionObserver(
(entries) => {
for (const entry of entries)
if (entry.isIntersecting) activate(entry.target);
},
{ rootMargin: "-48% 0px -48% 0px" },
);
chapters.forEach((c) => chapterIo.observe(c));
activate(chapters[0]);
// Warm the next images once the tour is close, so swaps never wait on the network.
new IntersectionObserver(
(entries, obs) => {
if (entries.some((e) => e.isIntersecting)) {
media.forEach((img) => (img.loading = "eager"));
obs.disconnect();
}
},
{ rootMargin: "400px 0px" },
).observe($(".chapters"));
/* ───────────── Device filter ───────────── */
const filterButtons = $$("[data-filter]");
const deviceItems = $$(".device-grid li");
filterButtons.forEach((button) =>
button.addEventListener("click", () => {
const kind = button.dataset.filter;
filterButtons.forEach((b) =>
b.setAttribute("aria-pressed", String(b === button)),
);
deviceItems.forEach(
(li) => (li.hidden = kind !== "all" && li.dataset.kind !== kind),
);
}),
);
/* ───────────── Copy build commands ───────────── */
const copyButton = $("#copy-command");
const copyStatus = $("#copy-status");
copyButton.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText($("#build-command").textContent.trim());
copyButton.textContent = "Copied ✓";
copyStatus.textContent = "Copied. Paste the commands into your terminal.";
setTimeout(() => (copyButton.textContent = "Copy"), 2400);
} catch {
copyStatus.textContent =
"Copying isn’t available here. Select the commands and copy them.";
}
});
/* ───────────── Hero scope ───────────── */
const canvas = $(".hero-scope");
const hero = $(".hero");
// Waves are drawn dithered: one texel per 3 CSS px, quantised with a Bayer matrix (dither.js).
const scope = window.Dither.surface(canvas, 3);
let W = 0;
let H = 0;
let pointerX = -1;
let pointerY = -1;
let heroVisible = true;
let rafId = 0;
let lastFrame = -1e9;
function resize() {
({ width: W, height: H } = scope.resize());
draw(reduceMotion ? 0 : performance.now());
}
addEventListener("resize", () => W && resize(), { passive: true });
hero.addEventListener(
"pointermove",
(e) => {
const r = canvas.getBoundingClientRect();
pointerX = e.clientX - r.left;
pointerY = e.clientY - r.top;
},
{ passive: true },
);
hero.addEventListener("pointerleave", () => (pointerX = -1));
// One cycle that morphs from a sine (the mark) towards a saw, by mixing harmonics.
const morph = (phase, m) => {
let v = Math.sin(phase);
for (let k = 2; k <= 6; k++)
v += (m * Math.sin(k * phase) * (k % 2 ? 1 : -1)) / k;
return v / (1 + m * 0.75);
};
// Track colours as dim, mid and bright dither levels (the last value is alpha).
const PINK = [
[236, 92, 159, 70],
[236, 92, 159, 150],
[244, 150, 192, 200],
];
const ORANGE = [
[222, 126, 54, 60],
[222, 126, 54, 130],
[236, 168, 112, 180],
];
function draw(now) {
const t = now / 1000;
const mid = H * 0.52;
// A short band (the phone layout) gets taller waves for its size.
const amp = H < 260 ? H * 0.26 : Math.min(H * 0.14, 120);
const spread = Math.min(1, H / 300);
// A soft bump that follows the pointer, as if you were bending the signal.
const bump = (x) =>
pointerX < 0
? 0
: Math.exp(-((x - pointerX) ** 2) / (2 * 110 ** 2)) *
((pointerY - mid) / H) *
0.9;
const m = 0.5 - 0.5 * Math.cos(t * 0.35);
const k = (Math.PI * 2 * 2.2) / Math.max(W, 1);
const drift = t * 1.1;
scope.draw([
// Two quieter "tracks" behind the main signal, in the app's track colours.
{
y: (x) => mid + 40 * spread + Math.sin(x * k * 1.5 - drift * 0.8 + 1) * amp * 0.35,
ramp: PINK,
width: 1.5,
glow: 5,
strength: 0.35,
},
{
y: (x) =>
mid - 30 * spread + morph(x * k * 0.75 - drift * 0.6 + 2, 1 - m) * amp * 0.5,
ramp: ORANGE,
width: 1.5,
glow: 5,
strength: 0.35,
},
{
y: (x) =>
mid +
morph(x * k - drift, m) * amp * (1 + bump(x)) -
bump(x) * amp * 0.4,
ramp: window.Dither.BLUE,
width: 3,
glow: 9,
strength: 0.6,
},
]);
}
// 30 frames a second is plenty for a dithered scope and halves the work on phones.
function frame(now) {
if (now - lastFrame >= 32) {
lastFrame = now;
draw(now);
}
rafId = requestAnimationFrame(frame);
}
function startLoop() {
if (W && !rafId && heroVisible && !document.hidden && !reduceMotion) {
rafId = requestAnimationFrame(frame);
}
}
function stopLoop() {
cancelAnimationFrame(rafId);
rafId = 0;
}
new IntersectionObserver(([entry]) => {
heroVisible = entry.isIntersecting;
heroVisible ? startLoop() : stopLoop();
}).observe(hero);
document.addEventListener("visibilitychange", () =>
document.hidden ? stopLoop() : startLoop(),
);
// Draw the scope only after the first paint, so it never delays the text.
const initScope = () => {
resize();
canvas.classList.add("is-on");
startLoop();
};
if (document.readyState === "complete")
requestAnimationFrame(() => setTimeout(initScope, 0));
else
addEventListener("load", () => requestAnimationFrame(initScope), {
once: true,
});