-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
381 lines (325 loc) · 14.2 KB
/
Copy pathscript.js
File metadata and controls
381 lines (325 loc) · 14.2 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
(() => {
"use strict";
const root = document.documentElement;
root.classList.add("js");
const state = {
motionEnabled: true,
pointerFine: window.matchMedia("(pointer: fine)").matches,
scrollFrame: 0,
pointerFrame: 0,
};
function safeFeature(name, initialize) {
try {
initialize();
} catch (error) {
console.warn(`[portfolio] ${name} enhancement unavailable`, error);
}
}
function readPreference(key) {
try {
return window.localStorage.getItem(key);
} catch (_error) {
return null;
}
}
function writePreference(key, value) {
try {
window.localStorage.setItem(key, value);
} catch (_error) {
// The interface remains usable when storage is restricted.
}
}
function clamp(min, max, value) {
return Math.min(max, Math.max(min, value));
}
safeFeature("year", () => {
const year = document.getElementById("year");
if (year) year.textContent = String(new Date().getFullYear());
});
safeFeature("theme", () => {
const button = document.getElementById("themeToggle");
const label = button?.querySelector(".theme-label");
const themeColor = document.getElementById("themeColor");
const systemTheme = window.matchMedia("(prefers-color-scheme: light)");
let savedTheme = readPreference("portfolio-theme");
function applyTheme(theme, persist = false) {
const nextTheme = theme === "light" ? "light" : "dark";
root.setAttribute("data-theme", nextTheme);
if (themeColor) themeColor.setAttribute("content", nextTheme === "light" ? "#f1efe7" : "#0b0d0c");
if (label) label.textContent = nextTheme === "light" ? "Dark" : "Light";
if (button) button.setAttribute("aria-label", `Switch to ${nextTheme === "light" ? "dark" : "light"} theme`);
if (persist) {
savedTheme = nextTheme;
writePreference("portfolio-theme", nextTheme);
}
}
const initialTheme = root.getAttribute("data-theme") === "light" ? "light" : "dark";
applyTheme(initialTheme);
button?.addEventListener("click", () => {
const nextTheme = root.getAttribute("data-theme") === "light" ? "dark" : "light";
applyTheme(nextTheme, true);
});
systemTheme.addEventListener?.("change", (event) => {
if (savedTheme !== "light" && savedTheme !== "dark") {
applyTheme(event.matches ? "light" : "dark");
}
});
});
safeFeature("motion preference", () => {
const button = document.getElementById("motionToggle");
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
let savedMotion = readPreference("portfolio-motion");
function applyMotion(enabled, persist = false) {
state.motionEnabled = reducedMotion.matches ? false : Boolean(enabled);
root.classList.toggle("motion-off", !state.motionEnabled);
if (button) {
button.setAttribute("aria-pressed", String(state.motionEnabled));
button.disabled = reducedMotion.matches;
button.setAttribute(
"aria-label",
reducedMotion.matches
? "Motion reduced by system setting"
: `Turn motion ${state.motionEnabled ? "off" : "on"}`
);
}
if (persist && !reducedMotion.matches) {
savedMotion = state.motionEnabled ? "on" : "off";
writePreference("portfolio-motion", savedMotion);
}
window.dispatchEvent(new CustomEvent("portfolio:motionchange", { detail: state.motionEnabled }));
}
const initialMotion = reducedMotion.matches ? false : savedMotion === "on" ? true : savedMotion === "off" ? false : true;
applyMotion(initialMotion);
button?.addEventListener("click", () => applyMotion(!state.motionEnabled, true));
reducedMotion.addEventListener?.("change", (event) => {
if (event.matches) applyMotion(false);
else applyMotion(savedMotion === "on" ? true : savedMotion === "off" ? false : true);
});
});
safeFeature("navigation", () => {
const header = document.getElementById("siteHeader");
const nav = document.getElementById("siteNav");
const menuButton = document.getElementById("menuToggle");
const navLinks = Array.from(nav?.querySelectorAll("a") || []);
if (!header || !nav || !menuButton) return;
function closeMenu({ restoreFocus = false } = {}) {
nav.classList.remove("open");
menuButton.setAttribute("aria-expanded", "false");
document.body.classList.remove("menu-open");
if (restoreFocus) menuButton.focus();
}
function openMenu() {
nav.classList.add("open");
menuButton.setAttribute("aria-expanded", "true");
document.body.classList.add("menu-open");
window.requestAnimationFrame(() => navLinks[0]?.focus());
}
menuButton.addEventListener("click", () => {
if (nav.classList.contains("open")) closeMenu();
else openMenu();
});
nav.addEventListener("click", (event) => {
if (event.target instanceof HTMLAnchorElement) {
closeMenu({ restoreFocus: window.innerWidth <= 900 });
}
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && nav.classList.contains("open")) {
closeMenu({ restoreFocus: true });
}
});
document.addEventListener("pointerdown", (event) => {
if (nav.classList.contains("open") && event.target instanceof Node && !header.contains(event.target)) {
closeMenu();
}
});
document.addEventListener("focusin", (event) => {
if (nav.classList.contains("open") && event.target instanceof Node && !header.contains(event.target)) {
closeMenu();
}
});
window.addEventListener("resize", () => {
if (window.innerWidth > 900 && nav.classList.contains("open")) closeMenu();
}, { passive: true });
});
safeFeature("reveal", () => {
const elements = Array.from(document.querySelectorAll(".reveal"));
if (!elements.length || !("IntersectionObserver" in window)) {
elements.forEach((element) => element.classList.add("is-visible"));
return;
}
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
}
});
}, { rootMargin: "0px 0px -8%", threshold: 0.08 });
elements.forEach((element) => {
const rect = element.getBoundingClientRect();
if (rect.top < window.innerHeight * 0.96) element.classList.add("is-visible");
else observer.observe(element);
});
if (state.motionEnabled) root.classList.add("motion-ok");
window.addEventListener("portfolio:motionchange", (event) => {
root.classList.toggle("motion-ok", Boolean(event.detail));
});
});
safeFeature("scroll motion", () => {
const header = document.getElementById("siteHeader");
const hero = document.querySelector(".hero");
const heroLines = Array.from(document.querySelectorAll(".hero-line[data-depth]"));
const media = Array.from(document.querySelectorAll(".parallax-media"));
let elevated = false;
function resetTransforms() {
heroLines.forEach((line) => { line.style.transform = ""; });
media.forEach((image) => image.style.setProperty("--parallax-y", "0px"));
}
function updateScrollEffects() {
state.scrollFrame = 0;
const scrollY = window.scrollY || 0;
const scrollable = Math.max(1, document.documentElement.scrollHeight - window.innerHeight);
root.style.setProperty("--scroll-progress", String(clamp(0, 1, scrollY / scrollable)));
const nextElevated = scrollY > 18;
if (header && nextElevated !== elevated) {
elevated = nextElevated;
header.setAttribute("data-elevated", String(elevated));
}
if (!state.motionEnabled || !state.pointerFine || window.innerWidth < 900) {
resetTransforms();
return;
}
if (hero) {
const heroBottom = hero.offsetTop + hero.offsetHeight;
if (scrollY < heroBottom) {
heroLines.forEach((line) => {
const depth = Number(line.getAttribute("data-depth")) || 0;
const offset = -clamp(0, 66, scrollY * depth * 0.52);
line.style.transform = `translate3d(0, ${offset.toFixed(2)}px, 0)`;
});
}
}
media.forEach((image) => {
const frame = image.parentElement;
if (!frame) return;
const rect = frame.getBoundingClientRect();
if (rect.bottom < 0 || rect.top > window.innerHeight) return;
const imageCenter = rect.top + rect.height / 2;
const offset = clamp(-24, 24, (window.innerHeight / 2 - imageCenter) * 0.035);
image.style.setProperty("--parallax-y", `${offset.toFixed(2)}px`);
});
}
function requestScrollUpdate() {
if (!state.scrollFrame) state.scrollFrame = window.requestAnimationFrame(updateScrollEffects);
}
window.addEventListener("scroll", requestScrollUpdate, { passive: true });
window.addEventListener("resize", requestScrollUpdate, { passive: true });
window.addEventListener("portfolio:motionchange", requestScrollUpdate);
updateScrollEffects();
});
safeFeature("pointer depth", () => {
if (!state.pointerFine) return;
const loop = document.getElementById("loopVisual");
let pointerX = window.innerWidth * 0.68;
let pointerY = window.innerHeight * 0.18;
function updatePointer() {
state.pointerFrame = 0;
if (!state.motionEnabled) return;
root.style.setProperty("--pointer-x", `${((pointerX / window.innerWidth) * 100).toFixed(2)}%`);
root.style.setProperty("--pointer-y", `${((pointerY / window.innerHeight) * 100).toFixed(2)}%`);
}
window.addEventListener("pointermove", (event) => {
pointerX = event.clientX;
pointerY = event.clientY;
if (!state.pointerFrame) state.pointerFrame = window.requestAnimationFrame(updatePointer);
}, { passive: true });
loop?.addEventListener("pointermove", (event) => {
if (!state.motionEnabled) return;
const rect = loop.getBoundingClientRect();
const x = (event.clientX - rect.left) / rect.width - 0.5;
const y = (event.clientY - rect.top) / rect.height - 0.5;
loop.style.setProperty("--loop-rotate-x", `${(-y * 6).toFixed(2)}deg`);
loop.style.setProperty("--loop-rotate-y", `${(x * 6).toFixed(2)}deg`);
}, { passive: true });
loop?.addEventListener("pointerleave", () => {
loop.style.setProperty("--loop-rotate-x", "0deg");
loop.style.setProperty("--loop-rotate-y", "0deg");
});
window.addEventListener("portfolio:motionchange", (event) => {
if (!event.detail) {
loop?.style.setProperty("--loop-rotate-x", "0deg");
loop?.style.setProperty("--loop-rotate-y", "0deg");
}
});
});
safeFeature("active navigation", () => {
if (!("IntersectionObserver" in window)) return;
const navLinks = Array.from(document.querySelectorAll(".site-nav a[href^='#']"));
const sections = Array.from(document.querySelectorAll("[data-section]"));
function setActive(id) {
navLinks.forEach((link) => {
const isActive = link.getAttribute("href") === `#${id}`;
if (isActive) link.setAttribute("aria-current", "location");
else link.removeAttribute("aria-current");
});
}
const observer = new IntersectionObserver((entries) => {
const visible = entries.filter((entry) => entry.isIntersecting).sort((a, b) => b.intersectionRatio - a.intersectionRatio);
if (visible[0]?.target.id) setActive(visible[0].target.id);
}, { rootMargin: "-28% 0px -58%", threshold: [0, 0.1, 0.3] });
sections.forEach((section) => observer.observe(section));
});
safeFeature("case study", () => {
const caseStudy = document.querySelector(".case-study");
const steps = Array.from(document.querySelectorAll(".story-step"));
const captionIndex = caseStudy?.querySelector(".caption-index");
const captionCopy = caseStudy?.querySelector(".caption-copy");
if (!caseStudy || !steps.length || !("IntersectionObserver" in window)) return;
function activate(step) {
const stepNumber = step.getAttribute("data-step") || "1";
caseStudy.setAttribute("data-step", stepNumber);
steps.forEach((item) => item.classList.toggle("is-active", item === step));
if (captionIndex) captionIndex.textContent = stepNumber.padStart(2, "0");
if (captionCopy) captionCopy.textContent = step.getAttribute("data-caption") || "";
}
const observer = new IntersectionObserver((entries) => {
const active = entries.filter((entry) => entry.isIntersecting).sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0];
if (active) activate(active.target);
}, { rootMargin: "-24% 0px -38%", threshold: [0.1, 0.3, 0.55] });
steps.forEach((step) => observer.observe(step));
});
safeFeature("visible project motion", () => {
if (!("IntersectionObserver" in window)) return;
const cards = Array.from(document.querySelectorAll(".project-card"));
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => entry.target.classList.toggle("is-onscreen", entry.isIntersecting && state.motionEnabled));
}, { rootMargin: "12% 0px", threshold: 0.01 });
cards.forEach((card) => observer.observe(card));
window.addEventListener("portfolio:motionchange", (event) => {
cards.forEach((card) => {
if (!event.detail) {
card.classList.remove("is-onscreen");
return;
}
const rect = card.getBoundingClientRect();
card.classList.toggle("is-onscreen", rect.bottom > 0 && rect.top < window.innerHeight);
});
});
});
safeFeature("approach focus", () => {
if (!("IntersectionObserver" in window)) return;
const items = Array.from(document.querySelectorAll(".approach-item"));
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => entry.target.classList.toggle("is-current", entry.isIntersecting));
}, { rootMargin: "-35% 0px -45%", threshold: 0.1 });
items.forEach((item) => observer.observe(item));
});
safeFeature("page visibility", () => {
function syncVisibility() {
document.body.classList.toggle("page-hidden", document.hidden);
}
document.addEventListener("visibilitychange", syncVisibility);
syncVisibility();
});
})();