-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarfield.htm
More file actions
138 lines (117 loc) · 2.71 KB
/
Copy pathstarfield.htm
File metadata and controls
138 lines (117 loc) · 2.71 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Starfield</title>
<style>
#canvas {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
display: block;
}
canvas {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: -o-crisp-edges;
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {window.setTimeout(callback, 1000 / 60);};
})();
</script>
<script>
var stars_num = 200;
var starColors = ["#ffc", "#cff", "#fff"];
var q = window.innerHeight / window.innerWidth;
var w = 320, h = Math.floor(q * w), d = 10;
var canvas = document.getElementById("canvas");
canvas.height = h;
canvas.width = w;
var ctx = canvas.getContext("2d");
var Star = function()
{
this.x = 0;
this.y = 0;
this.z = 0;
this.type = 1;
this.speed = 1;
};
var cx = w / 2, cy = h / 2;
var stars = new Array(stars_num);
function randomInRange(m, M)
{
return Math.floor(Math.random() * (M - m) + m);
}
function createStar(i)
{
stars[i].x = randomInRange(-cx, cx);
stars[i].y = randomInRange(-cy, cy);
stars[i].z = randomInRange(1, 2 * d);
stars[i].color = randomInRange(0, starColors.length);
}
function createStarfield()
{
for (var i = 0; i < stars.length; i++)
{
stars[i] = new Star();
createStar(i);
}
}
function drawStar(x, y, color)
{
ctx.fillStyle = color;
ctx.fillRect(x, y, 1, 1);
}
function clearScreen()
{
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, w, h);
}
function moveStarfield()
{
clearScreen();
for (var i = 0; i < stars.length; i++)
{
var star = stars[i];
star.z -= 0.1 * (star.color + 1);
if (star.z < 0)
{
createStar(i);
}
var color = starColors[star.color];
var k = d / star.z;
var px = Math.round(k * star.x + cx);
var py = Math.round(k * star.y + cy);
if (0 <= px && px <= w && 0 <= py && py <= h)
{
drawStar(px, py, color);
}
}
}
function startStarField()
{
moveStarfield();
requestAnimFrame(startStarField);
}
function starField()
{
clearScreen();
createStarfield();
startStarField();
}
window.addEventListener("load", starField);
</script>
</body>
</html>