-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbounce.html
112 lines (100 loc) · 2.46 KB
/
bounce.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
<!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);
let killer = 20000; // No infinite loops
let x = centerX;
let y = centerY;
let dx = 2, dy = 2, radius = 20;
const animate = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
x += dx;
y += dy;
if (x<radius) {
dx *= -1;
killer += 140;
} else if (x>canvas.width-radius) {
dx *= -1;
killer -= 140;
}
if (y<radius) {
dy *= -1;
killer += 140;
} else if (y>canvas.height-radius) {
dy *= -1;
killer -= 140;
}
ctx.fillStyle = 'hsl(' + killer + ', 100%, 50%)';
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
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 = 20000;
animate();
}
});
</script>
</body>
</html>