炫酷粒子动画

85 阅读2分钟
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>彩色粒子追逐</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background-color: #111;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
        }
        canvas {
            border: 2px solid #333;
            border-radius: 5px;
            box-shadow: 0 0 20px rgba(0, 150, 255, 0.3);
        }
        #info {
            position: absolute;
            top: 10px;
            left: 10px;
            color: white;
            background-color: rgba(0, 0, 0, 0.5);
            padding: 10px;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div id="info">粒子数量: <span id="count">0</span> | 点击添加粒子</div>
    <canvas id="particleCanvas"></canvas>

    <script>
        const canvas = document.getElementById('particleCanvas');
        const ctx = canvas.getContext('2d');
        const countDisplay = document.getElementById('count');
        
        // 设置画布大小为窗口大小
        canvas.width = window.innerWidth * 0.9;
        canvas.height = window.innerHeight * 0.9;
        
        // 鼠标位置
        let mouseX = canvas.width / 2;
        let mouseY = canvas.height / 2;
        
        // 跟踪鼠标移动
        canvas.addEventListener('mousemove', (e) => {
            const rect = canvas.getBoundingClientRect();
            mouseX = e.clientX - rect.left;
            mouseY = e.clientY - rect.top;
        });
        
        // 点击添加粒子
        canvas.addEventListener('click', (e) => {
            const rect = canvas.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;
            createParticle(x, y);
        });
        
        // 粒子类
        class Particle {
            constructor(x, y) {
                this.x = x;
                this.y = y;
                this.size = Math.random() * 5 + 2;
                this.speed = Math.random() * 2 + 1;
                this.color = getRandomColor();
                this.direction = Math.random() * Math.PI * 2;
                this.life = 1000 + Math.random() * 2000; // 粒子寿命
                this.age = 0;
            }
            
            update() {
                // 计算指向鼠标的方向
                const dx = mouseX - this.x;
                const dy = mouseY - this.y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                
                // 如果鼠标足够近,粒子会追逐鼠标
                if (distance < 300) {
                    this.direction = Math.atan2(dy, dx);
                } else {
                    // 否则随机移动
                    this.direction += (Math.random() - 0.5) * 0.2;
                }
                
                // 移动粒子
                this.x += Math.cos(this.direction) * this.speed;
                this.y += Math.sin(this.direction) * this.speed;
                
                // 边界检查
                if (this.x < 0) this.x = canvas.width;
                if (this.x > canvas.width) this.x = 0;
                if (this.y < 0) this.y = canvas.height;
                if (this.y > canvas.height) this.y = 0;
                
                // 年龄增长
                this.age++;
                
                // 粒子逐渐变小
                if (this.age > this.life * 0.7) {
                    this.size *= 0.99;
                }
            }
            
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
                
                // 添加发光效果
                ctx.shadowBlur = 10;
                ctx.shadowColor = this.color;
            }
            
            isDead() {
                return this.age >= this.life || this.size < 0.5;
            }
        }
        
        // 粒子数组
        let particles = [];
        
        // 创建初始粒子
        function createInitialParticles() {
            for (let i = 0; i < 30; i++) {
                createParticle(
                    Math.random() * canvas.width,
                    Math.random() * canvas.height
                );
            }
        }
        
        // 创建新粒子
        function createParticle(x, y) {
            particles.push(new Particle(x, y));
            updateCount();
        }
        
        // 更新粒子数量显示
        function updateCount() {
            countDisplay.textContent = particles.length;
        }
        
        // 随机颜色生成
        function getRandomColor() {
            const hue = Math.floor(Math.random() * 360);
            return `hsl(${hue}, 100%, 50%)`;
        }
        
        // 动画循环
        function animate() {
            // 清除画布(使用半透明黑色创造拖尾效果)
            ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            // 更新和绘制所有粒子
            for (let i = particles.length - 1; i >= 0; i--) {
                particles[i].update();
                particles[i].draw();
                
                // 移除死亡的粒子
                if (particles[i].isDead()) {
                    particles.splice(i, 1);
                    updateCount();
                }
                
                // 检测粒子碰撞
                for (let j = i + 1; j < particles.length; j++) {
                    const p1 = particles[i];
                    const p2 = particles[j];
                    const dx = p1.x - p2.x;
                    const dy = p1.y - p2.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    if (distance < p1.size + p2.size) {
                        // 碰撞时交换颜色
                        [p1.color, p2.color] = [p2.color, p1.color];
                        
                        // 有几率分裂粒子
                        if (Math.random() < 0.1 && particles.length < 500) {
                            createParticle(p1.x, p1.y);
                            createParticle(p2.x, p2.y);
                        }
                    }
                }
            }
            
            // 绘制鼠标位置的光标
            ctx.beginPath();
            ctx.arc(mouseX, mouseY, 10, 0, Math.PI * 2);
            ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
            ctx.fill();
            
            requestAnimationFrame(animate);
        }
        
        // 初始化
        createInitialParticles();
        animate();
        
        // 窗口大小调整
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth * 0.9;
            canvas.height = window.innerHeight * 0.9;
        });
    </script>
</body>
</html>