-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstarfield.html
126 lines (113 loc) · 3.04 KB
/
starfield.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pretty pretty starfield</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#target {
background: black;
height: 100vh;
width: 100vw;
z-index: -1;
position: fixed;
}
button {
background: transparent;
border: 1px solid transparent;
border-radius: 5px;
box-shadow: 0 0 5px 2px #0f0;
color: #0f0;
margin: 1em;
padding: 0.3em;
cursor: pointer;
}
#counter {
cursor: all-scroll;
position: absolute;
top: 0;
right: 0;
}
</style>
</head>
<body>
<canvas id="target"></canvas>
<div id="wrapper">
<button id="stahp">That one button</button>
<button id="counter"></button>
</div>
<script type="text/javascript">
// Are you KIDDING me with this type casting?
/** @type {HTMLCanvasElement} */
const canvas = document.querySelector('#target');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/** @type {CanvasRenderingContext2d} */
const ctx = canvas.getContext('2d');
const centerX = Math.ceil(canvas.width/2);
const centerY = Math.ceil(canvas.height/2);
const stars = [];
for (let i = 0; i<30; i++) {
// Create stars, but force immediate resets
stars.push({ x: -100 });
};
let killer = 1336; // No infinite loops
const oof = Math.PI*2;
const animate = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
stars.forEach((star) => {
// Reset star?
if (star.x < 0
|| star.y < 0
|| star.x > canvas.width+2
|| star.y > canvas.height+2)
{
star.speed = Math.random();
star.radius = Math.random()+0.2;
star.deltax = Math.random()*2 - 1;
star.deltay = Math.random()*2 - 1;
star.x = centerX;
star.y = centerY;
// my starfield is rainbowy, what of it?
star.color = 'hsl('+killer+', 100%, 80%)';
if (killer % 10 === 0) {
star.color = "#e22";
} else if (killer % 31 === 0) {
star.color = "#22e";
}
}
star.x += star.deltax * star.speed;
star.y += star.deltay * star.speed;
star.speed *= 1.05;
star.radius *= 1.01;
ctx.fillStyle = star.color;
ctx.beginPath();
ctx.arc(star.x, star.y, star.radius, 0, oof);
ctx.closePath();
ctx.fill();
});
if (killer > 0) {
requestAnimationFrame(animate);
}
killer--;
document.querySelector('#counter').innerHTML = killer;
};
animate();
const btn = document.querySelector('#stahp');
btn.addEventListener('click', () => {
if (killer > 0) {
killer = 0;
} else {
killer = 1337;
animate();
}
});
</script>
</body>
</html>