-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
487 lines (438 loc) Β· 16.8 KB
/
Copy pathscript.js
File metadata and controls
487 lines (438 loc) Β· 16.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
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
// Update time
function updateTime() {
const now = new Date();
const time = now.toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit'
});
document.getElementById('current-time').textContent = time;
}
updateTime();
setInterval(updateTime, 1000);
document.getElementById('getWeather').addEventListener('click', () => {
const city = document.getElementById('city').value;
if (city) {
getWeatherByCity(city);
} else {
showError('Please enter a city name to get weather information.');
}
});
document.getElementById('city').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
const city = document.getElementById('city').value;
if (city) {
getWeatherByCity(city);
} else {
showError('Please enter a city name to get weather information.');
}
}
});
function getWeatherByCity(city) {
showLoading();
const apiKey = '2a3174d3d9351fef4e64629ee8de0c3a';
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
hideLoading();
if (data.cod === 200) {
displayWeather(data);
recommendOutfit(data.weather[0].main, data.main.temp);
} else {
handleWeatherError(data);
}
})
.catch(error => {
hideLoading();
console.error('Error fetching weather data:', error);
showError('Connection failed. Please check your internet connection.');
});
}
function showLoading() {
const weatherResult = document.getElementById('weather-result');
weatherResult.innerHTML = `
<div class="loading fade-in">
<div class="loading-spinner"></div>
<p>Loading weather data...</p>
</div>
`;
// Hide outfit while loading
const outfitSection = document.querySelector('.outfit-section');
if (outfitSection) {
outfitSection.classList.remove('is-visible');
outfitSection.classList.add('is-hidden');
}
const outfit = document.getElementById('outfit');
if (outfit) outfit.innerHTML = '';
}
function hideLoading() {
// Loading will be replaced by weather data or error message
}
function handleWeatherError(data) {
let message = '';
switch(data.cod) {
case '404':
message = `City "${document.getElementById('city').value}" not found. Please check spelling and try again.`;
break;
case '401':
message = 'API key error. Please contact support.';
break;
case '429':
message = 'Too many requests. Please try again later.';
break;
default:
message = data.message || 'Unable to fetch weather data.';
}
showError(message);
}
function showError(message) {
document.getElementById('weather-result').innerHTML = `
<div class="error-message fade-in">
<div class="error-icon">β οΈ</div>
<p>${message}</p>
</div>
`;
}
function displayWeather(data) {
const weatherIcon = getWeatherIcon(data.weather[0].main);
const colorClass = getWeatherColor(data.weather[0].main);
const windSpeed = data.wind ? Math.round(data.wind.speed * 3.6) : 'N/A'; // Convert m/s to km/h
// Change background based on weather
changeBackground(data.weather[0].main);
const weatherResult = `
<div class="weather-display fade-in">
<div class="weather-icon ${colorClass} bounce-in">${weatherIcon}</div>
<div class="location slide-in">${data.name}, ${data.sys.country}</div>
<div class="temperature pulse">${Math.round(data.main.temp)}Β°</div>
<div class="description fade-in">${data.weather[0].description}</div>
<div class="weather-details slide-up">
<div class="detail-item">
<div class="detail-icon">π§</div>
<div class="detail-info">
<span class="detail-label">Humidity</span>
<span class="detail-value">${data.main.humidity}%</span>
</div>
</div>
<div class="detail-item">
<div class="detail-icon">π¨</div>
<div class="detail-info">
<span class="detail-label">Wind Speed</span>
<span class="detail-value">${windSpeed} km/h</span>
</div>
</div>
<div class="detail-item">
<div class="detail-icon">π‘οΈ</div>
<div class="detail-info">
<span class="detail-label">Feels like</span>
<span class="detail-value">${Math.round(data.main.feels_like)}Β°</span>
</div>
</div>
</div>
</div>
`;
document.getElementById('weather-result').innerHTML = weatherResult;
// Reveal outfit section smoothly
const outfitSection = document.querySelector('.outfit-section');
if (outfitSection) {
outfitSection.classList.remove('is-hidden');
// Trigger reflow for transition
void outfitSection.offsetWidth;
outfitSection.classList.add('is-visible');
}
}
let lightningIntervalId = null;
function changeBackground(weather) {
const body = document.body;
// Remove all weather classes safely
['sunny','cloudy','rainy','stormy','snowy','foggy'].forEach(c => body.classList.remove(c));
// Remove existing animations/effects
document.querySelectorAll('.rain-animation, .cloud, .snow-animation, .sun-particles, .lightning-flash, .fog-layer').forEach(el => el.remove());
// Clear any existing lightning intervals to avoid stacking
if (lightningIntervalId !== null) {
clearInterval(lightningIntervalId);
lightningIntervalId = null;
}
// Add weather-specific class and animations
switch (weather) {
case 'Clear':
body.classList.add('sunny');
createSunParticles();
createClouds(2);
break;
case 'Clouds':
body.classList.add('cloudy');
createClouds(4);
break;
case 'Rain':
case 'Drizzle':
body.classList.add('rainy');
createRain();
createClouds(3);
break;
case 'Thunderstorm':
body.classList.add('stormy');
createRain();
createClouds(3);
createLightning();
break;
case 'Snow':
body.classList.add('snowy');
createSnow();
createClouds(2);
break;
case 'Mist':
case 'Fog':
body.classList.add('foggy');
createFog();
createClouds(5);
break;
}
}
function createRain() {
const rainContainer = document.createElement('div');
rainContainer.className = 'rain-animation';
// Enhanced rain with better visibility
for (let i = 0; i < 50; i++) {
const drop = document.createElement('div');
drop.className = 'rain-drop';
drop.style.left = Math.random() * 100 + '%';
drop.style.animationDuration = (Math.random() * 0.8 + 0.6) + 's';
drop.style.animationDelay = Math.random() * 2 + 's';
// Enhanced drop styling
drop.style.cssText += `
background: linear-gradient(to bottom, rgba(138, 196, 255, 0.9), rgba(52, 152, 219, 0.6));
box-shadow: 0 0 8px rgba(52, 152, 219, 0.5);
width: 3px;
`;
// Add some variation to drop sizes
const height = Math.random() * 15 + 20;
drop.style.height = height + 'px';
rainContainer.appendChild(drop);
}
document.body.appendChild(rainContainer);
}
function createClouds(count) {
// Limit maximum clouds for performance
const maxClouds = Math.min(count, 4);
for (let i = 0; i < maxClouds; i++) {
const cloud = document.createElement('div');
cloud.className = 'cloud';
cloud.style.top = Math.random() * 50 + '%';
cloud.style.width = (Math.random() * 80 + 40) + 'px';
cloud.style.height = (Math.random() * 30 + 20) + 'px';
cloud.style.animationDuration = (Math.random() * 25 + 20) + 's';
cloud.style.animationDelay = Math.random() * 5 + 's';
document.body.appendChild(cloud);
}
}
function createSnow() {
const snowContainer = document.createElement('div');
snowContainer.className = 'snow-animation';
// Enhanced snow with better visibility
for (let i = 0; i < 35; i++) {
const flake = document.createElement('div');
flake.className = 'snow-flake';
flake.innerHTML = 'β';
flake.style.left = Math.random() * 100 + '%';
flake.style.animationDuration = (Math.random() * 4 + 3) + 's';
flake.style.animationDelay = Math.random() * 2 + 's';
flake.style.fontSize = (Math.random() * 10 + 12) + 'px';
// Enhanced styling for better visibility
flake.style.cssText += `
color: rgba(255, 255, 255, 0.95);
text-shadow: 0 0 12px rgba(255, 255, 255, 0.6);
`;
snowContainer.appendChild(flake);
}
document.body.appendChild(snowContainer);
}
function createSunParticles() {
const particleContainer = document.createElement('div');
particleContainer.className = 'sun-particles';
// Enhanced sun particles with better visibility
for (let i = 0; i < 20; i++) {
const particle = document.createElement('div');
particle.className = 'sun-particle';
particle.style.left = Math.random() * 100 + '%';
particle.style.animationDuration = (Math.random() * 8 + 6) + 's';
particle.style.animationDelay = Math.random() * 3 + 's';
// Enhanced styling for better visibility
const size = Math.random() * 8 + 6;
particle.style.cssText += `
width: ${size}px;
height: ${size}px;
background: radial-gradient(circle, rgba(255, 215, 0, 0.8), rgba(255, 140, 0, 0.6));
box-shadow: 0 0 15px rgba(255, 215, 0, 0.5);
`;
particleContainer.appendChild(particle);
}
document.body.appendChild(particleContainer);
}
function createLightning() {
// Prevent multiple intervals (handled by changeBackground)
lightningIntervalId = setInterval(() => {
const flash = document.createElement('div');
flash.className = 'lightning-flash';
flash.style.cssText = `
background: rgba(255, 255, 255, 0.4);
box-shadow: 0 0 50px rgba(255, 255, 255, 0.6);
`;
document.body.appendChild(flash);
setTimeout(() => flash.remove(), 200);
}, 2500 + Math.random() * 4000);
}
function createFog() {
const fogLayer = document.createElement('div');
fogLayer.className = 'fog-layer';
fogLayer.style.cssText = `
background: linear-gradient(45deg,
rgba(200, 200, 200, 0.3),
rgba(150, 150, 150, 0.2),
rgba(180, 180, 180, 0.3));
backdrop-filter: blur(2px);
`;
document.body.appendChild(fogLayer);
}
function recommendOutfit(weather, temperature) {
let outfit = getOutfitRecommendation(weather, temperature);
document.getElementById('outfit').innerHTML = `
<div class="outfit-display fade-in">
<div class="outfit-main">
<div class="outfit-icons">
${outfit.icons.join(' ')}
</div>
<div class="outfit-text">
<div class="outfit-title">${outfit.title}</div>
<div class="outfit-details">${outfit.description}</div>
${outfit.accessories ? `<div class="outfit-accessories">π ${outfit.accessories}</div>` : ''}
${outfit.colors ? `<div class="outfit-colors">π¨ ${outfit.colors}</div>` : ''}
</div>
</div>
</div>
`;
}
function getOutfitRecommendation(weather, temperature) {
let recommendation = {
icons: [],
title: '',
description: '',
accessories: '',
colors: ''
};
// Base clothing on temperature
if (temperature >= 30) {
recommendation.icons = ['π', 'π©³'];
recommendation.title = 'Hot Weather';
recommendation.description = 'Light, breathable clothing';
recommendation.colors = 'Light colors (white, beige, pastels) to reflect heat';
recommendation.accessories = 'Sunglasses, sun hat, sunscreen';
if (temperature >= 15) {
// light jacket + long pants
recommendation.icons = ['\ud83e\udde5', '\ud83d\udc56'];
recommendation.title = 'Warm Weather';
recommendation.description = 'Comfortable summer attire';
recommendation.colors = 'Bright or light colors';
recommendation.accessories = 'Sunglasses, light cap';
} else if (temperature >= 20) {
recommendation.icons = ['π', 'π'];
recommendation.title = 'Pleasant Weather';
recommendation.description = 'Light layers work well';
recommendation.colors = 'Any colors you like';
recommendation.accessories = 'Light scarf or cardigan';
} else if (temperature >= 15) {
// coat, pants, gloves
recommendation.icons = ['\ud83e\udde5', '\ud83d\udc56', '\ud83e\udde4'];
recommendation.title = 'Cool Weather';
recommendation.description = 'Light jacket recommended';
recommendation.colors = 'Medium tones work well';
recommendation.accessories = 'Light jacket, long sleeves';
} else if (temperature >= 10) {
recommendation.icons = ['π§₯', 'π'];
recommendation.title = 'Chilly Weather';
recommendation.description = 'Warm layers needed';
recommendation.colors = 'Darker colors for warmth';
recommendation.accessories = 'Warm jacket, scarf';
} else if (temperature >= 0) {
recommendation.icons = ['οΏ½', 'π', 'οΏ½π§€'];
recommendation.title = 'Cold Weather';
recommendation.description = 'Heavy winter clothing';
recommendation.colors = 'Dark colors (navy, black, brown)';
recommendation.accessories = 'Winter coat, gloves, warm hat';
} else {
recommendation.icons = ['π§₯', 'π', 'π§€', 'π§£'];
recommendation.title = 'Very Cold';
recommendation.description = 'Full winter gear essential';
recommendation.colors = 'Dark, warm colors';
recommendation.accessories = 'Heavy coat, gloves, scarf, warm boots';
}
// Add weather-specific accessories
switch (weather) {
case 'Rain':
case 'Drizzle':
recommendation.accessories += ', umbrella βοΈ, raincoat π§₯';
recommendation.colors = 'Waterproof materials in any color';
break;
case 'Thunderstorm':
recommendation.accessories += ', umbrella βοΈ, waterproof jacket';
recommendation.description += ' + stay indoors if possible';
break;
case 'Snow':
recommendation.accessories += ', winter boots π₯Ύ, warm socks';
recommendation.colors = 'Dark colors (better heat absorption)';
if (!recommendation.icons.includes('π§€')) recommendation.icons.push('π§€');
if (!recommendation.icons.includes('π§£')) recommendation.icons.push('π§£');
break;
case 'Clear':
if (temperature > 25) {
recommendation.accessories += ', sunglasses πΆοΈ, sun hat π';
}
break;
case 'Mist':
case 'Fog':
recommendation.accessories += ', visibility gear if driving';
break;
}
return recommendation;
}
function getWeatherIcon(weather) {
switch (weather) {
case 'Clear':
return 'βοΈ';
case 'Clouds':
return 'βοΈ';
case 'Rain':
return 'π§οΈ';
case 'Drizzle':
return 'π¦οΈ';
case 'Thunderstorm':
return 'βοΈ';
case 'Snow':
return 'βοΈ';
case 'Mist':
case 'Fog':
return 'π«οΈ';
default:
return 'π€οΈ';
}
}
function getWeatherColor(weather) {
switch (weather) {
case 'Clear':
return 'sunny';
case 'Clouds':
return 'cloudy';
case 'Rain':
case 'Drizzle':
return 'rainy';
case 'Thunderstorm':
return 'stormy';
case 'Snow':
return 'snowy';
case 'Mist':
case 'Fog':
return 'foggy';
default:
return '';
}
}