HTML5 Canvas彩虹圈动画特效

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML5 Canvas彩虹圈动画特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: #121212;
            overflow: hidden;
            font-family: Arial, sans-serif;
        }
        
        canvas {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
        }
        
        .title {
            position: relative;
            color: white;
            font-size: 36px;
            text-align: center;
            z-index: 2;
            text-shadow: 0 0 10px rgba(255,255,255,0.5);
        }
        
        .link {
            position: absolute;
            bottom: 20px;
            right: 20px;
            color: white;
            font-size: 14px;
            text-decoration: none;
            font-weight: bold;
            z-index: 3;
        }
    </style>
</head>
<body>
    <h1 class="title">彩虹圈动画特效</h1>
    <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 colors = [
            '#FF0000', '#FF7F00', '#FFFF00',
            '#00FF00', '#0000FF', '#4B0082',
            '#9400D3'
        ];
        
        // 圆环数组
        let rings = [];
        
        // 圆环类
        class Ring {
            constructor(x, y) {
                this.x = x;
                this.y = y;
                this.radius = 5;
                this.maxRadius = Math.random() * 100 + 50;
                this.lineWidth = Math.random() * 10 + 5;
                this.color = colors[Math.floor(Math.random() * colors.length)];
                this.velocity = Math.random() * 2 + 1;
                this.opacity = 1;
                this.life = 100;
            }
            
            update() {
                this.radius += this.velocity;
                this.life--;
                this.opacity = this.life / 100;
                
                if (this.radius > this.maxRadius) {
                    this.velocity = -this.velocity * 0.5;
                }
                
                if (this.life <= 0) {
                    this.reset();
                }
            }
            
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
                ctx.strokeStyle = this.color;
                ctx.lineWidth = this.lineWidth;
                ctx.globalAlpha = this.opacity;
                ctx.stroke();
                ctx.globalAlpha = 1;
            }
            
            reset() {
                this.x = Math.random() * canvas.width;
                this.y = Math.random() * canvas.height;
                this.radius = 5;
                this.maxRadius = Math.random() * 100 + 50;
                this.lineWidth = Math.random() * 10 + 5;
                this.color = colors[Math.floor(Math.random() * colors.length)];
                this.velocity = Math.random() * 2 + 1;
                this.opacity = 1;
                this.life = 100;
            }
        }
        
        // 初始化圆环
        function initRings() {
            for (let i = 0; i < 20; i++) {
                setTimeout(() => {
                    const x = Math.random() * canvas.width;
                    const y = Math.random() * canvas.height;
                    rings.push(new Ring(x, y));
                }, i * 300);
            }
        }
        
        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            ctx.fillStyle = 'rgba(18, 18, 18, 0.1)';
            ctx.fillRect(0, 0, canvas.width, canvas.height);
            
            rings.forEach(ring => {
                ring.update();
                ring.draw();
            });
        }
        
        // 鼠标移动事件
        canvas.addEventListener('mousemove', (e) => {
            rings.push(new Ring(e.clientX, e.clientY));
            
            // 限制圆环数量
            if (rings.length > 50) {
                rings = rings.slice(rings.length - 50);
            }
        });
        
        // 窗口大小调整
        window.addEventListener('resize', () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });
        
        // 点击事件
        canvas.addEventListener('click', (e) => {
            for (let i = 0; i < 5; i++) {
                setTimeout(() => {
                    rings.push(new Ring(e.clientX, e.clientY));
                }, i * 100);
            }
        });
        
        // 启动动画
        initRings();
        animate();
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值