流星划过背景动画特效

<!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;
            padding: 0;
            height: 100vh;
            overflow: hidden;
            background: radial-gradient(ellipse at bottom, #1B2735 0%, #090A0F 100%);
            font-family: Arial, sans-serif;
        }

        canvas {
            display: block;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1;
        }

        .content {
            position: relative;
            z-index: 2;
            color: white;
            text-align: center;
            padding-top: 20vh;
        }

        h1 {
            font-size: 3em;
            margin-bottom: 20px;
            text-shadow: 0 0 10px rgba(255,255,255,0.5);
        }

        p {
            font-size: 1.2em;
            max-width: 600px;
            margin: 0 auto 30px;
            line-height: 1.6;
        }

        .btn {
            display: inline-block;
            padding: 12px 30px;
            background: rgba(255,255,255,0.2);
            color: white;
            text-decoration: none;
            border-radius: 30px;
            font-size: 1.1em;
            transition: all 0.3s ease;
            border: 1px solid rgba(255,255,255,0.3);
        }

        .btn:hover {
            background: rgba(255,255,255,0.3);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0,0,0,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="meteorCanvas"></canvas>
    
    <div class="content">
        <h1>流星划过夜空</h1>
        <p>欣赏这美丽的流星雨动画效果,每一颗流星都是随机生成的,带来独特的视觉体验。</p>
        <a href="#" class="btn" id="moreMeteors">更多流星</a>
    </div>
    

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('meteorCanvas');
            const ctx = canvas.getContext('2d');
            const moreMeteorsBtn = document.getElementById('moreMeteors');
            
            // 设置canvas尺寸为窗口大小
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            
            // 流星数组
            let meteors = [];
            
            // 流星类
            class Meteor {
                constructor() {
                    this.reset();
                    this.speed = Math.random() * 10 + 5;
                    this.length = Math.random() * 100 + 50;
                }
                
                reset() {
                    this.x = Math.random() * canvas.width;
                    this.y = Math.random() * -100;
                    this.size = Math.random() * 2 + 1;
                    this.color = `hsl(${Math.random() * 60 + 20}, 100%, ${Math.random() * 30 + 70}%)`;
                    this.angle = Math.random() * Math.PI / 4 + Math.PI / 8;
                    this.speedX = Math.cos(this.angle) * this.speed;
                    this.speedY = Math.sin(this.angle) * this.speed;
                    this.alpha = 1;
                    this.decay = Math.random() * 0.02 + 0.01;
                }
                
                update() {
                    this.x += this.speedX;
                    this.y += this.speedY;
                    this.alpha -= this.decay;
                    
                    // 如果流星飞出屏幕或消失,重置
                    if (this.y > canvas.height || this.x > canvas.width || this.x < 0 || this.alpha <= 0) {
                        this.reset();
                    }
                }
                
                draw() {
                    ctx.save();
                    ctx.globalAlpha = this.alpha;
                    
                    // 绘制流星尾迹
                    const gradient = ctx.createLinearGradient(this.x, this.y, this.x - this.speedX * 5, this.y - this.speedY * 5);
                    gradient.addColorStop(0, this.color);
                    gradient.addColorStop(1, 'transparent');
                    
                    ctx.strokeStyle = gradient;
                    ctx.lineWidth = this.size;
                    ctx.beginPath();
                    ctx.moveTo(this.x, this.y);
                    ctx.lineTo(this.x - this.speedX * (this.length / this.speed), this.y - this.speedY * (this.length / this.speed));
                    ctx.stroke();
                    
                    // 绘制流星头部
                    ctx.fillStyle = this.color;
                    ctx.beginPath();
                    ctx.arc(this.x, this.y, this.size * 1.5, 0, Math.PI * 2);
                    ctx.fill();
                    
                    ctx.restore();
                }
            }
            
            // 创建初始流星
            function createMeteors(count) {
                for (let i = 0; i < count; i++) {
                    meteors.push(new Meteor());
                }
            }
            
            // 动画循环
            function animate() {
                requestAnimationFrame(animate);
                
                // 半透明填充制造拖尾效果
                ctx.fillStyle = 'rgba(27, 39, 53, 0.2)';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                // 更新和绘制所有流星
                meteors.forEach(meteor => {
                    meteor.update();
                    meteor.draw();
                });
            }
            
            // 初始化
            createMeteors(15);
            animate();
            
            // 点击按钮添加更多流星
            moreMeteorsBtn.addEventListener('click', function(e) {
                e.preventDefault();
                createMeteors(10);
            });
            
            // 窗口大小改变时调整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、付费专栏及课程。

余额充值