棱镜粒子飞出动画特效

<!DOCTYPE html>
<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: #000;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        
        // 设置画布大小为窗口大小
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        // 粒子数组
        const particles = [];
        const particleCount = 150;
        
        // 颜色数组 - 棱镜效果的颜色
        const colors = [
            '#FF0000', '#FF7F00', '#FFFF00', 
            '#00FF00', '#0000FF', '#4B0082', '#9400D3'
        ];
        
        // 粒子类
        class Particle {
            constructor() {
                this.reset();
                this.radius = Math.random() * 2 + 1;
            }
            
            reset() {
                this.x = canvas.width / 2;
                this.y = canvas.height / 2;
                this.color = colors[Math.floor(Math.random() * colors.length)];
                
                // 随机角度和速度
                const angle = Math.random() * Math.PI * 2;
                const speed = Math.random() * 5 + 1;
                
                this.vx = Math.cos(angle) * speed;
                this.vy = Math.sin(angle) * speed;
                
                // 生命周期
                this.life = 0;
                this.maxLife = Math.random() * 100 + 100;
            }
            
            update() {
                this.x += this.vx;
                this.y += this.vy;
                
                // 增加阻力效果
                this.vx *= 0.98;
                this.vy *= 0.98;
                
                this.life++;
                
                // 如果粒子生命周期结束,重置它
                if (this.life >= this.maxLife || 
                    this.x < 0 || this.x > canvas.width || 
                    this.y < 0 || this.y > canvas.height) {
                    this.reset();
                }
            }
            
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
                
                // 添加发光效果
                ctx.shadowBlur = 10;
                ctx.shadowColor = this.color;
            }
        }
        
        // 初始化粒子
        for (let i = 0; i < particleCount; i++) {
            particles.push(new Particle());
            
            // 让粒子初始位置稍微分散
            particles[i].x += (Math.random() - 0.5) * 50;
            particles[i].y += (Math.random() - 0.5) * 50;
        }
        
        // 动画循环
        function animate() {
            // 清除画布,使用半透明黑色实现拖尾效果
            ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            // 更新并绘制所有粒子
            particles.forEach(particle => {
                particle.update();
                particle.draw();
            });
            
            requestAnimationFrame(animate);
        }
        
        // 开始动画
        animate();
        
        // 窗口大小改变时重置画布大小
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });
        
        // 鼠标移动时改变粒子方向
        window.addEventListener('mousemove', (e) => {
            particles.forEach(particle => {
                const dx = e.clientX - particle.x;
                const dy = e.clientY - particle.y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                
                if (distance < 100) {
                    const forceDirectionX = dx / distance;
                    const forceDirectionY = dy / distance;
                    const force = (100 - distance) / 50;
                    
                    particle.vx = -forceDirectionX * force;
                    particle.vy = -forceDirectionY * force;
                }
            });
        });
    </script>
    

    <div style="position: fixed; bottom: 10px; right: 10px; color: white; font-family: Arial;">

    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值