HTML5 Canvas 全屏彩色气泡动画特效

<!DOCTYPE html>
<html lang="zh-CN">
<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: #000;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="bubbleCanvas"></canvas>
    <script>
        // 画布设置
        const canvas = document.getElementById('bubbleCanvas');
        const ctx = canvas.getContext('2d');
        
        // 设置画布为全屏
        function resizeCanvas() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        }
        
        window.addEventListener('resize', resizeCanvas);
        resizeCanvas();
        
        // 气泡数组
        const bubbles = [];
        const bubbleCount = 100;
        
        // 气泡类
        class Bubble {
            constructor() {
                this.reset();
            }
            
            reset() {
                this.x = Math.random() * canvas.width;
                this.y = canvas.height + Math.random() * 100;
                this.size = Math.random() * 20 + 5;
                this.speed = Math.random() * 3 + 1;
                this.opacity = Math.random() * 0.6 + 0.1;
                this.color = `hsla(${Math.random() * 360}, 100%, 50%, ${this.opacity})`;
                this.wave = Math.random() * 2;
                this.waveSpeed = Math.random() * 0.02 + 0.01;
                this.waveOffset = Math.random() * Math.PI * 2;
            }
            
            update() {
                this.y -= this.speed;
                this.x += Math.sin(this.waveOffset + this.y * this.waveSpeed) * this.wave;
                
                if (this.y < -this.size) {
                    this.reset();
                }
            }
            
            draw() {
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
                
                // 添加高光效果
                ctx.beginPath();
                ctx.arc(
                    this.x - this.size * 0.3, 
                    this.y - this.size * 0.3, 
                    this.size * 0.2, 
                    0, 
                    Math.PI * 2
                );
                ctx.fillStyle = `hsla(0, 100%, 100%, ${this.opacity * 0.8})`;
                ctx.fill();
            }
        }
        
        // 初始化气泡
        for (let i = 0; i < bubbleCount; i++) {
            bubbles.push(new Bubble());
            // 让气泡初始位置分散
            bubbles[i].y = Math.random() * canvas.height;
        }
        
        // 动画循环
        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            for (let bubble of bubbles) {
                bubble.update();
                bubble.draw();
            }
            
            requestAnimationFrame(animate);
        }
        
        animate();
        

    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值