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: #000;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: Arial, sans-serif;
        }
        
        canvas {
            display: block;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1;
        }
        
        .controls {
            position: fixed;
            bottom: 20px;
            left: 20px;
            z-index: 2;
            display: flex;
            gap: 10px;
        }
        
        button {
            padding: 8px 15px;
            background: rgba(255,255,255,0.1);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        button:hover {
            background: rgba(255,255,255,0.2);
        }
        
        /* 插入的链接样式 */
        .custom-link {
            position: fixed;
            bottom: 20px;
            right: 20px;
            color: rgba(255,255,255,0.7);
            text-decoration: none;
            font-size: 14px;
            background: rgba(0,0,0,0.3);
            padding: 8px 15px;
            border-radius: 20px;
            z-index: 10;
            transition: all 0.3s ease;
        }
        
        .custom-link:hover {
            color: white;
            background: rgba(0,0,0,0.5);
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    
    <div class="controls">
        <button id="addShapes">增加图形</button>
        <button id="changePattern">变换模式</button>
    </div>
    

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('canvas');
            const ctx = canvas.getContext('2d');
            const addBtn = document.getElementById('addShapes');
            const changeBtn = document.getElementById('changePattern');
            
            // 设置canvas尺寸为窗口大小
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            
            // 图形数组
            let shapes = [];
            let patternType = 1;
            const colors = [
                '#FF5252', '#FF4081', '#E040FB', 
                '#7C4DFF', '#536DFE', '#448AFF',
                '#40C4FF', '#18FFFF', '#64FFDA',
                '#69F0AE', '#B2FF59', '#EEFF41',
                '#FFFF00', '#FFD740', '#FFAB40'
            ];
            
            // 图形类
            class Shape {
                constructor(type) {
                    this.type = type;
                    this.x = Math.random() * canvas.width;
                    this.y = Math.random() * canvas.height;
                    this.size = Math.random() * 30 + 10;
                    this.color = colors[Math.floor(Math.random() * colors.length)];
                    this.speedX = Math.random() * 2 - 1;
                    this.speedY = Math.random() * 2 - 1;
                    this.rotation = Math.random() * Math.PI * 2;
                    this.rotationSpeed = Math.random() * 0.02 - 0.01;
                    this.points = this.generatePoints();
                }
                
                generatePoints() {
                    const points = [];
                    const sides = this.type === 1 ? Math.floor(Math.random() * 3) + 3 : 
                                  this.type === 2 ? Math.floor(Math.random() * 4) + 4 : 
                                  Math.floor(Math.random() * 5) + 5;
                    
                    for (let i = 0; i < sides; i++) {
                        const angle = i * (Math.PI * 2 / sides);
                        const distance = this.size * (0.8 + Math.random() * 0.4);
                        points.push({
                            x: Math.cos(angle) * distance,
                            y: Math.sin(angle) * distance
                        });
                    }
                    
                    return points;
                }
                
                update() {
                    this.x += this.speedX;
                    this.y += this.speedY;
                    this.rotation += this.rotationSpeed;
                    
                    // 边界检测
                    if (this.x < -this.size * 2) this.x = canvas.width + this.size;
                    if (this.x > canvas.width + this.size) this.x = -this.size * 2;
                    if (this.y < -this.size * 2) this.y = canvas.height + this.size;
                    if (this.y > canvas.height + this.size) this.y = -this.size * 2;
                }
                
                draw() {
                    ctx.save();
                    ctx.translate(this.x, this.y);
                    ctx.rotate(this.rotation);
                    ctx.fillStyle = this.color;
                    
                    ctx.beginPath();
                    ctx.moveTo(this.points[0].x, this.points[0].y);
                    for (let i = 1; i < this.points.length; i++) {
                        ctx.lineTo(this.points[i].x, this.points[i].y);
                    }
                    ctx.closePath();
                    ctx.fill();
                    
                    ctx.restore();
                }
            }
            
            // 初始化图形
            function initShapes(count = 50) {
                for (let i = 0; i < count; i++) {
                    shapes.push(new Shape(patternType));
                }
            }
            
            // 动画循环
            function animate() {
                requestAnimationFrame(animate);
                
                // 半透明填充制造拖尾效果
                ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                // 更新和绘制所有图形
                shapes.forEach(shape => {
                    shape.update();
                    shape.draw();
                });
            }
            
            // 初始化并开始动画
            initShapes();
            animate();
            
            // 点击按钮增加图形
            addBtn.addEventListener('click', function() {
                initShapes(20);
            });
            
            // 点击按钮变换模式
            changeBtn.addEventListener('click', function() {
                patternType = patternType < 3 ? patternType + 1 : 1;
                shapes = [];
                initShapes();
            });
            
            // 窗口大小改变时调整canvas尺寸
            window.addEventListener('resize', function() {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值