-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
220 lines (183 loc) · 6.93 KB
/
app.js
File metadata and controls
220 lines (183 loc) · 6.93 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
// Constants
const CONFIG = {
ANIMATION: {
THRESHOLD: 0.1,
PARTICLE_DURATION: 1000,
PARTICLE_COUNT: 5,
CODE_RAIN_INTERVAL: 33
},
API: {
URL: 'https://bugtoboss-atrvaawatades-projects.vercel.app/api/submit',
ALERT_DURATION: 5000
}
};
// Utility Functions
const createElement = (className, styles = {}) => {
const element = document.createElement('div');
element.className = className;
Object.assign(element.style, styles);
return element;
};
const getRandomPosition = () => ({
x: Math.random() * window.innerWidth,
y: Math.random() * window.innerHeight
});
const getRandomValue = (min, max) => Math.random() * (max - min) + min;
// Intersection Observer for Animations
const createAnimationObserver = () => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate__animated', 'animate__fadeInUp');
}
});
},
{ threshold: CONFIG.ANIMATION.THRESHOLD }
);
document.querySelectorAll('.section, .timeline-item').forEach(element => {
observer.observe(element);
});
};
// Bug Animation in Hero Section
const createBugs = () => {
const heroSection = document.querySelector('.hero');
if (!heroSection) return;
Array.from({ length: CONFIG.ANIMATION.PARTICLE_COUNT }).forEach(() => {
const bug = createElement('bug', {
left: `${getRandomPosition().x}px`,
top: `${getRandomPosition().y}px`,
animationDelay: `${-getRandomValue(0, 15)}s`
});
heroSection.appendChild(bug);
});
};
// Form Handling with Submit Button Animation
const setupForm = () => {
const form = document.getElementById('submissionForm');
const elements = {
submitButton: document.getElementById('submitButton'),
loading: document.getElementById('loading'),
successAlert: document.getElementById('successAlert'),
errorAlert: document.getElementById('errorAlert')
};
if (!form || !Object.values(elements).every(Boolean)) return;
const showAlert = (element, message) => {
element.style.display = 'block';
element.textContent = message;
setTimeout(() => {
element.style.display = 'none';
}, CONFIG.API.ALERT_DURATION);
};
const handleSubmit = async (e) => {
e.preventDefault();
elements.successAlert.style.display = 'none';
elements.errorAlert.style.display = 'none';
elements.loading.style.display = 'block';
elements.submitButton.disabled = true;
elements.submitButton.classList.add('loading'); // Add animation class
try {
const formData = ['name', 'email', 'github_url', 'linkedin_url', 'twitter_url']
.reduce((acc, field) => ({ ...acc, [field]: form[field].value }), {});
const response = await fetch(CONFIG.API.URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
});
const data = await response.json();
if (response.ok) {
showAlert(elements.successAlert, 'Project submitted successfully!');
form.reset();
} else {
showAlert(elements.errorAlert, data.detail || 'Failed to submit project');
}
} catch (error) {
showAlert(elements.errorAlert, 'Network error. Please try again.');
} finally {
elements.loading.style.display = 'none';
elements.submitButton.disabled = false;
elements.submitButton.classList.remove('loading'); // Remove animation class
}
};
form.addEventListener('submit', handleSubmit);
};
// FAQ Toggle Handling
const setupFAQ = () => {
document.querySelectorAll('.faq-question').forEach(question => {
question.addEventListener('click', () => {
question.parentElement?.classList.toggle('active');
});
});
};
// Particle Effect on Mouse Move
const setupParticleEffect = () => {
const handleMouseMove = (e) => {
const particle = createElement('particle', {
width: `${getRandomValue(2, 7)}px`,
height: `${getRandomValue(2, 7)}px`,
left: `${e.clientX}px`,
top: `${e.clientY}px`
});
const destinationX = (Math.random() - 0.5) * 100;
const destinationY = (Math.random() - 0.5) * 100;
particle.style.setProperty('--x', `${destinationX}px`);
particle.style.setProperty('--y', `${destinationY}px`);
particle.style.animation = 'particle-animation 1s forwards';
document.body.appendChild(particle);
setTimeout(() => particle.remove(), CONFIG.ANIMATION.PARTICLE_DURATION);
};
document.addEventListener('mousemove', handleMouseMove);
};
// Code Rain Effect
const createCodeRain = () => {
const canvas = createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) return null;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const drops = Array(Math.floor(canvas.width / 20)).fill(1);
const draw = () => {
ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#0F0';
ctx.font = '15px monospace';
drops.forEach((drop, i) => {
const text = characters[Math.floor(Math.random() * characters.length)];
ctx.fillText(text, i * 20, drop * 20);
if (drop * 20 > canvas.height && Math.random() > 0.975) {
drops[i] = 0;
}
drops[i]++;
});
};
setInterval(draw, CONFIG.ANIMATION.CODE_RAIN_INTERVAL);
return canvas;
};
// Initialize everything
const init = () => {
createAnimationObserver();
createBugs();
setupForm();
setupFAQ();
setupParticleEffect();
const codeRainCanvas = createCodeRain();
if (codeRainCanvas) {
document.body.appendChild(codeRainCanvas);
}
};
const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const links = document.querySelectorAll('.nav-links a');
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
navLinks.classList.toggle('active');
});
// Close mobile menu when a link is clicked
links.forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
navLinks.classList.remove('active');
});
});
document.addEventListener('DOMContentLoaded', init);