-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
288 lines (262 loc) · 8.67 KB
/
script.js
File metadata and controls
288 lines (262 loc) · 8.67 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
/*
* JavaScript for Akash Banerjee’s portfolio site.
*
* This script uses an IntersectionObserver to detect when elements with
* the `fade-in` class enter the viewport and applies a `visible` class
* that triggers CSS transitions defined in styles.css. It also exposes
* a helper function for smooth scrolling to anchor targets.
*/
let panels = [];
let viewportHeight;
let currentIndex = 0;
function updateViewportHeight() {
// Use the innerHeight at load time to avoid changes when the address bar
// hides on mobile, which can cause jumpy scrolling.
viewportHeight = window.innerHeight;
}
document.addEventListener("DOMContentLoaded", () => {
const fluidCanvas = document.getElementById("fluid-canvas");
if (fluidCanvas && window["webgl-fluid"]) {
window["webgl-fluid"](fluidCanvas, {
IMMEDIATE: false,
TRANSPARENT: true,
COLORFUL: false,
BACK_COLOR: { r: 230, g: 244, b: 255 },
SPLAT_RADIUS: 0.2,
});
const forward = (e) => {
let clientX, clientY;
if (e.touches && e.touches[0]) {
clientX = e.touches[0].clientX;
clientY = e.touches[0].clientY;
} else {
clientX = e.clientX;
clientY = e.clientY;
}
const map = {
touchstart: "mousedown",
touchmove: "mousemove",
touchend: "mouseup",
};
const type = map[e.type] || e.type;
const evt = new MouseEvent(type, {
clientX,
clientY,
bubbles: true,
});
fluidCanvas.dispatchEvent(evt);
};
[
"mousemove",
"mousedown",
"mouseup",
"touchstart",
"touchmove",
"touchend",
].forEach((evt) => window.addEventListener(evt, forward, { passive: true }));
}
panels = Array.from(document.querySelectorAll(".sections > .panel"));
updateViewportHeight();
let downArrow;
const createDownArrow = () => {
const arrowSvg =
'<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9" /></svg>';
downArrow = document.createElement("a");
downArrow.className = "down-arrow";
downArrow.setAttribute("aria-label", "Next section");
downArrow.innerHTML = arrowSvg;
downArrow.addEventListener("click", (e) => {
e.preventDefault();
const nextIndex = Math.min(currentIndex + 1, panels.length - 1);
const nextId = panels[nextIndex].id;
window.scrollToSection(nextId);
});
document.body.appendChild(downArrow);
};
const updateDownArrow = () => {
if (currentIndex >= panels.length - 1) {
downArrow.style.display = "none";
} else {
const nextId = panels[currentIndex + 1].id;
downArrow.href = `#${nextId}`;
downArrow.style.display = "flex";
}
};
let interactionTimeout;
const hideArrow = () => {
if (downArrow) {
downArrow.classList.add("hidden");
}
};
const showArrow = () => {
if (downArrow && downArrow.style.display !== "none") {
downArrow.classList.remove("hidden");
}
};
const scheduleArrowShow = () => {
clearTimeout(interactionTimeout);
interactionTimeout = setTimeout(showArrow, 3000);
};
const handleInteraction = (e) => {
if (downArrow && (e.target === downArrow || downArrow.contains(e.target))) {
return;
}
hideArrow();
scheduleArrowShow();
};
const setBodyHeight = () => {
document.body.style.height = `${panels.length * viewportHeight}px`;
};
setBodyHeight();
createDownArrow();
updateDownArrow();
["scroll", "keydown", "touchstart"].forEach((evt) =>
window.addEventListener(evt, handleInteraction, { passive: true })
);
const observer = new IntersectionObserver(
(entries, obs) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
obs.unobserve(entry.target);
}
});
},
{ threshold: 0.1 },
);
document.querySelectorAll(".fade-in").forEach((el) => observer.observe(el));
const CROSSFADE_DISTANCE = 0.25; // amount of scroll to transition between sections
const isTouch = "ontouchstart" in window || navigator.maxTouchPoints > 0;
const FOCUS_PADDING = isTouch ? 0.08 : 0.2; // rubberband zone
let autoScrolling = false;
let scrollTimeout;
let formFocusActive = false;
let focusedPanel = null;
panels.forEach((panel) => {
panel.addEventListener(
"wheel",
(e) => {
const atTop = panel.scrollTop === 0;
const atBottom =
panel.scrollHeight - panel.scrollTop <= panel.clientHeight + 1;
if ((e.deltaY < 0 && atTop) || (e.deltaY > 0 && atBottom)) {
e.preventDefault();
window.scrollBy({ top: e.deltaY });
}
},
{ passive: false },
);
let startY = 0;
panel.addEventListener("touchstart", (e) => {
startY = e.touches[0].clientY;
});
panel.addEventListener(
"touchmove",
(e) => {
const deltaY = startY - e.touches[0].clientY;
const atTop = panel.scrollTop === 0;
const atBottom =
panel.scrollHeight - panel.scrollTop <= panel.clientHeight + 1;
if ((deltaY < 0 && atTop) || (deltaY > 0 && atBottom)) {
window.scrollBy({ top: deltaY });
e.preventDefault();
}
},
{ passive: false },
);
});
const updatePanels = () => {
const maxScroll = (panels.length - 1) * viewportHeight;
const scrollPos = Math.min(Math.max(window.scrollY, 0), maxScroll);
const pos = scrollPos / viewportHeight;
// When a form input is focused on mobile, disable transform on its panel
if (formFocusActive && focusedPanel) {
panels.forEach((panel) => {
if (panel === focusedPanel) {
panel.style.opacity = 1;
panel.style.transform = "none";
panel.style.pointerEvents = "auto";
panel.style.zIndex = 100;
} else {
panel.style.opacity = 0; // fully hide other panels while focusing inputs
panel.style.transform = "scale(0.98)";
panel.style.pointerEvents = "none";
panel.style.zIndex = 1;
}
});
return;
}
panels.forEach((panel, i) => {
const diff = i - pos;
const absDiff = Math.abs(diff);
const offset = Math.max(absDiff - FOCUS_PADDING, 0);
const clamped = Math.min(offset / CROSSFADE_DISTANCE, 1);
const opacity = 1 - clamped;
const scale = 1 - clamped * 0.05;
panel.style.opacity = opacity;
panel.style.transform = `scale(${scale})`;
panel.style.pointerEvents = opacity > 0.1 ? "auto" : "none";
panel.style.zIndex = Math.round(opacity * 100);
});
};
const onScroll = () => {
updatePanels();
// Do not snap panels while typing in a form field
if (formFocusActive) return;
clearTimeout(scrollTimeout);
if (!autoScrolling) {
scrollTimeout = setTimeout(() => {
const rawIndex = Math.round(window.scrollY / viewportHeight);
const maxIndex = panels.length - 1;
const targetIndex = Math.max(0, Math.min(rawIndex, maxIndex));
if (Math.abs(targetIndex - currentIndex) > 1) {
currentIndex += Math.sign(targetIndex - currentIndex);
} else {
currentIndex = targetIndex;
}
updateDownArrow();
autoScrolling = true;
window.scrollTo({
top: currentIndex * viewportHeight,
behavior: "smooth",
});
setTimeout(() => {
autoScrolling = false;
}, 400);
}, 80);
}
};
window.addEventListener("scroll", onScroll);
window.addEventListener("resize", () => {
updateViewportHeight();
setBodyHeight();
updatePanels();
});
updatePanels();
window.scrollToSection = (id) => {
const index = panels.findIndex((p) => p.id === id);
if (index !== -1) {
const clamped = Math.max(0, Math.min(index, panels.length - 1));
currentIndex = clamped;
updateDownArrow();
window.scrollTo({ top: clamped * viewportHeight, behavior: "smooth" });
}
};
// Reduce mobile input issues: track focus and disable transforms/snapping
const isFormControl = (el) =>
el && (el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.tagName === "SELECT");
const updateFocusState = () => {
const ae = document.activeElement;
if (isFormControl(ae)) {
formFocusActive = true;
focusedPanel = ae.closest(".panel") || null;
} else {
formFocusActive = false;
focusedPanel = null;
}
updatePanels();
};
document.addEventListener("focusin", updateFocusState, true);
document.addEventListener("focusout", updateFocusState, true);
// No special key handling required for the new background
});