<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>html5 canvas粒子火圈动画特效</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
display: block;
}
.credit {
position: absolute;
bottom: 10px;
color: rgba(255,255,255,0.3);
font-family: Arial, sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
<canvas id="fireCanvas"></canvas>
<script>
const canvas = document.getElementById('fireCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Particle system
const particles = [];
const particleCount = 300;
const center = {
x: canvas.width / 2,
y: canvas.height / 2
};
const ringRadius = Math.min(canvas.width, canvas.height) * 0.3;
// Particle class
class Particle {
constructor() {
this.reset();
this.angle = Math.random() * Math.PI * 2;
this.distance = ringRadius + (Math.random() * 30 - 15);
}
reset() {
this.x = center.x;
this.y = center.y;
this.size = Math.random() * 5 + 2;
this.color = `hsl(${Math.random() * 30 + 10}, 100%, ${Math.random() * 30 + 50}%)`;
this.speed = Math.random() * 0.2 + 0.05;
this.velocity = {
x: Math.random() * 4 - 2,
y: Math.random() * 4 - 2
};
this.life = 100;
this.decay = Math.random() * 0.5 + 0.1;
this.angle = Math.random() * Math.PI * 2;
this.distance = ringRadius + (Math.random() * 30 - 15);
}
update() {
// Move along the ring
this.angle += this.speed * 0.01;
// Calculate target position on ring
const targetX = center.x + Math.cos(this.angle) * this.distance;
const targetY = center.y + Math.sin(this.angle) * this.distance;
// Move toward target with some randomness
this.x += (targetX - this.x) * 0.1 + this.velocity.x;
this.y += (targetY - this.y) * 0.1 + this.velocity.y;
// Apply random movement
this.velocity.x *= 0.98;
this.velocity.y *= 0.98;
// Add some turbulence
if (Math.random() < 0.1) {
this.velocity.x += Math.random() * 2 - 1;
this.velocity.y += Math.random() * 2 - 1;
}
// Life cycle
this.life -= this.decay;
if (this.life <= 0) {
this.reset();
}
// Fade out as life decreases
this.alpha = this.life / 100;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * (this.life / 100), 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.globalAlpha = this.alpha;
ctx.fill();
ctx.globalAlpha = 1;
}
}
// Create particles
for (let i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
// Animation loop
function animate() {
// Clear with semi-transparent black for trail effect
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Update and draw particles
particles.forEach(particle => {
particle.update();
particle.draw();
});
requestAnimationFrame(animate);
}
// Handle window resize
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
center.x = canvas.width / 2;
center.y = canvas.height / 2;
});
// Start animation
animate();
</script>
</body>
</html>
1058

被折叠的 条评论
为什么被折叠?



