html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Beautiful Fireworks Simulator</title>
<style>
html, body {
margin: 0; padding: 0; overflow: hidden;
background: #000;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
user-select: none;
}
#canvas {
display: block;
position: fixed;
top: 0; left: 0;
width: 100vw;
height: 100vh;
background: linear-gradient(to top, #00111f 0%, #000000 100%);
}
#settings {
position: fixed;
top: 10px; right: 10px;
background: rgba(0,0,0,0.6);
color: white;
padding: 12px 16px;
border-radius: 8px;
font-size: 14px;
max-width: 220px;
z-index: 10;
}
#settings label {
display: flex;
align-items: center;
margin-bottom: 8px;
cursor: pointer;
}
#settings label input {
margin-right: 8px;
}
#settings button {
margin-top: 8px;
width: 100%;
padding: 6px;
background: #222;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
}
#settings button:hover {
background: #444;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="settings">
<label><input type="checkbox" id="toggleFog" checked /> Fog</label>
<label><input type="checkbox" id="toggleClouds" checked /> Volumetric Clouds</label>
<label><input type="checkbox" id="toggleSound" checked /> Sound Effects</label>
<label><input type="checkbox" id="toggleAutoDayNight" checked /> Auto Day/Night Cycle</label>
<button id="toggleDayNightBtn">Toggle Day/Night</button>
</div>
<script>
(() => {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let W, H;
// Settings toggles
const toggleFog = document.getElementById('toggleFog');
const toggleClouds = document.getElementById('toggleClouds');
const toggleSound = document.getElementById('toggleSound');
const toggleAutoDayNight = document.getElementById('toggleAutoDayNight');
const toggleDayNightBtn = document.getElementById('toggleDayNightBtn');
// Resize canvas
function resize() {
W = window.innerWidth;
H = window.innerHeight;
canvas.width = W * devicePixelRatio;
canvas.height = H * devicePixelRatio;
canvas.style.width = W + 'px';
canvas.style.height = H + 'px';
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(devicePixelRatio, devicePixelRatio);
}
window.addEventListener('resize', resize);
resize();
// Utility functions
function random(min, max) {
return Math.random() * (max - min) + min;
}
function randomInt(min, max) {
return Math.floor(random(min, max));
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function easeOutQuad(t) {
return t * (2 - t);
}
// Colors for fireworks
const FIREWORK_COLORS = [
'#ff3f3f', '#ff9a3f', '#fff53f', '#3fff4f', '#3fffff', '#3f3fff', '#9a3fff', '#ff3fff'
];
// Day/Night cycle parameters
let dayNightTime = 0; // 0 to 1, 0 = midnight, 0.5 = noon, 1 = midnight
const dayNightCycleDuration = 60 * 1000; // 60 seconds for full cycle
let lastCycleUpdate = performance.now();
let isDay = false;
// Fog parameters
const fogColorDay = 'rgba(255,255,255,0.05)';
const fogColorNight = 'rgba(0,0,0,0.1)';
// Clouds parameters
const cloudLayers = [];
const cloudCount = 30;
// Fireworks arrays
const fireworks = [];
const particles = [];
// Sound context
let audioCtx;
try {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
} catch {
audioCtx = null;
}
// Sound effect functions
function playLaunchSound() {
if (!audioCtx || !toggleSound.checked) return;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = 'triangle';
osc.frequency.setValueAtTime(300, audioCtx.currentTime);
osc.frequency.exponentialRampToValueAtTime(1000, audioCtx.currentTime + 0.3);
gain.gain.setValueAtTime(0.1, audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.3);
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.start();
osc.stop(audioCtx.currentTime + 0.3);
}
function playExplosionSound() {
if (!audioCtx || !toggleSound.checked) return;
const bufferSize = audioCtx.sampleRate * 0.2;
const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
data[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / bufferSize, 2);
}
const noise = audioCtx.createBufferSource();
noise.buffer = buffer;
const gain = audioCtx.createGain();
gain.gain.setValueAtTime(0.2, audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.2);
noise.connect(gain);
gain.connect(audioCtx.destination);
noise.start();
noise.stop(audioCtx.currentTime + 0.2);
}
// Firework classes
class Firework {
constructor(x, y, targetY, color, type) {
this.x = x;
this.y = y;
this.targetY = targetY;
this.color = color;
this.type = type;
this.speed = random(5, 8);
this.exploded = false;
this.particlesCreated = false;
this.brightness = 1;
}
update() {
if (!this.exploded) {
this.y -= this.speed;
this.speed *= 0.98; // slow down a bit
if (this.y <= this.targetY) {
this.explode();
}
}
}
explode() {
this.exploded = true;
playExplosionSound();
this.createParticles();
}
createParticles() {
if (this.particlesCreated) return;
this.particlesCreated = true;
const count = 50;
switch (this.type) {
case 'burst':
for (let i = 0; i < count; i++) {
const angle = (Math.PI * 2 / count) * i;
const speed = random(2, 6);
particles.push(new Particle(this.x, this.y, Math.cos(angle) * speed, Math.sin(angle) * speed, this.color));
}
break;
case 'star':
for (let i = 0; i < count; i++) {
const angle = (Math.PI * 2 / 5) * (i % 5) + (Math.PI * 2 / count) * i;
const speed = random(2, 5);
particles.push(new Particle(this.x, this.y, Math.cos(angle) * speed, Math.sin(angle) * speed, this.color));
}
break;
case 'ring':
for (let i = 0; i < count; i++) {
const angle = (Math.PI * 2 / count) * i;
const speed = 4;
particles.push(new Particle(this.x + Math.cos(angle) * 10, this.y + Math.sin(angle) * 10, Math.cos(angle) * speed, Math.sin(angle) * speed, this.color));
}
break;
}
}
draw(ctx) {
if (!this.exploded) {
// Draw launch trail
ctx.save();
ctx.fillStyle = this.color;
ctx.shadowColor = this.color;
ctx.shadowBlur = 10;
ctx.beginPath();
ctx.arc(this.x, this.y, 3, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
}
class Particle {
constructor(x, y, vx, vy, color) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.color = color;
this.alpha = 1;
this.size = random(1.5, 3);
this.gravity = 0.05;
this.friction = 0.98;
this.life = 60 + random(0, 30);
this.age = 0;
this.brightness = 1;
}
update() {
this.vx *= this.friction;
this.vy *= this.friction;
this.vy += this.gravity;
this.x += this.vx;
this.y += this.vy;
this.age++;
this.alpha = lerp(1, 0, this.age / this.life);
this.brightness = lerp(1, 0, this.age / this.life);
}
draw(ctx) {
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.fillStyle = this.color;
ctx.shadowColor = this.color;
ctx.shadowBlur = 15 * this.brightness;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
// Clouds generation
function generateClouds() {
cloudLayers.length = 0;
for (let i = 0; i < cloudCount; i++) {
cloudLayers.push({
x: random(0, W),
y: random(H * 0.1, H * 0.4),
size: random(50, 150),
speed: random(0.1, 0.3),
opacity: random(0.05, 0.15),
offset: random(0, 1000)
});
}
}
generateClouds();
// Draw volumetric clouds
function drawClouds(ctx, time) {
ctx.save();
ctx.fillStyle = isDay ? 'rgba(255,255,255,0.15)' : 'rgba(200,200,200,0.1)';
for (const cloud of cloudLayers) {
const x = (cloud.x + cloud.speed * time) % (W + cloud.size) - cloud.size;
const y = cloud.y + Math.sin(time * 0.001 + cloud.offset) * 10;
const gradient = ctx.createRadialGradient(x, y, cloud.size * 0.3, x, y, cloud.size);
gradient.addColorStop(0, `rgba(255,255,255,${cloud.opacity})`);
gradient.addColorStop(1, 'rgba(255,255,255,0)');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.ellipse(x, y, cloud.size * 1.5, cloud.size * 0.6, 0, 0, Math.PI * 2);
ctx.fill();
}
ctx.restore();
}
// Fog drawing
function drawFog(ctx) {
ctx.save();
ctx.fillStyle = isDay ? fogColorDay : fogColorNight;
ctx.fillRect(0, 0, W, H);
ctx.restore();
}
// Background day/night gradient
function drawBackground(ctx) {
// Calculate color based on dayNightTime (0-midnight, 0.5-noon)
// Night: dark blue/black, Day: light blue
const nightColorTop = [0, 17, 31];
const nightColorBottom = [0, 0, 0];
const dayColorTop = [135, 206, 250];
const dayColorBottom = [255, 255, 255];
// Use a smooth curve for brightness
let t = Math.sin(dayNightTime * Math.PI * 2) * 0.5 + 0.5; // 0 to 1
// Interpolate colors
function interpColor(c1, c2, t) {
return c1.map((v, i) => Math.round(lerp(v, c2[i], t)));
}
const topColor = interpColor(nightColorTop, dayColorTop, t);
const bottomColor = interpColor(nightColorBottom, dayColorBottom, t);
const gradient = ctx.createLinearGradient(0, 0, 0, H);
gradient.addColorStop(0, `rgb(${topColor.join(',')})`);
gradient.addColorStop(1, `rgb(${bottomColor.join(',')})`);
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, W, H);
isDay = t > 0.5;
}
// Launch fireworks periodically
let lastLaunch = 0;
const launchInterval = 1200;
function launchFirework() {
const x = random(W * 0.1, W * 0.9);
const y = H;
const targetY = random(H * 0.2, H * 0.5);
const color = FIREWORK_COLORS[randomInt(0, FIREWORK_COLORS.length)];
const types = ['burst', 'star', 'ring'];
const type = types[randomInt(0, types.length)];
fireworks.push(new Firework(x, y, targetY, color, type));
playLaunchSound();
}
// Main animation loop
function animate(time = 0) {
// Update day/night cycle
if (toggleAutoDayNight.checked) {
const now = performance.now();
dayNightTime += (now - lastCycleUpdate) / dayNightCycleDuration;
dayNightTime %= 1;
lastCycleUpdate = now;
}
// Draw background
drawBackground(ctx);
// Draw volumetric clouds
if (toggleClouds.checked) drawClouds(ctx, time);
// Draw fog
if (toggleFog.checked) drawFog(ctx);
// Launch fireworks
if (time - lastLaunch > launchInterval) {
launchFirework();
lastLaunch = time;
}
// Update and draw fireworks
for (let i = fireworks.length - 1; i >= 0; i--) {
const fw = fireworks[i];
fw.update();
fw.draw(ctx);
if (fw.exploded && fw.particlesCreated) {
fireworks.splice(i, 1);
}
}
// Update and draw particles
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.update();
if (p.alpha <= 0) {
particles.splice(i, 1);
} else {
p.draw(ctx);
}
}
requestAnimationFrame(animate);
}
// Manual toggle day/night button
toggleDayNightBtn.addEventListener('click', () => {
toggleAutoDayNight.checked = false;
dayNightTime = (dayNightTime + 0.5) % 1;
});
// Start animation
animate();
// User interaction: click to launch fireworks
canvas.addEventListener('click', e => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const color = FIREWORK_COLORS[randomInt(0, FIREWORK_COLORS.length)];
const types = ['burst', 'star', 'ring'];
const type = types[randomInt(0, types.length)];
fireworks.push(new Firework(x, H, y, color, type));
playLaunchSound();
});
})();
</script>
</body>
</html>