心形烟花优化展示效果
<!DOCTYPE html>
<html>
<head>
<title>完整心形烟花特效</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
font-family: Arial, sans-serif;
}
#controls {
position: fixed;
top: 20px;
left: 20px;
background: rgba(255,255,255,0.1);
padding: 15px;
border-radius: 8px;
}
button {
padding: 8px 15px;
background: #2196F3;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
margin: 5px;
}
</style>
</head>
<body>
<div id="controls">
<button id="modeBtn">自动模式</button>
<button id="clearBtn">清空</button>
</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let isAutoMode = false;
let fireworks = [];
// 画布自适应
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// 心形生成算法
function generateHeartPoints(centerX, centerY) {
const points = [];
for(let t = 0; t < Math.PI*2; t += 0.015) {
const x = 16 * Math.pow(Math.sin(t), 3);
const y = -13 * Math.cos(t) + 5 * Math.cos(2*t) +
2 * Math.cos(3*t) + Math.cos(4*t);
points.push({
x: centerX + x * 18,
y: centerY + y * 18,
hue: Math.random() * 360,
size: Math.random() * 3 + 2
});
}
return points;
}
// 烟花粒子类
class FireworkParticle {
constructor(startX, startY, target) {
this.x = startX;
this.y = startY;
this.target = target;
this.speed = {
x: (target.x - startX) * 0.04,
y: (target.y - startY) * 0.04
};
this.alpha = 1;
}
update() {
this.x += this.speed.x;
this.y += this.speed.y;
this.alpha -= 0.008;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.target.size, 0, Math.PI*2);
ctx.fillStyle = `hsla(${this.target.hue},100%,60%,${this.alpha})`;
ctx.fill();
}
}
// 烟花发射器类
class Firework {
constructor(targetX, targetY) {
this.particles = [];
this.launchHeight = canvas.height;
this.x = Math.random() * canvas.width;
this.y = this.launchHeight;
this.target = { x: targetX, y: targetY };
this.speed = -15;
this.exploded = false;
}
update() {
if (!this.exploded) {
this.y += this.speed;
if (this.y <= this.target.y) this.explode();
} else {
this.particles.forEach(p => p.update());
this.particles = this.particles.filter(p => p.alpha > 0);
}
}
explode() {
this.exploded = true;
generateHeartPoints(this.target.x, this.target.y).forEach(point => {
this.particles.push(new FireworkParticle(
this.target.x,
this.target.y,
point
));
});
}
draw() {
if (!this.exploded) {
ctx.beginPath();
ctx.arc(this.x, this.y, 3, 0, Math.PI*2);
ctx.fillStyle = `hsla(${Math.random()*360},100%,70%,1)`;
ctx.fill();
} else {
this.particles.forEach(p => p.draw());
}
}
}
// 控制系统
document.getElementById('modeBtn').addEventListener('click', () => {
isAutoMode = !isAutoMode;
modeBtn.textContent = isAutoMode ? '手动模式' : '自动模式';
});
document.getElementById('clearBtn').addEventListener('click', () => {
fireworks = [];
});
canvas.addEventListener('click', e => {
const rect = canvas.getBoundingClientRect();
fireworks.push(new Firework(e.clientX - rect.left, e.clientY - rect.top));
});
// 动画循环
function animate() {
ctx.fillStyle = 'rgba(0,0,0,0.08)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 自动模式生成
if(isAutoMode && fireworks.length < 5 && Math.random() < 0.03) {
fireworks.push(new Firework(
Math.random() * canvas.width,
canvas.height * 0.3
));
}
// 更新所有烟花
fireworks.forEach((fw, index) => {
fw.update();
fw.draw();
if (fw.exploded && fw.particles.length === 0) {
fireworks.splice(index, 1);
}
});
requestAnimationFrame(animate);
}
animate();
// 移动端支持
canvas.addEventListener('touchstart', e => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
const touch = e.touches[0];
fireworks.push(new Firework(
touch.clientX - rect.left,
touch.clientY - rect.top
));
}, { passive: false });
</script>
</body>
</html>