contador-da-viagem/main.js
2025-03-21 19:51:20 -03:00

96 lines
3.3 KiB
JavaScript

import { initFireworks, startFireworks, resetFireworks } from './fireworks.js';
// Target date for countdown (YYYY, Month-1, Day, Hour, Minute, Second)
// Default date is set to April 10, 2025
let targetDate = new Date(2025, 3, 10, 0, 0, 0).getTime();
const test = window.location.href.includes('localhost') ||
window.location.href.includes('127.0.0.1') ||
window.location.href.includes('192.168');
if (test) {
// Para teste: descomentar a linha abaixo para simular que a data já passou
targetDate = new Date().getTime() + 10000; // 10 segundos no futuro
}
// Elements
const daysElement = document.getElementById('days');
const hoursElement = document.getElementById('hours');
const minutesElement = document.getElementById('minutes');
const secondsElement = document.getElementById('seconds');
const countdownElement = document.querySelector('.countdown');
const expiredMessageElement = document.getElementById('expired-message');
const canvas = document.getElementById('fireworks-canvas');
const body = document.body;
// Initialize the fireworks
initFireworks(canvas, body);
// Update the target date display
document.getElementById('target-date').textContent = new Date(targetDate).toLocaleDateString('pt-BR', {
day: 'numeric',
month: 'long',
year: 'numeric'
});
// Update the countdown every second
function updateCountdown() {
// Get the current date and time
const now = new Date().getTime();
// Find the distance between now and the target date
const distance = targetDate - now;
// Check if the target date has passed
if (distance < 0) {
// Hide countdown and show expired message
countdownElement.classList.add('hidden');
expiredMessageElement.classList.remove('hidden');
startFireworks();
return;
}
// Time calculations for days, hours, minutes and seconds
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the results
daysElement.innerHTML = days.toString().padStart(2, '0');
hoursElement.innerHTML = hours.toString().padStart(2, '0');
minutesElement.innerHTML = minutes.toString().padStart(2, '0');
secondsElement.innerHTML = seconds.toString().padStart(2, '0');
}
// Function to change the target date
function changeTargetDate(newDate) {
// You can call this function to change the target date dynamically
targetDate = new Date(newDate).getTime();
// Update the target date display
document.getElementById('target-date').textContent = new Date(targetDate).toLocaleDateString('pt-BR', {
day: 'numeric',
month: 'long',
year: 'numeric'
});
// Reset the countdown display
countdownElement.classList.remove('hidden');
expiredMessageElement.classList.add('hidden');
// Reset fireworks if active
resetFireworks();
// Update the countdown immediately
updateCountdown();
}
// Make changeTargetDate available globally
window.changeTargetDate = changeTargetDate;
// Initial call to set correct values
updateCountdown();
// Update the countdown every second
const countdownInterval = setInterval(updateCountdown, 1000);