-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
220 lines (188 loc) · 5.38 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dancing Mosquitoes</title>
<meta name="description" content="Sunset with dancing mosquitoes" />
<link rel="canonical" href="https://dancing-mosquitoes.koenvangilst.nl/" />
<link rel="author" href="https://koenvangilst.nl" />
<meta name="theme-color" content="#8f2404" />
<meta name="creator" content="Koen van Gilst" />
<style>
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
background: conic-gradient(
from 90deg at 50% 50%,
#300904 0deg,
#3f0c04 120deg,
#8f2404 240deg,
#300904 360deg
);
}
canvas {
margin-top: auto;
display: block;
border-radius: 50%;
overflow: hidden;
font-family: monospace;
width: min(70vh, 80%);
max-width: 600px;
filter: drop-shadow(0 0 20px rgba(0, 0, 0, 0.25));
background: conic-gradient(
from 0deg at -30% 30%,
#fbf00d 0deg,
#fba906 120deg,
#de4604 240deg,
#fbf00d 360deg
);
}
#made {
text-align: center;
line-height: 1.5;
font-family: monospace;
margin-top: auto;
margin-bottom: 20px;
font-size: 10px;
color: white;
}
a {
color: white;
text-decoration: none;
border-bottom: 1px solid white;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<p id="made">
made by
<a href="https://koenvangilst.nl/labs/pong-wars">Koen van Gilst</a> |
source on
<a href="https://github.com/vnglst/dancing-mosquitoes">github</a>
</p>
<script>
const sunsetPalette = {
yellow: "#fbf00d",
fierySky: "#fba906",
burntOrange: "#de4604",
emberGlow: "#c54604",
smokyAsh: "#f77706",
deepNight: "#3f0c04",
charcoal: "#8f2404",
darkestIndico: "#1c0c04",
inkyBlack: "#300904",
};
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const flock = [];
const numBoids = 100;
class Boid {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.dx = 0;
this.dy = Math.random() - 0.5;
this.speed = 2;
}
draw() {
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(this.x - 2, this.y + 4);
ctx.lineTo(this.x + 2, this.y + 4);
ctx.closePath();
ctx.fill();
}
update() {
let alignment = this.align();
let cohesion = this.cohere();
let separation = this.separate();
let totalDx = alignment.dx + cohesion.dx + separation.dx;
let totalDy = alignment.dy + cohesion.dy + separation.dy;
// Add inertia to the movement
this.dx += totalDx * 0.01;
this.dy += totalDy * 0.01;
let speed = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
this.dx = (this.dx / speed) * this.speed;
this.dy = (this.dy / speed) * this.speed;
this.x += this.dx;
this.y += this.dy;
if (this.x < 0) this.x = canvas.width;
if (this.x > canvas.width) this.x = 0;
if (this.y < 0) this.y = canvas.height;
if (this.y > canvas.height) this.y = 0;
}
align() {
let dx = 0;
let dy = 0;
let count = 0;
for (let boid of flock) {
if (boid !== this && this.distanceTo(boid) < 50) {
dx += boid.dx;
dy += boid.dy;
count++;
}
}
if (count > 0) {
dx /= count;
dy /= count;
}
return { dx, dy };
}
cohere() {
let dx = 0;
let dy = 0;
let count = 0;
for (let boid of flock) {
if (boid !== this && this.distanceTo(boid) < 80) {
dx += boid.x;
dy += boid.y;
count++;
}
}
if (count > 0) {
dx = dx / count - this.x;
dy = dy / count - this.y;
}
return { dx, dy };
}
separate() {
let dx = 0;
let dy = 0;
for (let boid of flock) {
if (boid !== this && this.distanceTo(boid) < 30) {
dx -= boid.x - this.x;
dy -= boid.y - this.y;
}
}
return { dx, dy };
}
distanceTo(boid) {
let dx = boid.x - this.x;
let dy = boid.y - this.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
for (let i = 0; i < numBoids; i++) {
flock.push(new Boid());
}
let intervalId = setInterval(animate, 1000 / 140);
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let boid of flock) {
boid.update();
boid.draw();
}
}
</script>
</body>
</html>