-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
493 lines (422 loc) · 16 KB
/
Copy pathscript.js
File metadata and controls
493 lines (422 loc) · 16 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
/**
* VisionTrack Malawi — Landing Page Scripts
* Handles: particle background, navigation, scroll reveals,
* stat counters, form validation, and accessibility helpers.
*/
(function () {
"use strict";
/* --------------------------------------------------------------------------
DOM references
-------------------------------------------------------------------------- */
const header = document.querySelector(".site-header");
const navToggle = document.querySelector(".nav-toggle");
const navMenu = document.querySelector(".nav-menu");
const navLinks = document.querySelectorAll(".nav-menu a");
const revealElements = document.querySelectorAll(".reveal");
const statCounters = document.querySelectorAll("[data-count]");
const joinForm = document.getElementById("join-form");
const contactForm = document.getElementById("contact-form");
const yearEl = document.getElementById("year");
const canvas = document.getElementById("particle-canvas");
const themeToggle = document.getElementById("theme-toggle");
const CONTACT_EMAIL = "virtously9@gmail.com";
/* --------------------------------------------------------------------------
Footer year
-------------------------------------------------------------------------- */
if (yearEl) {
yearEl.textContent = String(new Date().getFullYear());
}
/* --------------------------------------------------------------------------
Footer: show current site URL (updates automatically on Netlify)
-------------------------------------------------------------------------- */
const footerSiteLink = document.getElementById("footer-site-link");
if (footerSiteLink && window.location.protocol.startsWith("http")) {
const origin = window.location.origin;
footerSiteLink.href = origin;
footerSiteLink.textContent = origin.replace(/^https?:\/\//, "");
}
/* --------------------------------------------------------------------------
Theme toggle (dark/light), persisted to localStorage
-------------------------------------------------------------------------- */
function getPreferredTheme() {
const saved = localStorage.getItem("vtm-theme");
if (saved === "light" || saved === "dark") return saved;
return window.matchMedia &&
window.matchMedia("(prefers-color-scheme: light)").matches
? "light"
: "dark";
}
function applyTheme(theme) {
document.documentElement.dataset.theme = theme;
localStorage.setItem("vtm-theme", theme);
if (themeToggle) {
const isLight = theme === "light";
themeToggle.setAttribute(
"aria-label",
isLight ? "Switch to dark theme" : "Switch to light theme"
);
const icon = themeToggle.querySelector(".theme-toggle-icon");
if (icon) icon.textContent = isLight ? "☀" : "☾";
}
}
applyTheme(getPreferredTheme());
if (themeToggle) {
themeToggle.addEventListener("click", () => {
const current = document.documentElement.dataset.theme === "light" ? "light" : "dark";
applyTheme(current === "light" ? "dark" : "light");
});
}
/* --------------------------------------------------------------------------
Sticky header on scroll
-------------------------------------------------------------------------- */
function handleHeaderScroll() {
if (!header) return;
header.classList.toggle("scrolled", window.scrollY > 40);
}
window.addEventListener("scroll", handleHeaderScroll, { passive: true });
handleHeaderScroll();
/* --------------------------------------------------------------------------
Mobile navigation toggle
-------------------------------------------------------------------------- */
function closeNav() {
if (!navToggle || !navMenu) return;
navToggle.setAttribute("aria-expanded", "false");
navToggle.setAttribute("aria-label", "Open menu");
navMenu.classList.remove("open");
}
function openNav() {
if (!navToggle || !navMenu) return;
navToggle.setAttribute("aria-expanded", "true");
navToggle.setAttribute("aria-label", "Close menu");
navMenu.classList.add("open");
}
if (navToggle && navMenu) {
navToggle.addEventListener("click", () => {
const isOpen = navMenu.classList.contains("open");
isOpen ? closeNav() : openNav();
});
// Close menu when a link is clicked
navLinks.forEach((link) => {
link.addEventListener("click", closeNav);
});
// Close on escape key
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") closeNav();
});
}
/* --------------------------------------------------------------------------
Scroll reveal (Intersection Observer)
-------------------------------------------------------------------------- */
function initReveal() {
if (!revealElements.length) return;
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (prefersReducedMotion) {
revealElements.forEach((el) => el.classList.add("visible"));
return;
}
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
const el = entry.target;
const delay = parseInt(el.dataset.delay || "0", 10);
setTimeout(() => el.classList.add("visible"), delay);
observer.unobserve(el);
});
},
{ threshold: 0.12, rootMargin: "0px 0px -40px 0px" }
);
revealElements.forEach((el) => observer.observe(el));
}
/* --------------------------------------------------------------------------
Animated stat counters
-------------------------------------------------------------------------- */
function animateCounter(element) {
const target = parseInt(element.dataset.count, 10);
const suffix = element.dataset.suffix || "";
const duration = 1800;
const startTime = performance.now();
function tick(now) {
const progress = Math.min((now - startTime) / duration, 1);
// Ease-out cubic
const eased = 1 - Math.pow(1 - progress, 3);
const value = Math.floor(eased * target);
element.textContent = value.toLocaleString() + suffix;
if (progress < 1) {
requestAnimationFrame(tick);
}
}
requestAnimationFrame(tick);
}
function initCounters() {
if (!statCounters.length) return;
const counterObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
animateCounter(entry.target);
counterObserver.unobserve(entry.target);
});
},
{ threshold: 0.5 }
);
statCounters.forEach((el) => counterObserver.observe(el));
}
/* --------------------------------------------------------------------------
Form handling (validation + optional real submission)
-------------------------------------------------------------------------- */
function showFeedback(el, message, isSuccess) {
if (!el) return;
el.textContent = message;
el.classList.remove("success", "error");
el.classList.add(isSuccess ? "success" : "error");
}
function validateEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
async function submitJson(endpoint, payload) {
const res = await fetch(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
// Treat non-2xx as an error to surface to the user
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(text || `Request failed (${res.status})`);
}
// If the API returns JSON, parse it; otherwise ignore.
const contentType = res.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
return await res.json();
}
return null;
}
if (joinForm) {
joinForm.addEventListener("submit", async (e) => {
e.preventDefault();
const feedback = document.getElementById("form-feedback");
const name = joinForm.querySelector("#join-name").value.trim();
const email = joinForm.querySelector("#join-email").value.trim();
const role = joinForm.querySelector("#join-role").value;
if (!name || !email || !role) {
showFeedback(feedback, "Please fill in all fields.", false);
return;
}
if (!validateEmail(email)) {
showFeedback(feedback, "Please enter a valid email address.", false);
return;
}
const endpoint = (joinForm.dataset.endpoint || "").trim();
const payload = { name, email, role, source: "join-form" };
try {
if (endpoint) {
showFeedback(feedback, "Submitting…", true);
await submitJson(endpoint, payload);
showFeedback(feedback, "Submitted! We will be in touch soon.", true);
} else {
// Demo fallback: no endpoint configured.
showFeedback(
feedback,
"Saved (demo). Add a data-endpoint URL to send this to a real backend.",
true
);
try {
const saved = JSON.parse(localStorage.getItem("vtm-leads") || "[]");
saved.push({ ...payload, ts: new Date().toISOString() });
localStorage.setItem("vtm-leads", JSON.stringify(saved));
} catch (_) {
// Ignore localStorage failures (privacy mode, etc.)
}
}
joinForm.reset();
} catch (err) {
showFeedback(
feedback,
"Could not submit right now. Please try again in a moment.",
false
);
}
});
}
if (contactForm) {
contactForm.addEventListener("submit", async (e) => {
e.preventDefault();
const feedback = document.getElementById("contact-feedback");
const name = contactForm.querySelector("#contact-name").value.trim();
const message = contactForm.querySelector("#contact-message").value.trim();
if (!name || !message) {
showFeedback(feedback, "Please complete all fields.", false);
return;
}
const endpoint = (contactForm.dataset.endpoint || "").trim();
const payload = { name, message, source: "contact-form" };
try {
if (endpoint) {
showFeedback(feedback, "Sending…", true);
await submitJson(endpoint, payload);
showFeedback(feedback, "Message sent! We will respond shortly.", true);
} else {
// No backend endpoint configured: open email client with pre-filled content.
const mailUrl = `mailto:${CONTACT_EMAIL}?subject=${encodeURIComponent(
`Website message from ${name}`
)}&body=${encodeURIComponent(message)}`;
window.location.href = mailUrl;
showFeedback(feedback, "Opening your email app…", true);
}
contactForm.reset();
} catch (err) {
showFeedback(
feedback,
"Could not send right now. Please try again in a moment.",
false
);
}
});
}
/* --------------------------------------------------------------------------
Particle canvas background (futuristic grid + nodes)
-------------------------------------------------------------------------- */
function initParticles() {
if (!canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let width = 0;
let height = 0;
let particles = [];
let animationId = null;
// Brand-aligned particle colors
const COLORS = {
green: "rgba(22, 160, 52, 0.6)",
red: "rgba(225, 6, 0, 0.35)",
white: "rgba(255, 255, 255, 0.15)",
};
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
createParticles();
}
function createParticles() {
const count = Math.min(Math.floor((width * height) / 12000), 80);
particles = [];
for (let i = 0; i < count; i++) {
particles.push({
x: Math.random() * width,
y: Math.random() * height,
vx: (Math.random() - 0.5) * 0.4,
vy: (Math.random() - 0.5) * 0.4,
radius: Math.random() * 1.5 + 0.5,
color:
Math.random() > 0.85
? COLORS.red
: Math.random() > 0.5
? COLORS.green
: COLORS.white,
});
}
}
function drawGrid() {
const spacing = 60;
ctx.strokeStyle = "rgba(255, 255, 255, 0.03)";
ctx.lineWidth = 1;
for (let x = 0; x < width; x += spacing) {
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, height);
ctx.stroke();
}
for (let y = 0; y < height; y += spacing) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.stroke();
}
}
function drawParticles() {
particles.forEach((p, i) => {
// Move particle
p.x += p.vx;
p.y += p.vy;
// Wrap around edges
if (p.x < 0) p.x = width;
if (p.x > width) p.x = 0;
if (p.y < 0) p.y = height;
if (p.y > height) p.y = 0;
// Draw dot
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fillStyle = p.color;
ctx.fill();
// Connect nearby particles with lines
for (let j = i + 1; j < particles.length; j++) {
const other = particles[j];
const dx = p.x - other.x;
const dy = p.y - other.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 120) {
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(other.x, other.y);
ctx.strokeStyle = `rgba(22, 160, 52, ${0.15 * (1 - dist / 120)})`;
ctx.lineWidth = 0.5;
ctx.stroke();
}
}
});
}
function render() {
ctx.clearRect(0, 0, width, height);
drawGrid();
if (!prefersReducedMotion) {
drawParticles();
} else {
// Static dots for reduced motion
particles.forEach((p) => {
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fillStyle = p.color;
ctx.fill();
});
}
if (!prefersReducedMotion) {
animationId = requestAnimationFrame(render);
}
}
window.addEventListener("resize", resize);
resize();
render();
// Static render already done for reduced motion; loop only when motion allowed
if (prefersReducedMotion && animationId) {
cancelAnimationFrame(animationId);
}
}
/* --------------------------------------------------------------------------
Smooth scroll offset for fixed header (anchor links)
-------------------------------------------------------------------------- */
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", (e) => {
const id = anchor.getAttribute("href");
if (!id || id === "#") return;
const target = document.querySelector(id);
if (!target) return;
e.preventDefault();
const headerOffset = 80;
const top =
target.getBoundingClientRect().top + window.scrollY - headerOffset;
window.scrollTo({ top, behavior: "smooth" });
});
});
/* --------------------------------------------------------------------------
Initialize all modules
-------------------------------------------------------------------------- */
initReveal();
initCounters();
initParticles();
})();