-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
612 lines (543 loc) · 21.6 KB
/
script.js
File metadata and controls
612 lines (543 loc) · 21.6 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
=========================================
HANS PARSON PORTFOLIO & BLOG LOGIC
=========================================
*/
document.addEventListener("DOMContentLoaded", () => {
// Theme Toggle Logic
const themeToggle = document.getElementById("theme-toggle");
const storedTheme = localStorage.getItem("theme") || "dark";
document.documentElement.setAttribute("data-theme", storedTheme);
updateThemeIcon(storedTheme);
themeToggle.addEventListener("click", () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
const newTheme = currentTheme === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", newTheme);
localStorage.setItem("theme", newTheme);
updateThemeIcon(newTheme);
});
function updateThemeIcon(theme) {
themeToggle.innerHTML = theme === "dark" ? "☀️" : "🌙";
}
// Mobile Navigation Menu Toggle
const navToggleBtn = document.getElementById("nav-toggle-btn");
const navMenu = document.getElementById("nav-menu");
navToggleBtn.addEventListener("click", () => {
navMenu.classList.toggle("open");
navToggleBtn.innerHTML = navMenu.classList.contains("open") ? "✕" : "☰";
});
// Close Mobile Menu on clicking links
document.querySelectorAll(".nav-link").forEach(link => {
link.addEventListener("click", () => {
navMenu.classList.remove("open");
navToggleBtn.innerHTML = "☰";
});
});
// Active Link Tracking on Scroll
const sections = document.querySelectorAll("section");
const navLinks = document.querySelectorAll(".nav-link");
window.addEventListener("scroll", () => {
let current = "";
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (pageYOffset >= (sectionTop - 150)) {
current = section.getAttribute("id");
}
});
navLinks.forEach(link => {
link.classList.remove("active");
if (link.getAttribute("href") === `#${current}`) {
link.classList.add("active");
}
});
});
// Skill Filtering & Display (guard: only if old grid exists)
const skillsGrid = document.getElementById("skills-grid");
if (skillsGrid) {
const skills = {
backend: [
{ name: "Go (Golang)", icon: "🐹" }, { name: "Python", icon: "🐍" },
{ name: "PHP", icon: "🐘" }, { name: "Java", icon: "☕" },
{ name: "PostgreSQL", icon: "🐘" }, { name: "MySQL", icon: "🐬" },
{ name: "MongoDB", icon: "🍃" }, { name: "Redis", icon: "📦" }
],
frontend: [
{ name: "React", icon: "⚛️" }, { name: "Flutter", icon: "💙" },
{ name: "HTML5 & CSS3", icon: "🌐" }, { name: "JavaScript (ES6)", icon: "⚡" },
{ name: "Tailwind CSS", icon: "🎨" }
],
devops: [
{ name: "Docker", icon: "🐳" }, { name: "Nginx", icon: "⚙️" },
{ name: "Git & GitLab", icon: "🦊" }, { name: "Linux (Ubuntu)", icon: "🐧" },
{ name: "VPS Server", icon: "☁️" }
],
iot: [
{ name: "Embedded Systems", icon: "📟" }, { name: "Arduino & NodeMCU", icon: "🔌" },
{ name: "LoRa Networks", icon: "📡" }, { name: "PLC Programming", icon: "🤖" }
]
};
const categoryButtons = document.querySelectorAll(".skills-category-btn");
function renderSkills(category) {
skillsGrid.innerHTML = "";
const skillsList = category === "all"
? [...skills.backend, ...skills.frontend, ...skills.devops, ...skills.iot]
: skills[category];
skillsList.forEach(skill => {
const card = document.createElement("div");
card.className = "skill-card";
card.innerHTML = `<span class="skill-icon">${skill.icon}</span><h4 class="skill-name">${skill.name}</h4>`;
skillsGrid.appendChild(card);
});
}
categoryButtons.forEach(btn => {
btn.addEventListener("click", () => {
categoryButtons.forEach(b => b.classList.remove("active"));
btn.classList.add("active");
renderSkills(btn.dataset.category);
});
});
renderSkills("all");
}
// Load Projects Database dynamically
const projectsGrid = document.getElementById("projects-grid");
const projectFilterBtns = document.querySelectorAll(".project-filter-btn");
let allProjects = [];
function getCategoryIcon(cat) {
const icons = { iot: "📡", ai: "🤖", backend: "⚙️", fullstack: "🌐", hardware: "🔩" };
return icons[cat] || "💻";
}
function renderProjects(projects) {
projectsGrid.innerHTML = "";
projects.forEach(project => {
const icon = getCategoryIcon(project.category);
const card = document.createElement("div");
if (project.image) {
card.className = "project-card has-image";
card.innerHTML = `
<div class="project-mockup-header">
<span class="mockup-dot mockup-red"></span>
<span class="mockup-dot mockup-yellow"></span>
<span class="mockup-dot mockup-green"></span>
<span class="mockup-url">hansparson.dev/${project.id}</span>
</div>
<div class="project-img-wrap">
<img src="${project.image}" alt="${project.title}" class="project-img" loading="lazy" onerror="this.parentElement.style.display='none'">
</div>
<div class="project-top">
<span class="project-icon">${icon}</span>
<h3 class="project-title">${project.title}</h3>
<p class="project-desc">${project.description}</p>
<div class="project-tech-list">
${project.tech.map(t => `<span class="tech-tag">${t}</span>`).join("")}
</div>
</div>
<a href="${project.link}" target="_blank" class="project-link">View Project ↗</a>
`;
} else {
card.className = "project-card terminal-card";
card.innerHTML = `
<div class="project-terminal-header">
<div class="terminal-dots">
<span class="mockup-dot mockup-red"></span>
<span class="mockup-dot mockup-yellow"></span>
<span class="mockup-dot mockup-green"></span>
</div>
<span class="terminal-title">bash - hans@server:~/${project.id}</span>
</div>
<div class="project-top">
<span class="project-icon">${icon}</span>
<h3 class="project-title">${project.title}</h3>
<p class="project-desc">${project.description}</p>
<div class="project-tech-list">
${project.tech.map(t => `<span class="tech-tag">${t}</span>`).join("")}
</div>
</div>
<a href="${project.link}" target="_blank" class="project-link">View Project ↗</a>
`;
}
projectsGrid.appendChild(card);
});
}
fetch("projects.json")
.then(res => res.json())
.then(projects => {
allProjects = projects;
renderProjects(allProjects);
// Bind project filter buttons
projectFilterBtns.forEach(btn => {
btn.addEventListener("click", () => {
projectFilterBtns.forEach(b => b.classList.remove("active"));
btn.classList.add("active");
const cat = btn.dataset.cat;
const filtered = cat === "all" ? allProjects : allProjects.filter(p => p.category === cat);
renderProjects(filtered);
});
});
})
.catch(err => console.error("Error loading projects:", err));
// Load Blog Database dynamically
const blogGrid = document.getElementById("blog-grid");
const modalOverlay = document.getElementById("modal-overlay");
const modalCloseBtn = document.getElementById("modal-close-btn");
const modalMeta = document.getElementById("modal-meta");
const modalTitle = document.getElementById("modal-title");
const modalBody = document.getElementById("modal-body");
let blogPosts = [];
fetch("blog.json")
.then(res => res.json())
.then(posts => {
blogPosts = posts;
blogGrid.innerHTML = "";
posts.forEach(post => {
const row = document.createElement("div");
row.className = "blog-row";
row.innerHTML = `
<div class="blog-row-left">
<span class="blog-row-date">${post.date}</span>
<span class="blog-row-time">⏱️ ${post.readTime}</span>
</div>
<div class="blog-row-center">
<h3 class="blog-row-title">${post.title}</h3>
<p class="blog-row-summary">${post.summary}</p>
</div>
<div class="blog-row-right">
<span class="blog-row-action" data-id="${post.id}">Read Article ↗</span>
</div>
`;
blogGrid.appendChild(row);
});
// Bind article modal triggers
document.querySelectorAll(".blog-row").forEach(row => {
row.addEventListener("click", () => {
const actionBtn = row.querySelector(".blog-row-action");
const postId = actionBtn.dataset.id;
const post = blogPosts.find(p => p.id === postId);
if (post) {
modalMeta.innerText = `${post.date} • ${post.readTime}`;
modalTitle.innerText = post.title;
modalBody.innerHTML = post.content;
modalOverlay.classList.add("active");
document.body.style.overflow = "hidden"; // disable scroll
}
});
});
})
.catch(err => console.error("Error loading blog posts:", err));
// Load Certificates Database dynamically
const certsGrid = document.getElementById("certificates-grid");
const certModalOverlay = document.getElementById("cert-modal-overlay");
const certModalCloseBtn = document.getElementById("cert-modal-close-btn");
const certModalTitle = document.getElementById("cert-modal-title");
const certModalViewer = document.getElementById("cert-modal-viewer");
const prevBtn = document.getElementById("cert-prev-btn");
const nextBtn = document.getElementById("cert-next-btn");
const dotsContainer = document.getElementById("cert-carousel-dots");
let certificatesList = [];
let currentIndex = 0;
function getItemsPerPage() {
if (window.innerWidth <= 650) return 1;
if (window.innerWidth <= 992) return 2;
return 3;
}
function updateCarousel() {
const itemsPerPage = getItemsPerPage();
const totalItems = certificatesList.length;
const maxIndex = Math.max(0, totalItems - itemsPerPage);
if (currentIndex > maxIndex) currentIndex = maxIndex;
if (currentIndex < 0) currentIndex = 0;
const offset = -(currentIndex * (100 / itemsPerPage));
certsGrid.style.transform = `translateX(${offset}%)`;
// Update Dots
const dots = dotsContainer.querySelectorAll(".carousel-dot-indicator");
dots.forEach((dot, idx) => {
dot.classList.toggle("active", idx === Math.min(idx, currentIndex));
});
}
fetch("certificates.json")
.then(res => res.json())
.then(certs => {
certificatesList = certs;
certsGrid.innerHTML = "";
certs.forEach(cert => {
const card = document.createElement("div");
card.className = "cert-card";
const icon = cert.type === "pdf" ? "📄" : "🖼️";
card.innerHTML = `
<div class="cert-card-header">
<span class="cert-type-icon">${icon}</span>
<span class="cert-date">${cert.date}</span>
</div>
<h3 class="cert-title">${cert.title}</h3>
<span class="cert-issuer">🏢 ${cert.issuer}</span>
<p class="cert-desc">${cert.description}</p>
<button class="btn btn-outline cert-view-btn" data-id="${cert.id}" style="width:100%; padding: 8px 16px; font-size: 0.85rem; margin-top: 15px;">View Certificate</button>
`;
certsGrid.appendChild(card);
});
// Generate Dots
dotsContainer.innerHTML = "";
const totalItems = certs.length;
for (let i = 0; i < totalItems; i++) {
const dot = document.createElement("span");
dot.className = "carousel-dot-indicator";
if (i === 0) dot.classList.add("active");
dot.addEventListener("click", () => {
currentIndex = Math.min(i, totalItems - getItemsPerPage());
updateCarousel();
});
dotsContainer.appendChild(dot);
}
// Initialize Carousel
updateCarousel();
window.addEventListener("resize", updateCarousel);
// Controls event listeners
prevBtn.addEventListener("click", () => {
if (currentIndex > 0) {
currentIndex--;
} else {
currentIndex = certificatesList.length - getItemsPerPage(); // Wrap around
}
updateCarousel();
});
nextBtn.addEventListener("click", () => {
if (currentIndex < certificatesList.length - getItemsPerPage()) {
currentIndex++;
} else {
currentIndex = 0; // Wrap around
}
updateCarousel();
});
// Bind certificate viewer buttons
document.querySelectorAll(".cert-view-btn").forEach(btn => {
btn.addEventListener("click", () => {
const certId = btn.dataset.id;
const cert = certificatesList.find(c => c.id === certId);
if (cert) {
certModalTitle.innerText = cert.title;
if (cert.type === "pdf") {
certModalViewer.innerHTML = `
<object data="${cert.file}" type="application/pdf" width="100%" height="500px">
<p>Your browser does not support viewing PDFs inline. <a href="${cert.file}" target="_blank" class="project-link">Download PDF instead ↗</a></p>
</object>
`;
} else {
certModalViewer.innerHTML = `
<img src="${cert.file}" alt="${cert.title}" style="max-width: 100%; max-height: 500px; display: block; margin: 0 auto; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.3);">
`;
}
certModalOverlay.classList.add("active");
document.body.style.overflow = "hidden"; // disable scroll
}
});
});
})
.catch(err => console.error("Error loading certificates:", err));
// Close Certificate Modal triggers
certModalCloseBtn.addEventListener("click", closeCertModal);
certModalOverlay.addEventListener("click", (e) => {
if (e.target === certModalOverlay) closeCertModal();
});
function closeCertModal() {
certModalOverlay.classList.remove("active");
certModalViewer.innerHTML = "";
document.body.style.overflow = "auto"; // restore scroll
}
// Close Modal triggers
modalCloseBtn.addEventListener("click", closeModal);
modalOverlay.addEventListener("click", (e) => {
if (e.target === modalOverlay) closeModal();
});
function closeModal() {
modalOverlay.classList.remove("active");
document.body.style.overflow = "auto"; // restore scroll
}
// Contact Form — mailto: handler
const contactForm = document.getElementById("contact-form");
const toastMsg = document.getElementById("toast-msg");
contactForm.addEventListener("submit", (e) => {
e.preventDefault();
const name = contactForm.querySelector("#name").value.trim();
const email = contactForm.querySelector("#email").value.trim();
const message = contactForm.querySelector("#message").value.trim();
const subject = encodeURIComponent(`[Portfolio] Message from ${name}`);
const body = encodeURIComponent(
`Hi Hans,\n\nYou have a new message from your portfolio contact form:\n\n` +
`Name : ${name}\nEmail : ${email}\n\nMessage:\n${message}\n\n---\nSent via hansparson.github.io`
);
const mailtoLink = `mailto:hansparson013@gmail.com?subject=${subject}&body=${body}`;
// Open email client
window.location.href = mailtoLink;
// Reset form & show toast
contactForm.reset();
toastMsg.classList.add("active");
setTimeout(() => toastMsg.classList.remove("active"), 4000);
});
// =========================================
// 1. GRADIENT MESH ANIMATED CANVAS BACKGROUND
// =========================================
const canvas = document.getElementById("mesh-canvas");
const ctx = canvas.getContext("2d");
let blobs = [];
function resizeCanvas() {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
}
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
const isDark = () => document.documentElement.getAttribute("data-theme") !== "light";
function createBlobs() {
blobs = [];
const colors = [
[172, 66, 50], // cyan accent
[217, 91, 60], // blue accent
[280, 70, 60], // purple
];
for (let i = 0; i < 5; i++) {
const c = colors[i % colors.length];
blobs.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
r: 180 + Math.random() * 140,
hue: c[0], sat: c[1], lit: c[2],
vx: (Math.random() - 0.5) * 0.4,
vy: (Math.random() - 0.5) * 0.4,
});
}
}
createBlobs();
function drawMesh() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
blobs.forEach(b => {
const alpha = isDark() ? 0.12 : 0.08;
const grad = ctx.createRadialGradient(b.x, b.y, 0, b.x, b.y, b.r);
grad.addColorStop(0, `hsla(${b.hue}, ${b.sat}%, ${b.lit}%, ${alpha})`);
grad.addColorStop(1, `hsla(${b.hue}, ${b.sat}%, ${b.lit}%, 0)`);
ctx.fillStyle = grad;
ctx.beginPath();
ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2);
ctx.fill();
b.x += b.vx;
b.y += b.vy;
if (b.x < -b.r || b.x > canvas.width + b.r) b.vx *= -1;
if (b.y < -b.r || b.y > canvas.height + b.r) b.vy *= -1;
});
requestAnimationFrame(drawMesh);
}
drawMesh();
// =========================================
// 2. TYPEWRITER CYCLING ANIMATION
// =========================================
const typingEl = document.getElementById("typing-text");
if (typingEl) {
const words = ["Backend APIs", "Go Services", "IoT Systems", "AI Solutions", "Fintech Apps"];
let wordIdx = 0, charIdx = 0, isDeleting = false;
function typeWriter() {
const currentWord = words[wordIdx];
if (!isDeleting) {
typingEl.textContent = currentWord.substring(0, charIdx + 1);
charIdx++;
if (charIdx === currentWord.length) {
setTimeout(() => { isDeleting = true; typeWriter(); }, 1800);
return;
}
} else {
typingEl.textContent = currentWord.substring(0, charIdx - 1);
charIdx--;
if (charIdx === 0) {
isDeleting = false;
wordIdx = (wordIdx + 1) % words.length;
}
}
setTimeout(typeWriter, isDeleting ? 60 : 100);
}
setTimeout(typeWriter, 800);
}
// =========================================
// 3. SCROLL REVEAL ANIMATION
// =========================================
const revealEls = document.querySelectorAll(".reveal");
const revealObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
}
});
}, { threshold: 0.1 });
revealEls.forEach(el => revealObserver.observe(el));
// =========================================
// 4. ANIMATED STAT COUNTERS
// =========================================
const statNumbers = document.querySelectorAll(".stat-number");
let statsAnimated = false;
function animateCounter(el, target, duration = 1500) {
let start = 0;
const step = target / (duration / 16);
const timer = setInterval(() => {
start += step;
if (start >= target) {
el.textContent = target;
clearInterval(timer);
} else {
el.textContent = Math.floor(start);
}
}, 16);
}
const statsBar = document.getElementById("stats-bar");
if (statsBar) {
const statsObserver = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && !statsAnimated) {
statsAnimated = true;
statNumbers.forEach(el => {
animateCounter(el, parseInt(el.dataset.target));
});
}
}, { threshold: 0.3 });
statsObserver.observe(statsBar);
}
});
// =========================================
// 5. CUSTOM CURSOR + MOUSE SPOTLIGHT
// (outside DOMContentLoaded — runs globally)
// =========================================
const cursorDot = document.getElementById("cursor-dot");
const cursorRing = document.getElementById("cursor-ring");
const spotlight = document.getElementById("mouse-spotlight");
let mouseX = 0, mouseY = 0;
let ringX = 0, ringY = 0;
document.addEventListener("mousemove", (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
// Dot follows instantly
cursorDot.style.left = mouseX + "px";
cursorDot.style.top = mouseY + "px";
// Spotlight follows with slight smooth delay
spotlight.style.left = mouseX + "px";
spotlight.style.top = mouseY + "px";
});
// Ring follows with lerp lag for premium feel
function animateRing() {
ringX += (mouseX - ringX) * 0.12;
ringY += (mouseY - ringY) * 0.12;
cursorRing.style.left = ringX + "px";
cursorRing.style.top = ringY + "px";
requestAnimationFrame(animateRing);
}
animateRing();
// Hover effect on interactive elements
const interactiveEls = document.querySelectorAll(
"a, button, .bento-card, .project-card, .blog-card, .skill-card, .btn"
);
interactiveEls.forEach(el => {
el.addEventListener("mouseenter", () => {
cursorRing.classList.add("hover");
cursorDot.style.transform = "translate(-50%, -50%) scale(1.5)";
});
el.addEventListener("mouseleave", () => {
cursorRing.classList.remove("hover");
cursorDot.style.transform = "translate(-50%, -50%) scale(1)";
});
});
// Click effect
document.addEventListener("mousedown", () => cursorDot.classList.add("click"));
document.addEventListener("mouseup", () => cursorDot.classList.remove("click"));