hailstorm-chasers / script.js
TC12499's picture
put weather forecasting in the app
f13caf0 verified
Raw History Blame Contribute Delete
8.75 kB
// Create hail and lightning animation
function createHailAnimation() {
const container = document.createElement('div');
container.className = 'hail-container';
document.body.appendChild(container);
// Create lightning effect
const lightning = document.createElement('div');
lightning.className = 'lightning';
container.appendChild(lightning);
// Random lightning flashes
function triggerLightning() {
const duration = 2 + Math.random() * 3;
const delay = Math.random() * 15;
lightning.style.animation = `lightning ${duration}s ${delay}s linear`;
setTimeout(() => {
lightning.style.animation = 'none';
setTimeout(triggerLightning, Math.random() * 10000 + 5000); // Next flash in 5-15 seconds
}, (duration + delay) * 1000);
}
// Start lightning
triggerLightning();
// Create car container with lightning illumination effect
const carContainer = document.createElement('div');
carContainer.className = 'car-container';
carContainer.style.filter = 'brightness(0.7)';
carContainer.style.transition = 'filter 0.3s ease';
// Sync car brightness with lightning
lightning.addEventListener('animationstart', () => {
carContainer.style.filter = 'brightness(1.5)';
});
lightning.addEventListener('animationend', () => {
carContainer.style.filter = 'brightness(0.7)';
});
const car = document.createElement('div');
car.className = 'car';
carContainer.appendChild(car);
document.body.appendChild(carContainer);
// Add rain effect
function createRain() {
const rain = document.createElement('div');
rain.className = 'rain';
container.appendChild(rain);
for (let i = 0; i < 100; i++) {
const drop = document.createElement('div');
drop.className = 'rain-drop';
drop.style.left = `${Math.random() * 100}%`;
drop.style.animationDuration = `${0.5 + Math.random() * 0.5}s`;
drop.style.animationDelay = `${Math.random() * 2}s`;
rain.appendChild(drop);
}
}
createRain();
// Generate hail
function generateHail() {
const hail = document.createElement('div');
hail.className = 'hail';
// Random position
const left = Math.random() * 100;
const duration = 2 + Math.random() * 3;
const delay = Math.random() * 5;
const size = 4 + Math.random() * 4;
hail.style.left = `${left}%`;
hail.style.width = `${size}px`;
hail.style.height = `${size}px`;
hail.style.animationDuration = `${duration}s`;
hail.style.animationDelay = `${delay}s`;
container.appendChild(hail);
// Create impact effect when hail hits car
setTimeout(() => {
if (left > 35 && left < 65) { // Only show impacts on car
const impact = document.createElement('div');
impact.className = 'hail-impact';
impact.style.left = `${Math.random() * 80 + 10}%`;
impact.style.top = `${Math.random() * 50 + 20}%`;
car.appendChild(impact);
setTimeout(() => {
impact.remove();
}, 300);
}
}, duration * 1000 * (1 - (50 / window.innerHeight)));
// Remove hail after animation
setTimeout(() => {
hail.remove();
}, duration * 1000);
}
// Initial hail
for (let i = 0; i < 50; i++) {
generateHail();
}
// Continuous hail generation
setInterval(generateHail, 100);
}
document.addEventListener('DOMContentLoaded', function() {
// Create hail animation
createHailAnimation();
// Weather API functions
const API_KEY = 'YOUR_OPENWEATHERMAP_API_KEY'; // Replace with your actual API key
const weatherContainer = document.getElementById('weatherContainer');
const weatherSearchBtn = document.getElementById('weatherSearchBtn');
const locationInput = document.getElementById('locationInput');
async function fetchWeather(location) {
try {
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast?q=${location}&appid=${API_KEY}&units=imperial`);
const data = await response.json();
if (data.cod !== "200") throw new Error(data.message);
// Filter to get one forecast per day (every 8th item in the array)
const dailyForecasts = data.list.filter((item, index) => index % 8 === 0).slice(0, 3);
weatherContainer.innerHTML = dailyForecasts.map(forecast => {
const date = new Date(forecast.dt * 1000);
const iconUrl = `https://openweathermap.org/img/wn/${forecast.weather[0].icon}@2x.png`;
return `
<div class="bg-blue-50 rounded-lg p-4 text-center">
<h3 class="font-semibold text-lg mb-2">${date.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' })}</h3>
<img src="${iconUrl}" alt="${forecast.weather[0].description}" class="mx-auto w-16 h-16">
<div class="flex justify-center space-x-4 mt-2">
<span class="text-2xl font-bold">${Math.round(forecast.main.temp)}°F</span>
<span class="text-gray-600 mt-1">${forecast.weather[0].main}</span>
</div>
<div class="grid grid-cols-2 gap-2 mt-4 text-sm">
<div class="bg-blue-100 rounded p-2">
<i data-feather="wind" class="w-4 h-4 inline mr-1"></i>
${Math.round(forecast.wind.speed)} mph
</div>
<div class="bg-blue-100 rounded p-2">
<i data-feather="droplet" class="w-4 h-4 inline mr-1"></i>
${forecast.main.humidity}%
</div>
</div>
</div>
`;
}).join('');
feather.replace();
} catch (error) {
weatherContainer.innerHTML = `
<div class="col-span-3 text-center p-4 bg-red-50 rounded-lg">
<p class="text-red-600">${error.message}</p>
</div>
`;
}
}
// Event listeners for weather search
weatherSearchBtn.addEventListener('click', () => {
if (locationInput.value.trim()) {
fetchWeather(locationInput.value.trim());
}
});
locationInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && locationInput.value.trim()) {
fetchWeather(locationInput.value.trim());
}
});
// Form submission handler
const appointmentForm = document.getElementById('appointmentForm');
const confirmationModal = document.getElementById('confirmationModal');
if (appointmentForm) {
appointmentForm.addEventListener('submit', function(e) {
e.preventDefault();
// In a real app, you would send this data to your backend
const formData = {
name: document.getElementById('name').value,
email: document.getElementById('email').value,
phone: document.getElementById('phone').value,
vehicle: document.getElementById('vehicle').value,
date: document.getElementById('date').value,
time: document.getElementById('time').value,
notes: document.getElementById('notes').value
};
console.log('Form submitted:', formData); // For debugging
// Show confirmation modal
confirmationModal.classList.remove('hidden');
// Reset form
appointmentForm.reset();
// Close modal after 5 seconds (optional)
// setTimeout(closeModal, 5000);
});
}
// Date picker restrictions (today and future dates only)
const dateInput = document.getElementById('date');
if (dateInput) {
const today = new Date().toISOString().split('T')[0];
dateInput.setAttribute('min', today);
}
});
// Modal functions
function closeModal() {
const confirmationModal = document.getElementById('confirmationModal');
confirmationModal.classList.add('hidden');
}
// Close modal when clicking outside
window.addEventListener('click', function(event) {
const confirmationModal = document.getElementById('confirmationModal');
if (event.target === confirmationModal) {
closeModal();
}
});