html5 canvas粒子火圈动画特效

<!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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值