-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathstarry.html
143 lines (117 loc) · 2.74 KB
/
starry.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>星空背景</title>
<style>
html,
body {
height: 100%;
max-width: 100%;
margin: 0;
overflow: hidden;
font-family: sans-serif
}
#space {
width: 100%
}
#warp {
position: absolute;
z-index: 9;
bottom: 0;
left: 0;
right: 0;
margin: 20px auto;
color: rgba(209, 255, 255, 1);
border: 2px solid;
padding: 1em;
width: 10em;
text-align: center;
font-weight: 700;
font-size: 1.2em;
display: inline-block;
text-decoration: none;
background: rgba(0, 0, 0, 0.8)
}
</style>
</head>
<body>
<canvas id="space"></canvas>
<a href="#" id="warp">切换风格与速度</a>
<script>
//based on an Example by @curran
window.requestAnimFrame = (function(){ return window.requestAnimationFrame})();
var canvas = document.getElementById("space");
var c = canvas.getContext("2d");
var numStars = 1900;
var radius = '0.'+Math.floor(Math.random() * 9) + 1 ;
var focalLength = canvas.width *2;
var warp = 0;
var centerX, centerY;
var stars = [], star;
var i;
var animate = true;
initializeStars();
function executeFrame(){
if(animate)
requestAnimFrame(executeFrame);
moveStars();
drawStars();
}
function initializeStars(){
centerX = canvas.width / 2;
centerY = canvas.height / 2;
stars = [];
for(i = 0; i < numStars; i++){
star = {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
z: Math.random() * canvas.width,
o: '0.'+Math.floor(Math.random() * 99) + 1
};
stars.push(star);
}
}
function moveStars(){
for(i = 0; i < numStars; i++){
star = stars[i];
star.z--;
if(star.z <= 0){
star.z = canvas.width;
}
}
}
function drawStars(){
var pixelX, pixelY, pixelRadius;
// Resize to the screen
if(canvas.width != window.innerWidth || canvas.width != window.innerWidth){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
initializeStars();
}
if(warp==0)
{c.fillStyle = "rgba(0,10,20,1)";
c.fillRect(0,0, canvas.width, canvas.height);}
c.fillStyle = "rgba(209, 255, 255, "+radius+")";
for(i = 0; i < numStars; i++){
star = stars[i];
pixelX = (star.x - centerX) * (focalLength / star.z);
pixelX += centerX;
pixelY = (star.y - centerY) * (focalLength / star.z);
pixelY += centerY;
pixelRadius = 1 * (focalLength / star.z);
c.fillRect(pixelX, pixelY, pixelRadius, pixelRadius);
c.fillStyle = "rgba(209, 255, 255, "+star.o+")";
//c.fill();
}
}
document.getElementById('warp').addEventListener("click",function(e){
window.c.beginPath();
window.c.clearRect(0, 0, window.canvas.width, window.canvas.height);
window.warp = warp ? 0 : 1;
executeFrame();
});
executeFrame();
</script>
</body>
</html>