-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
444 lines (363 loc) · 15.8 KB
/
script.js
File metadata and controls
444 lines (363 loc) · 15.8 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
document.addEventListener("DOMContentLoaded", () => {
const docElement = document.documentElement;
const storage = localStorage;
let isNavScrolling = false;
let navTargetSectionId = null;
let activeScrollCleanup = null;
// --- UTILITY: THROTTLE FUNCTION ---
const throttle = (func, limit) => {
let inThrottle;
return function (...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
};
// Store event listeners for cleanup
const eventListeners = [];
const addEventListenerWithCleanup = (target, event, handler, options = false) => {
target.addEventListener(event, handler, options);
eventListeners.push({ target, event, handler, options });
};
// Cleanup function for memory efficiency
const cleanup = () => {
eventListeners.forEach(({ target, event, handler, options }) => {
target.removeEventListener(event, handler, options);
});
};
// --- 1. THEME LOGIC ---
const getCurrentTheme = () => {
return docElement.getAttribute("data-theme") || storage.getItem("theme") || "light";
};
const setTheme = (themeName) => {
docElement.setAttribute("data-theme", themeName);
storage.setItem("theme", themeName);
};
// Set initial theme
const initialTheme = getCurrentTheme();
docElement.setAttribute("data-theme", initialTheme);
// Remove transition block
requestAnimationFrame(() => {
docElement.classList.remove('no-transition');
});
// Theme toggle click listener (covers theme-toggle-btn and mobile-theme-toggle)
const handleThemeToggle = (event) => {
const button = event.target.closest("#theme-toggle, #mobile-theme-toggle");
if (button) {
event.preventDefault();
const newTheme = getCurrentTheme() === "dark" ? "light" : "dark";
setTheme(newTheme);
}
};
addEventListenerWithCleanup(document, "click", handleThemeToggle);
// --- 2. MOBILE DRAWER INTERACTION ---
const menuToggle = document.querySelector(".menu-toggle");
const closeDrawer = document.querySelector(".close-drawer");
const mobileDrawer = document.querySelector(".mobile-nav-drawer");
const mobileNavItems = document.querySelectorAll(".mobile-nav-item");
if (menuToggle && mobileDrawer) {
const handleMenuToggle = () => {
mobileDrawer.classList.add("open");
};
addEventListenerWithCleanup(menuToggle, "click", handleMenuToggle);
}
if (closeDrawer && mobileDrawer) {
const handleCloseDrawer = () => {
mobileDrawer.classList.remove("open");
};
addEventListenerWithCleanup(closeDrawer, "click", handleCloseDrawer);
}
// Close drawer when link clicked
mobileNavItems.forEach(item => {
const handleNavClick = () => {
if (mobileDrawer) {
mobileDrawer.classList.remove("open");
}
};
addEventListenerWithCleanup(item, "click", handleNavClick);
});
// --- 3. SCROLL REVEAL ANIMATION ---
const revealSelectors = '.reveal, .reveal-left, .reveal-right, .reveal-stagger';
const observer = new IntersectionObserver((entries, obs) => {
if (isNavScrolling) return; // Ignore observer during programmatic nav scrolls
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('active');
if (window.innerWidth <= 768) {
obs.unobserve(entry.target);
}
} else {
if (window.innerWidth > 768) {
entry.target.classList.remove('active');
}
}
});
}, {
threshold: 0.1
});
document.querySelectorAll(revealSelectors).forEach(el => observer.observe(el));
// Immediately activate hero elements (already in viewport on load)
document.querySelectorAll('.hero-section ' + revealSelectors.split(',').join(', .hero-section ')).forEach(el => {
el.classList.add('active');
});
// --- 4. FOOTER YEAR ---
const yearSpan = document.getElementById("current-year");
if (yearSpan) {
yearSpan.textContent = new Date().getFullYear();
}
// --- 5. SOCIAL LINKS (single source of truth) ---
const SOCIALS = [
{
name: "LinkedIn",
url: "https://www.linkedin.com/in/drshtmstry",
icon: "fab fa-linkedin",
label: "linkedin.com/in/drshtmstry",
},
{
name: "GitHub",
url: "https://github.com/drshtmstry",
icon: "fab fa-github",
label: "github.com/drshtmstry",
},
];
const renderSocials = () => {
// Vertical sidebar only: icon-only links
document.querySelectorAll('[data-socials="vertical"]').forEach(container => {
if (container.children.length === 0) {
container.innerHTML = SOCIALS.map(s =>
`<a href="${s.url}" target="_blank" rel="noopener noreferrer" aria-label="${s.name}"><i class="${s.icon}"></i></a>`
).join("");
}
});
// Contact section: full cards
document.querySelectorAll('[data-socials="contact"]').forEach(container => {
if (container.children.length === 0) {
container.innerHTML = SOCIALS.map(s =>
`<a href="${s.url}" target="_blank" rel="noopener noreferrer" class="connect-card">
<div class="connect-icon"><i class="${s.icon}"></i></div>
<div class="connect-details">
<h3>${s.name}</h3>
<p>${s.label}</p>
</div>
</a>`
).join("");
}
});
};
renderSocials();
// --- 6. SCROLL ORBIT FOR HERO DOTS ---
const dotsContainer = document.querySelector(".image-open-container");
let orbitRafId = null;
if (dotsContainer) {
const dots = dotsContainer.querySelectorAll(".floating-dot-wrapper");
const baseAngles = [0, 120, 240];
const updateOrbit = () => {
const scrollTop = window.scrollY;
const rotation = scrollTop * 0.2;
const width = dotsContainer.clientWidth || 320;
const height = dotsContainer.clientHeight || 380;
const centerX = width / 2;
const centerY = height / 2;
const radius = width / 2 + 15; // 15px offset outside the container border
dots.forEach((dot, index) => {
if (index < 3) {
const angleDeg = baseAngles[index] + rotation;
const angleRad = (angleDeg * Math.PI) / 180;
const x = centerX + radius * Math.cos(angleRad) - 6; // Subtract 6px (half of 12px dot width) to center it
const y = centerY + radius * Math.sin(angleRad) - 6;
dot.style.transform = `translate3d(${x}px, ${y}px, 0)`;
}
});
};
const handleOrbitScroll = () => {
if (orbitRafId) cancelAnimationFrame(orbitRafId);
orbitRafId = requestAnimationFrame(updateOrbit);
};
addEventListenerWithCleanup(window, "scroll", handleOrbitScroll, { passive: true });
addEventListenerWithCleanup(window, "resize", updateOrbit, { passive: true });
updateOrbit(); // Initial position setup
}
// --- 7. ACTIVE NAV LINK TRACKING ---
const navLinks = document.querySelectorAll(".nav-item[data-section], .mobile-nav-item[data-section]");
const sections = document.querySelectorAll("section[id]");
const updateActiveNav = throttle(() => {
let currentSection = null;
const scrollPosition = window.scrollY + 80; // Offset for header height
sections.forEach(section => {
if (section.offsetTop <= scrollPosition && section.offsetTop + section.offsetHeight > scrollPosition) {
currentSection = section.getAttribute("id");
}
});
navLinks.forEach(link => {
if (link.getAttribute("data-section") === currentSection) {
link.classList.add("active");
} else {
link.classList.remove("active");
}
});
const indicatorDots = document.querySelectorAll(".indicator-dot[data-section]");
indicatorDots.forEach(dot => {
if (dot.getAttribute("data-section") === currentSection) {
dot.classList.add("active");
} else {
dot.classList.remove("active");
}
});
}, 100);
addEventListenerWithCleanup(window, "scroll", updateActiveNav, { passive: true });
updateActiveNav(); // Initial call
// --- 8. CARD TILT EFFECT (3D Perspective) + CONNECT CARD RADIAL GRADIENT ---
const cards = document.querySelectorAll(".skill-card, .connect-card");
cards.forEach(card => {
const isConnectCard = card.classList.contains("connect-card");
const handleMouseMove = (e) => {
const rect = card.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const centerX = rect.width / 2;
const centerY = rect.height / 2;
const rotateX = ((y - centerY) / centerY) * 5;
const rotateY = ((centerX - x) / centerX) * 5;
card.style.transform = `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
if (isConnectCard) {
card.style.setProperty("--mouse-x", `${(x / rect.width) * 100}%`);
card.style.setProperty("--mouse-y", `${(y / rect.height) * 100}%`);
}
};
const handleMouseLeave = () => {
card.style.transform = "";
if (isConnectCard) {
card.style.setProperty("--mouse-x", "50%");
card.style.setProperty("--mouse-y", "50%");
}
};
addEventListenerWithCleanup(card, "mousemove", handleMouseMove, false);
addEventListenerWithCleanup(card, "mouseleave", handleMouseLeave, false);
});
// --- 9. SMOOTH SCROLL FOR INTERNALS & ANIMATION SYNC ---
const handleAnchorClick = (event) => {
const link = event.target.closest('a[href^="#"]');
if (!link) return;
const targetId = link.getAttribute("href");
if (targetId === "#") return;
let targetElement;
try {
targetElement = document.querySelector(targetId);
} catch (e) {
return;
}
if (!targetElement) return;
event.preventDefault();
const startY = window.scrollY;
const targetRect = targetElement.getBoundingClientRect();
const targetTop = targetRect.top + startY;
const targetY = targetTop - 64; // Header offset
const totalDistance = Math.abs(targetY - startY);
// If we are already extremely close, just update URL hash and activate immediately
if (totalDistance < 10) {
if (history.pushState) {
history.pushState(null, null, targetId);
} else {
location.hash = targetId;
}
// Activate target section elements immediately
const selectors = '.reveal, .reveal-left, .reveal-right, .reveal-stagger';
if (targetElement.matches(selectors)) {
targetElement.classList.add('active');
}
targetElement.querySelectorAll(selectors).forEach(el => {
el.classList.add('active');
});
return;
}
// Cancel any active scroll finalization from previous link clicks
if (activeScrollCleanup) {
activeScrollCleanup();
}
// Set scrolling flag and add layout override class
isNavScrolling = true;
navTargetSectionId = targetId;
docElement.classList.add("nav-scrolling");
// Scroll smoothly to target element
targetElement.scrollIntoView({ behavior: 'smooth' });
// Update URL hash without jumping
if (history.pushState) {
history.pushState(null, null, targetId);
} else {
location.hash = targetId;
}
let scrollTimeout = null;
const cleanupScroll = () => {
window.removeEventListener("scroll", onScrollFallback);
window.removeEventListener("scrollend", onScrollEndEvent);
clearTimeout(scrollTimeout);
docElement.classList.remove("nav-scrolling");
activeScrollCleanup = null;
};
activeScrollCleanup = cleanupScroll;
const finalizeScroll = () => {
cleanupScroll();
isNavScrolling = false;
navTargetSectionId = null;
// Activate target section elements after landing
const selectors = '.reveal, .reveal-left, .reveal-right, .reveal-stagger';
if (targetElement.matches(selectors)) {
targetElement.classList.add('active');
}
targetElement.querySelectorAll(selectors).forEach(el => {
el.classList.add('active');
});
};
const onScrollFallback = () => {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(finalizeScroll, 100);
};
const onScrollEndEvent = () => {
finalizeScroll();
};
if ("onscrollend" in window) {
window.addEventListener("scrollend", onScrollEndEvent);
}
window.addEventListener("scroll", onScrollFallback);
// Safety timeout in case scroll doesn't happen or handles weirdly
scrollTimeout = setTimeout(finalizeScroll, 1500);
};
// Register anchor click listener (covers all internal link navigations, navbar, drawer, buttons, dots)
addEventListenerWithCleanup(document, "click", handleAnchorClick);
// --- 10. PARALLAX EFFECT FOR DECORATIVE ELEMENTS ---
const decorDots = document.querySelectorAll(".decor-dots");
let rafId = null;
const updateParallax = () => {
const scrollY = window.scrollY;
const viewportHeight = window.innerHeight;
decorDots.forEach(dot => {
const parentSection = dot.closest("section");
if (!parentSection) return;
const sectionTop = parentSection.offsetTop;
const sectionHeight = parentSection.offsetHeight;
// Adjust speed: dots-left moves a bit more than dots-right
const speed = dot.classList.contains("dots-left") ? 0.25 : 0.15;
// Calculate how far the section center is from the viewport center
const sectionCenter = sectionTop + sectionHeight / 2;
const viewportCenter = scrollY + viewportHeight / 2;
const relativeScroll = viewportCenter - sectionCenter;
// Hardware accelerated 3D transform relative to viewport position
dot.style.transform = `translate3d(0, ${relativeScroll * speed}px, 0)`;
});
};
const handleParallaxScroll = () => {
if (rafId) cancelAnimationFrame(rafId);
rafId = requestAnimationFrame(updateParallax);
};
addEventListenerWithCleanup(window, "scroll", handleParallaxScroll, { passive: true });
addEventListenerWithCleanup(window, "resize", updateParallax, { passive: true });
updateParallax(); // Initial position setup
// Cleanup on page unload
window.addEventListener("beforeunload", () => {
if (rafId) cancelAnimationFrame(rafId);
if (orbitRafId) cancelAnimationFrame(orbitRafId);
cleanup();
});
});