-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path流星雨.html
161 lines (157 loc) · 4.45 KB
/
流星雨.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
background-color: black;
width: 100%;
height: 100%;
overflow: hidden;
}
.star {
position: absolute;
width: 0;
height: 0;
opacity: 0.2;
border: 2px solid transparent;
border-bottom: 4px solid #fff;
animation: flash 2s infinite linear;
}
.star::before {
content: "";
position: absolute;
left: -2px;
top: 4px;
border: 2px solid transparent;
border-top: 4px solid #fff;
}
@keyframes flash {
20% {
opacity: 0.2;
}
40% {
opacity: 0.5;
}
60% {
opacity: 1;
}
80% {
opacity: 0.5;
}
100% {
opacity: 0.2;
}
}
</style>
</head>
<body>
<canvas id="Meteor"></canvas>
<script type="text/javascript">
var starCount = 300;
var context;
function starInit() {
var bg = document.querySelector("body");
for (var i = 0; i < starCount; i++) {
var star = document.createElement("div");
star.classList.add("star");
bg.appendChild(star);
}
}
function starPosition() {
var stars = document.querySelectorAll(".star");
for (var i = 0; i < starCount; i++) {
stars[i].style.left = Math.random() * window.innerWidth + "px";
stars[i].style.top = Math.random() * window.innerHeight + "px";
stars[i].style.animationDelay = Math.random() * 10 + "s";
}
}
function init() {
var Meteor = document.getElementById("Meteor");
Meteor.width = window.innerWidth;
Meteor.height = window.innerHeight;
context = Meteor.getContext("2d");
}
function MeteorRain() {
this.x = Math.random() * window.innerWidth;
this.y = Math.random() * window.innerHeight;
this.length = Math.ceil(Math.random() * 80 + 150);
this.angle = 30;
this.cos = Math.cos((this.angle * 3.14) / 180);
this.sin = Math.sin((this.angle * 3.14) / 180);
this.width = this.length * this.cos;
this.height = this.length * this.sin;
this.speed = Math.ceil(Math.random() + 0.5);
this.shifting_x = this.speed * this.cos;
this.shifting_y = this.speed * this.sin;
this.countPos = function () {
this.x = this.x - this.shifting_x;
this.y = this.y + this.shifting_y;
};
this.draw = function () {
context.save();
context.beginPath();
context.lineWidth = 1;
context.globalAlpha = this.alpha;
var line = context.createLinearGradient(
this.x,
this.y,
this.x + this.width,
this.y - this.height
);
line.addColorStop(0, "white");
line.addColorStop(0.5, "grey");
line.addColorStop(1.0, "black");
context.strokeStyle = line;
context.moveTo(this.x, this.y);
context.lineTo(this.x + this.width, this.y - this.height);
context.closePath();
context.stroke();
context.restore();
};
this.move = function () {
var x = this.x + this.width - this.shifting_x;
var y = this.y - this.height + this.shifting_y;
context.clearRect(
x - 3,
y - 3,
this.shifting_x + 5,
this.shifting_y + 5
);
this.countPos();
this.alpha -= 0.002;
this.draw();
};
}
function playRains() {
for (var n = 0; n < rainCount; n++) {
var rain = rains[n];
rain.move();
if (rain.y > window.innerHeight) {
context.clearRect(
rain.x,
rain.y - rain.height,
rain.width,
rain.height
)
rains[n] = new MeteorRain();
}
}
setTimeout("playRains()", 2);
}
var rainCount = 20;
var rains = new Array();
init();
starInit();
starPosition();
for (var i = 0; i < rainCount; i++) {
var rain = new MeteorRain();
rain.draw();
rains.push(rain);
}
playRains();
</script>
</body>
</html>