JSDM

HTML

 
1
<!DOCTYPE html>
You don't need a DOCTYPE on CodePen. Just put here what you would normally put in the <body>.
2
<html>
3
<head>
4
    <title>平滑中心流星雨</title>
5
    <style>
6
        body { margin: 0; overflow: hidden; background: #000; }
7
        canvas { display: block; }
8
    </style>
9
</head>
10
<body>
11
    <canvas id="canvas"></canvas>
12
    <script>
13
        const canvas = document.getElementById('canvas');
14
        const ctx = canvas.getContext('2d');
15
        canvas.width = window.innerWidth;
16
        canvas.height = window.innerHeight;
17
18
        class Meteor {
19
            constructor() {
20
                this.active = false;
21
                this.delay = Math.random() * 3000; // 初始随机延迟(0-3秒)
22
                this.trail = [];
23
            }
24
25
            reset() {
26
                this.x = canvas.width/2;
27
                this.y = canvas.height/2;
28
                this.angle = Math.random() * Math.PI * 2;
29
                this.speed = Math.random() * 3 + 1;
30
                this.size = Math.random() * 1 + 0.5;
31
                this.trailLength = 10 + Math.random() * 8;
32
                this.maxDistance = Math.hypot(canvas.width, canvas.height) * 1.2;
33
                this.active = true;
34
            }
35
36
            update() {
37
                if (!this.active) {
38
                    this.delay -= 16;
39
                    if (this.delay <= 0) this.reset();
40
                    return;
41
                }
42
43
                // 轨迹管理
44
                this.trail.push({ x: this.x, y: this.y });
45
                if (this.trail.length > this.trailLength) this.trail.shift();
46
47
                // 位置更新
48
                this.x += Math.cos(this.angle) * this.speed;
49
                this.y += Math.sin(this.angle) * this.speed;
50
51
                // 渐显计算
52
                const dx = this.x - canvas.width/2;
53
                const dy = this.y - canvas.height/2;
54
                this.alpha = Math.min(Math.hypot(dx, dy) / 80, 1);
55
56
                // 边界重置
57
                if (Math.abs(dx) > this.maxDistance || Math.abs(dy) > this.maxDistance) {
58
                    this.active = false;
59
                    this.delay = 100 + Math.random() * 500; // 重生间隔0.1-0.6秒
60
                    this.trail = [];
61
                }
62
            }
63
64
            draw() {
65
                if (!this.active) return;
66
67
                // 绘制尾迹
68
                this.trail.forEach((pos, i) => {
69
                    const ratio = i / this.trailLength;
70
                    ctx.beginPath();
71
                    ctx.arc(pos.x, pos.y, 
72
                           this.size * (1 - ratio) * 0.6, 
73
                           0, Math.PI*2);
74
                    ctx.fillStyle = `rgba(255,255,255,${(1 - ratio) * 0.3 * this.alpha})`;
75
                    ctx.fill();
76
                });
77
78
                // 绘制头部
79
                ctx.beginPath();
80
                ctx.arc(this.x, this.y, this.size, 0, Math.PI*2);
81
                ctx.fillStyle = `rgba(255,255,255,${this.alpha})`;
82
                ctx.fill();
83
            }
84
        }
85
86
        // 创建流星池(数量增加到150)
87
        const meteors = Array(150).fill().map(() => new Meteor());
88
89
        function animate() {
90
            ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
91
            ctx.fillRect(0, 0, canvas.width, canvas.height);
92
93
            meteors.forEach(meteor => {
94
                meteor.update();
95
                meteor.draw();
96
            });
97
98
            requestAnimationFrame(animate);
99
        }
100
101
        window.addEventListener('resize', () => {
102
            canvas.width = window.innerWidth;
103
            canvas.height = window.innerHeight;
104
            meteors.forEach(meteor => {
105
                if (meteor.active) meteor.reset();
106
            });
107
        });
108
109
        animate();
110
    </script>
111
</body>
112
</html>
!

CSS

xxxxxxxxxx
1
 
1
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
1
 
1
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................