-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.js
More file actions
687 lines (579 loc) · 25.7 KB
/
contact.js
File metadata and controls
687 lines (579 loc) · 25.7 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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Contact Page JavaScript
document.addEventListener('DOMContentLoaded', function() {
// Ensure body is scrollable
document.body.style.overflow = 'auto';
document.documentElement.style.overflow = 'auto';
// Initialize components
setupShaderBackground();
setupFormValidation();
setupAnimations();
setupCustomCursor();
// Test API connectivity
testApiConnectivity();
});
// Custom cursor
function setupCustomCursor() {
const cursorDot = document.getElementById('cursor-dot');
const cursor = document.getElementById('custom-cursor');
if (!cursorDot || !cursor) return;
document.addEventListener('mousemove', (e) => {
const posX = e.clientX;
const posY = e.clientY;
// Update cursor dot position instantly
cursorDot.style.left = `${posX}px`;
cursorDot.style.top = `${posY}px`;
// Move custom cursor with slight delay for smooth effect
setTimeout(() => {
cursor.style.left = `${posX}px`;
cursor.style.top = `${posY}px`;
}, 50);
// Check if cursor is over interactable elements
const target = e.target;
// Add null checks to prevent errors
if (!target) return;
const isLink = target.tagName && target.tagName.toLowerCase() === 'a';
const isButton = target.tagName && target.tagName.toLowerCase() === 'button';
const isParentLink = target.parentElement && target.parentElement.tagName && target.parentElement.tagName.toLowerCase() === 'a';
const isParentButton = target.parentElement && target.parentElement.tagName && target.parentElement.tagName.toLowerCase() === 'button';
const hasContactClass = target.classList && target.classList.contains('contact-method');
const parentHasContactClass = target.parentElement && target.parentElement.classList && target.parentElement.classList.contains('contact-method');
const hasSocialClass = target.classList && target.classList.contains('social-link');
const parentHasSocialClass = target.parentElement && target.parentElement.classList && target.parentElement.classList.contains('social-link');
const hasMapClass = target.classList && target.classList.contains('map-overlay');
const parentHasMapClass = target.parentElement && target.parentElement.classList && target.parentElement.classList.contains('map-overlay');
if (isLink || isButton || isParentLink || isParentButton ||
hasContactClass || parentHasContactClass ||
hasSocialClass || parentHasSocialClass ||
hasMapClass || parentHasMapClass) {
cursor.style.width = '60px';
cursor.style.height = '60px';
cursor.style.opacity = '0.3';
cursorDot.style.opacity = '1';
} else {
cursor.style.width = '40px';
cursor.style.height = '40px';
cursor.style.opacity = '0.6';
cursorDot.style.opacity = '0.8';
}
});
// Hide default cursor
document.body.style.cursor = 'none';
// Handle cursor leaving the window
document.addEventListener('mouseleave', () => {
cursor.style.opacity = '0';
cursorDot.style.opacity = '0';
});
document.addEventListener('mouseenter', () => {
cursor.style.opacity = '0.6';
cursorDot.style.opacity = '0.8';
});
}
// Shader background
function setupShaderBackground() {
if (!window.THREE) {
console.warn('Three.js not loaded');
return;
}
const canvas = document.getElementById('contact-shader-canvas');
if (!canvas) return;
let width = window.innerWidth;
let height = window.innerHeight;
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
alpha: true
});
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
// Create noise texture
const noiseTexture = createNoiseTexture();
// Shader material
const shaderMaterial = createShaderMaterial(noiseTexture);
// Create mesh
const geometry = new THREE.PlaneGeometry(2, 2);
const mesh = new THREE.Mesh(geometry, shaderMaterial);
scene.add(mesh);
// Scroll and mouse tracking
let scrollY = 0;
let scrollVelocity = 0;
let lastScrollY = 0;
let mousePosition = new THREE.Vector2(0.5, 0.5);
// Track scrolling
window.addEventListener('scroll', () => {
scrollY = window.scrollY / (document.body.scrollHeight - window.innerHeight);
// Update scroll velocity
scrollVelocity = scrollY - lastScrollY;
lastScrollY = scrollY;
});
// Track mouse movement
window.addEventListener('mousemove', (e) => {
mousePosition.x = e.clientX / width;
mousePosition.y = 1 - e.clientY / height; // Invert Y for shader space
});
// Handle window resize
window.addEventListener('resize', () => {
width = window.innerWidth;
height = window.innerHeight;
renderer.setSize(width, height);
});
// Animation loop
const clock = new THREE.Clock();
function animate() {
const elapsedTime = clock.getElapsedTime();
// Update uniforms
shaderMaterial.uniforms.u_time.value = elapsedTime;
shaderMaterial.uniforms.u_resolution.value.set(width, height);
shaderMaterial.uniforms.u_mouse.value.copy(mousePosition);
shaderMaterial.uniforms.u_scroll.value = scrollY;
shaderMaterial.uniforms.u_scrollVelocity.value = scrollVelocity;
// Update section colors based on scroll position
updateShaderColors(shaderMaterial, scrollY);
// Render
renderer.render(scene, camera);
requestAnimationFrame(animate);
// Dampen scroll velocity
scrollVelocity *= 0.9;
}
animate();
}
// Create a noise texture for the shader
function createNoiseTexture() {
const size = 256;
const data = new Uint8Array(size * size * 4);
for (let i = 0; i < size * size * 4; i += 4) {
const noise = Math.random() * 255;
data[i] = noise;
data[i+1] = noise;
data[i+2] = noise;
data[i+3] = 255;
}
const texture = new THREE.DataTexture(data, size, size, THREE.RGBAFormat);
texture.needsUpdate = true;
return texture;
}
// Create the shader material
function createShaderMaterial(noiseTexture) {
return new THREE.ShaderMaterial({
uniforms: {
u_time: { value: 0 },
u_resolution: { value: new THREE.Vector2() },
u_mouse: { value: new THREE.Vector2(0.5, 0.5) },
u_scroll: { value: 0 },
u_scrollVelocity: { value: 0 },
u_noiseTexture: { value: noiseTexture },
u_color1: { value: new THREE.Color('#151515') },
u_color2: { value: new THREE.Color('#0a0a0a') },
u_color3: { value: new THREE.Color('#d4b878') },
u_colorMix: { value: 0 }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float u_time;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_scroll;
uniform float u_scrollVelocity;
uniform sampler2D u_noiseTexture;
uniform vec3 u_color1;
uniform vec3 u_color2;
uniform vec3 u_color3;
uniform float u_colorMix;
varying vec2 vUv;
// Simplex 2D noise function
vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
float snoise(vec2 v){
const vec4 C = vec4(0.211324865405187, 0.366025403784439,
-0.577350269189626, 0.024390243902439);
vec2 i = floor(v + dot(v, C.yy) );
vec2 x0 = v - i + dot(i, C.xx);
vec2 i1;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
i = mod(i, 289.0);
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
// Gold color helper
vec3 goldColor(float brightness) {
return vec3(0.83, 0.68, 0.21) * brightness;
}
void main() {
// Calculate aspect-corrected UVs
vec2 uv = vUv;
vec2 aspect = u_resolution / min(u_resolution.x, u_resolution.y);
vec2 uvAspect = (uv - 0.5) * aspect + 0.5;
// Create base gradient
float gradient = smoothstep(0.0, 1.0, uvAspect.y);
// Mouse interaction
float mouseDist = distance(uvAspect, u_mouse);
float mouseGlow = smoothstep(0.6, 0.0, mouseDist) * 0.5;
// Enhanced scroll effects
float scrollSpeed = abs(u_scrollVelocity) * 2.0;
float scrollWarp = sin(uvAspect.y * 10.0 + u_scroll * 0.1) * 0.02;
float scrollLines = sin(uvAspect.y * 50.0 + u_scroll * 0.2) * 0.01 * scrollSpeed;
// Dynamic noise pattern
vec2 noiseCoord = uvAspect * 3.0;
noiseCoord.y += u_time * 0.1;
noiseCoord.x += sin(u_time * 0.2) * 0.2;
float noise1 = snoise(noiseCoord) * 0.5 + 0.5;
// Second layer of noise for more complexity
vec2 noiseCoord2 = uvAspect * 2.0 - u_time * 0.05;
float noise2 = snoise(noiseCoord2) * 0.5 + 0.5;
// Combine different noise layers
float finalNoise = mix(noise1, noise2, 0.5) * 0.3;
// Add parallax effect based on mouse position
vec2 parallaxOffset = (u_mouse - 0.5) * 0.1;
vec2 uvWithParallax = uvAspect + parallaxOffset * (1.0 - gradient);
// Create flowing pattern
float flowPattern = sin(uvWithParallax.x * 10.0 + u_time * 0.5 + u_scroll * 0.05) *
cos(uvWithParallax.y * 8.0 - u_time * 0.3) * 0.5 + 0.5;
// Enhanced light beams effect - more golden
float beam = pow(sin(uvAspect.x * 3.14159 * 2.0 + u_time * 0.2), 20.0) * 0.15;
beam *= smoothstep(0.4, 0.0, abs(uvAspect.y - 0.5));
// Enhanced particles effect - more gold particles
float particles = 0.0;
float goldParticles = 0.0;
// Regular particles
for (int i = 0; i < 6; i++) {
float idx = float(i) / 6.0;
vec2 particleUV = uvAspect;
particleUV.y -= mod(u_time * (0.1 + idx * 0.1) + idx * 0.5, 1.0);
particleUV.x += sin(particleUV.y * 10.0 + u_time * idx) * 0.2;
float size = 0.004 * (sin(idx * 6.28 + u_time) * 0.5 + 1.5);
particles += smoothstep(size, 0.0, length(fract(particleUV * 10.0) - 0.5));
}
// Gold accent particles (larger, fewer, slower)
for (int i = 0; i < 4; i++) {
float idx = float(i) / 4.0;
vec2 particleUV = uvAspect;
particleUV.y -= mod(u_time * (0.05 + idx * 0.05) + idx * 0.7, 1.0);
particleUV.x += sin(particleUV.y * 5.0 + u_time * idx * 0.5) * 0.3;
float size = 0.007 * (sin(idx * 6.28 + u_time * 0.5) * 0.5 + 1.5);
goldParticles += smoothstep(size, 0.0, length(fract(particleUV * 5.0) - 0.5));
}
// Enhanced vignette effect with golden edge
float vignetteRadius = length(uvAspect - 0.5) * 1.8;
float vignetteDark = smoothstep(0.7, 1.4, vignetteRadius); // Darker outer vignette
float vignetteGold = smoothstep(0.5, 1.0, vignetteRadius) * smoothstep(1.2, 0.8, vignetteRadius); // Gold ring
// Color mixing
vec3 baseColor = mix(u_color1, u_color2, gradient + scrollWarp + finalNoise * 0.3);
baseColor = mix(baseColor, u_color3, flowPattern * 0.2);
// Apply lighting effects
baseColor += mouseGlow * vec3(1.0, 0.95, 0.9);
baseColor += beam * goldColor(1.2); // More golden beams
baseColor += particles * 0.15 * mix(vec3(1.0), goldColor(1.0), 0.3); // Regular particles
baseColor += goldParticles * 0.25 * goldColor(1.3); // Gold particles are brighter
// Apply gold vignette
baseColor = mix(baseColor, goldColor(0.4), vignetteGold * 0.15);
// Apply color transition based on section
baseColor = mix(baseColor, u_color3, u_colorMix * 0.3);
// Final color with effects
vec3 finalColor = baseColor * (1.0 - vignetteDark * 0.8); // Stronger vignette
// Add film grain
float grain = texture2D(u_noiseTexture, uv * 3.0 + vec2(u_time * 0.1, 0.0)).r * 0.05 - 0.025;
finalColor += grain;
// Apply subtle color correction - slightly warmer
finalColor = pow(finalColor, vec3(0.95, 0.97, 1.0));
gl_FragColor = vec4(finalColor, 0.7 + mouseDist * 0.15);
}
`,
transparent: true
});
}
// Update shader colors based on scroll position
function updateShaderColors(shaderMaterial, scrollY) {
// Create a smooth transition between sections
const colorTransitionPoints = [
{ pos: 0.0, color1: '#151515', color2: '#0a0a0a', accent: '#d4b878' }, // Top
{ pos: 0.4, color1: '#131313', color2: '#080808', accent: '#c5b070' }, // Middle
{ pos: 1.0, color1: '#111111', color2: '#050505', accent: '#b6a068' } // Bottom
];
// Find the two transition points we're between
let lowerPoint = colorTransitionPoints[0];
let upperPoint = colorTransitionPoints[1];
for (let i = 0; i < colorTransitionPoints.length - 1; i++) {
if (scrollY >= colorTransitionPoints[i].pos && scrollY <= colorTransitionPoints[i + 1].pos) {
lowerPoint = colorTransitionPoints[i];
upperPoint = colorTransitionPoints[i + 1];
break;
}
}
// Calculate interpolation factor
const range = upperPoint.pos - lowerPoint.pos;
const factor = range !== 0 ? (scrollY - lowerPoint.pos) / range : 0;
// Interpolate colors
const color1 = new THREE.Color(lowerPoint.color1).lerp(new THREE.Color(upperPoint.color1), factor);
const color2 = new THREE.Color(lowerPoint.color2).lerp(new THREE.Color(upperPoint.color2), factor);
const color3 = new THREE.Color(lowerPoint.accent).lerp(new THREE.Color(upperPoint.accent), factor);
// Update shader uniforms
shaderMaterial.uniforms.u_color1.value = color1;
shaderMaterial.uniforms.u_color2.value = color2;
shaderMaterial.uniforms.u_color3.value = color3;
shaderMaterial.uniforms.u_colorMix.value = Math.sin(scrollY * Math.PI) * 0.5 + 0.5;
}
// Form validation
function setupFormValidation() {
const form = document.getElementById('contact-form');
if (!form) return;
form.addEventListener('submit', function(e) {
e.preventDefault();
// Gather form data
const formData = new FormData(form);
const data = Object.fromEntries(formData.entries());
// Validate form (basic validation)
let isValid = true;
let errorMessages = [];
if (!data.name || data.name.trim() === '') {
isValid = false;
errorMessages.push('Please enter your name');
}
if (!data.email || !isValidEmail(data.email)) {
isValid = false;
errorMessages.push('Please enter a valid email address');
}
if (!data['inquiry-type'] || data['inquiry-type'] === '') {
isValid = false;
errorMessages.push('Please select an inquiry type');
}
if (!data.message || data.message.trim() === '') {
isValid = false;
errorMessages.push('Please enter your message');
}
// TODO: Re-implement reCAPTCHA verification when integration is fixed
// Verify CAPTCHA - temporarily disabled
/*
const recaptchaResponse = grecaptcha.getResponse();
if (recaptchaResponse.length === 0) {
isValid = false;
errorMessages.push('Please complete the CAPTCHA verification');
showErrorMessage('Please complete the CAPTCHA verification to submit the form.');
return;
}
*/
// Handle validation result
if (isValid) {
// TODO: Re-add reCAPTCHA token when re-implemented
// Add reCAPTCHA token to the data
// data['g-recaptcha-response'] = recaptchaResponse;
// Submit form
submitForm(data);
} else {
// Display error messages
console.error('Form validation failed:', errorMessages);
showErrorMessage('Please correct the following issues:<br>' + errorMessages.join('<br>'));
}
});
}
// Validate email format
function isValidEmail(email) {
const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regex.test(String(email).toLowerCase());
}
// Simulated form submission (replace with actual submission logic)
function submitForm(data) {
// Show loading state
const submitButton = document.querySelector('.submit-button');
const originalButtonText = submitButton.innerHTML;
submitButton.innerHTML = `<span>Sending...</span> <i class="fas fa-spinner fa-spin"></i>`;
submitButton.disabled = true;
// Log submission for debugging
console.log('Submitting form to API:', data);
// API endpoint URL - make sure this is correct
const apiUrl = 'https://gabriel-cavazos-contact-api.vercel.app/api/contact';
console.log('Submitting to URL:', apiUrl);
// Send data to the backend server
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
credentials: 'omit', // Important for CORS
mode: 'cors' // Explicitly set CORS mode
})
.then(response => {
if (!response.ok) {
// If the response is not in the 200-299 range
// Get more detailed error information
return response.json().then(errorData => {
throw new Error(errorData.message || 'Server error occurred');
});
}
return response.json();
})
.then(result => {
// Reset button
submitButton.innerHTML = originalButtonText;
submitButton.disabled = false;
if (result.success) {
// Show success message
showSuccessMessage(result.message);
document.getElementById('contact-form').reset();
// TODO: Re-enable reCAPTCHA reset when re-implemented
// grecaptcha.reset(); // Reset the CAPTCHA
} else {
// Show error message
showErrorMessage(result.message || 'Could not send your message. Please try again.');
}
})
.catch(error => {
// Handle error
console.error('Submission error:', error);
submitButton.innerHTML = originalButtonText;
submitButton.disabled = false;
// Check if it's a connection error
const errorMessage = error.message || '';
if (errorMessage.includes('Failed to fetch') ||
errorMessage.includes('NetworkError') ||
errorMessage.includes('connection') ||
errorMessage.includes('network')) {
showErrorMessage(`Unable to connect to the server. Please check your internet connection and try again, or email me directly at gcavazo1@gmail.com.`);
}
// Display more user-friendly error message
else if (errorMessage.includes('CAPTCHA')) {
showErrorMessage('Server error occurred. Please try again later or contact us directly at gcavazo1@gmail.com.');
} else {
showErrorMessage('Error connecting to server. Please try again later or contact us directly at gcavazo1@gmail.com.');
}
});
}
// Display success message
function showSuccessMessage(message) {
// Create success message element
const successElement = document.createElement('div');
successElement.className = 'message-overlay success-message';
successElement.innerHTML = `
<div class="message-box">
<div class="message-icon">
<i class="fas fa-check-circle"></i>
</div>
<h3>Message Sent!</h3>
<p>${message}</p>
<button class="close-message">Close</button>
</div>
`;
// Add to body
document.body.appendChild(successElement);
// Add animation
setTimeout(() => {
successElement.classList.add('visible');
}, 10);
// Handle close button
successElement.querySelector('.close-message').addEventListener('click', () => {
successElement.classList.remove('visible');
setTimeout(() => {
document.body.removeChild(successElement);
}, 300);
});
}
// Display error message
function showErrorMessage(message) {
// Create error message element
const errorElement = document.createElement('div');
errorElement.className = 'message-overlay error-message';
errorElement.innerHTML = `
<div class="message-box">
<div class="message-icon">
<i class="fas fa-exclamation-circle"></i>
</div>
<h3>Something Went Wrong</h3>
<p>${message}</p>
<button class="close-message">Close</button>
</div>
`;
// Add to body
document.body.appendChild(errorElement);
// Add animation
setTimeout(() => {
errorElement.classList.add('visible');
}, 10);
// Handle close button
errorElement.querySelector('.close-message').addEventListener('click', () => {
errorElement.classList.remove('visible');
setTimeout(() => {
document.body.removeChild(errorElement);
}, 300);
});
}
// Animations
function setupAnimations() {
// Initialize animations
const elementsToAnimate = document.querySelectorAll('.section-to-animate');
// Set up Intersection Observer
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-in');
// Stop observing once animated
observer.unobserve(entry.target);
}
});
}, {
threshold: 0.1, // Trigger when 10% of the element is visible
rootMargin: '0px 0px -100px 0px' // Adjust based on when you want animation to trigger
});
// Observe all elements
elementsToAnimate.forEach(element => {
observer.observe(element);
});
// Initial animation for elements that are already visible
setTimeout(() => {
elementsToAnimate.forEach(element => {
const rect = element.getBoundingClientRect();
if (rect.top < window.innerHeight) {
element.classList.add('animate-in');
observer.unobserve(element);
}
});
}, 100);
}
// Test API connectivity
function testApiConnectivity() {
console.log('Testing API connectivity...');
const apiUrl = 'https://gabriel-cavazos-contact-api.vercel.app/api/health';
fetch(apiUrl, {
method: 'GET',
mode: 'cors',
credentials: 'omit'
})
.then(response => {
if (!response.ok) {
console.error('API health check failed with status:', response.status);
return;
}
return response.json();
})
.then(data => {
if (data && data.status === 'ok') {
console.log('API health check passed:', data);
} else {
console.error('API health check returned unexpected data:', data);
}
})
.catch(error => {
console.error('API health check failed:', error);
});
}