Canvas魔幻线条动画特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas魔幻线条动画特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #000;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
        }
        
        canvas {
            display: block;
        }
        
        .info {
            position: absolute;
            bottom: 20px;
            color: rgba(255, 255, 255, 0.5);
            font-size: 14px;
            text-align: center;
        }
        
        .info a {
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
        }
        
        .title {
            position: absolute;
            top: 20px;
            color: rgba(255, 255, 255, 0.8);
            font-size: 24px;
            text-align: center;
            text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
        }
    </style>
</head>
<body>
    <div class="title">Canvas魔幻线条动画</div>
    <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 lines = [];
        const lineCount = 50;
        const maxDistance = 200;
        const colors = [
            'rgba(255, 0, 255, 0.5)',
            'rgba(0, 255, 255, 0.5)',
            'rgba(255, 255, 0, 0.5)',
            'rgba(0, 255, 0, 0.5)',
            'rgba(255, 0, 0, 0.5)',
            'rgba(0, 0, 255, 0.5)'
        ];
        
        // 线条类
        class Line {
            constructor() {
                this.reset();
                this.z = Math.random() * 4 + 1;
                this.color = colors[Math.floor(Math.random() * colors.length)];
            }
            
            reset() {
                this.x = Math.random() * canvas.width;
                this.y = Math.random() * canvas.height;
                this.vx = Math.random() * 2 - 1;
                this.vy = Math.random() * 2 - 1;
                this.life = 0;
                this.maxLife = Math.random() * 100 + 100;
            }
            
            update() {
                this.x += this.vx;
                this.y += this.vy;
                this.life++;
                
                // 边界检查
                if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height || this.life >= this.maxLife) {
                    this.reset();
                }
            }
            
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.z, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
            }
        }
        
        // 初始化线条
        for (let i = 0; i < lineCount; i++) {
            lines.push(new Line());
        }
        
        // 鼠标位置
        const mouse = {
            x: null,
            y: null
        };
        
        // 跟踪鼠标位置
        window.addEventListener('mousemove', (e) => {
            mouse.x = e.clientX;
            mouse.y = e.clientY;
        });
        
        // 窗口大小改变时重置canvas大小
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });
        
        // 动画循环
        function animate() {
            // 半透明背景制造拖尾效果
            ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            // 更新和绘制所有线条
            for (let i = 0; i < lines.length; i++) {
                lines[i].update();
                lines[i].draw();
                
                // 绘制线条之间的连接
                for (let j = i + 1; j < lines.length; j++) {
                    const dx = lines[i].x - lines[j].x;
                    const dy = lines[i].y - lines[j].y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    if (distance < maxDistance) {
                        const opacity = 1 - distance / maxDistance;
                        ctx.beginPath();
                        ctx.strokeStyle = `rgba(255, 255, 255, ${opacity * 0.2})`;
                        ctx.lineWidth = 0.5;
                        ctx.moveTo(lines[i].x, lines[i].y);
                        ctx.lineTo(lines[j].x, lines[j].y);
                        ctx.stroke();
                    }
                }
                
                // 绘制与鼠标的连接
                if (mouse.x && mouse.y) {
                    const dx = lines[i].x - mouse.x;
                    const dy = lines[i].y - mouse.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    if (distance < maxDistance * 1.5) {
                        const opacity = 1 - distance / (maxDistance * 1.5);
                        ctx.beginPath();
                        ctx.strokeStyle = `rgba(255, 255, 255, ${opacity * 0.3})`;
                        ctx.lineWidth = opacity * 2;
                        ctx.moveTo(lines[i].x, lines[i].y);
                        ctx.lineTo(mouse.x, mouse.y);
                        ctx.stroke();
                    }
                }
            }
            
            requestAnimationFrame(animate);
        }
        
        animate();
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值