流体动力学运动Canvas特效

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流体动力学Canvas特效</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background: #000;
        }
        canvas {
            display: block;
        }
        .info {
            position: absolute;
            bottom: 10px;
            right: 10px;
            color: white;
            font-family: Arial, sans-serif;
            font-size: 12px;
            opacity: 0.5;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
 

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        
        // 设置canvas大小为窗口大小
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        // 流体粒子数量
        const PARTICLE_COUNT = 150;
        // 粒子数组
        const particles = [];
        
        // 粒子类
        class Particle {
            constructor() {
                this.x = Math.random() * canvas.width;
                this.y = Math.random() * canvas.height;
                this.size = Math.random() * 3 + 1;
                this.density = Math.random() * 30 + 1;
                this.color = `hsl(${Math.random() * 60 + 180}, 100%, 50%)`;
                this.vx = Math.random() * 2 - 1;
                this.vy = Math.random() * 2 - 1;
            }
            
            // 绘制粒子
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.closePath();
                ctx.fillStyle = this.color;
                ctx.fill();
            }
            
            // 更新粒子位置
            update() {
                // 边界检测
                if (this.x < 0 || this.x > canvas.width) {
                    this.vx = -this.vx;
                }
                if (this.y < 0 || this.y > canvas.height) {
                    this.vy = -this.vy;
                }
                
                // 更新位置
                this.x += this.vx;
                this.y += this.vy;
                
                // 添加一些随机运动
                if (Math.random() < 0.05) {
                    this.vx += (Math.random() - 0.5) * 0.5;
                    this.vy += (Math.random() - 0.5) * 0.5;
                }
                
                // 限制速度
                const maxSpeed = 2;
                const speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
                if (speed > maxSpeed) {
                    this.vx = (this.vx / speed) * maxSpeed;
                    this.vy = (this.vy / speed) * maxSpeed;
                }
            }
        }
        
        // 初始化粒子
        function init() {
            for (let i = 0; i < PARTICLE_COUNT; i++) {
                particles.push(new Particle());
            }
        }
        
        // 连接相近的粒子
        function connectParticles() {
            for (let a = 0; a < particles.length; a++) {
                for (let b = a; b < particles.length; b++) {
                    const dx = particles[a].x - particles[b].x;
                    const dy = particles[a].y - particles[b].y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    if (distance < 100) {
                        const opacity = 1 - distance / 100;
                        ctx.strokeStyle = `rgba(100, 200, 255, ${opacity})`;
                        ctx.lineWidth = 0.5;
                        ctx.beginPath();
                        ctx.moveTo(particles[a].x, particles[a].y);
                        ctx.lineTo(particles[b].x, particles[b].y);
                        ctx.stroke();
                    }
                }
            }
        }
        
        // 动画循环
        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // 更新并绘制所有粒子
            for (const particle of particles) {
                particle.update();
                particle.draw();
            }
            
            // 连接粒子
            connectParticles();
            
            requestAnimationFrame(animate);
        }
        
        // 窗口大小改变时调整canvas大小
        window.addEventListener('resize', function() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });
        
        // 鼠标互动
        canvas.addEventListener('mousemove', function(event) {
            const mouseX = event.x;
            const mouseY = event.y;
            
            for (const particle of particles) {
                const dx = mouseX - particle.x;
                const dy = mouseY - 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) / 100;
                    
                    particle.vx -= forceDirectionX * force * 0.2;
                    particle.vy -= forceDirectionY * force * 0.2;
                }
            }
        });
        
        // 启动动画
        init();
        animate();
    </script>
</body>
</html>流体动力学运动Canvas特效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值