-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
288 lines (253 loc) · 8.69 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Time Flies - Koen van Gilst</title>
<meta
name="description"
content="A visualization of the passage of time using flies. Written in JavaScript with some HTML & CSS in one index.html. Feel free to reuse the code and create your own version."
/>
<link rel="canonical" href="https://time-flies.koenvangilst.nl/" />
<link rel="author" href="https://koenvangilst.nl" />
<meta name="theme-color" content="#130301" />
<meta name="creator" content="Koen van Gilst" />
<script
defer
data-domain="time-flies.koenvangilst.nl"
src="https://plausible.koenvangilst.nl/js/script.js"
></script>
<style>
*,
*:before,
*:after {
box-sizing: inherit;
}
html,
body {
box-sizing: border-box;
height: 110vh;
margin: 0;
display: flex;
flex-direction: column;
justify-content: center;
background: conic-gradient(from 90deg at 50% 50%, #130301 0deg, #1d0501 120deg, #000000 240deg, #170401 360deg);
}
.container {
flex-grow: 3;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
user-select: none;
-webkit-user-select: none;
}
canvas {
border-radius: 50%;
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%, #edecdb 0deg, #f3eee3 120deg, #f8e2d9 240deg, #f9f7e1 360deg);
}
.label {
margin-top: 25px;
color: rgb(220, 220, 220);
font-family: monospace;
font-size: 1rem;
}
#made {
text-align: center;
line-height: 1.8;
font-family: monospace;
margin-top: auto;
margin-bottom: 20px;
font-size: 10px;
color: rgb(220, 220, 220);
}
a {
color: white;
text-decoration: none;
border-bottom: 1px solid white;
}
</style>
</head>
<body>
<div class="container">
<canvas id="canvas" width="800" height="800"></canvas>
<div class="label">time flies</div>
</div>
<p id="made">
made by
<a href="https://koenvangilst.nl">Koen van Gilst</a> | idea by
<a href="http://www.mennoanker.nl/">Menno Anker</a> <br />
source on
<a href="https://github.com/vnglst/time-flies">github</a>
</p>
<script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
class Fly {
constructor(x, y) {
this.x = x;
this.y = y;
this.targetX = x;
this.targetY = y;
this.speed = Math.random() * 4 + 1;
this.size = 2;
this.lastDirChange = Date.now();
this.oscillationOffset = Math.random() * Math.PI * 2;
this.oscillationDirX = Math.random() < 0.5 ? 1 : -1;
this.oscillationDirY = Math.random() < 0.5 ? 1 : -1;
}
update(flies) {
const dx = this.targetX - this.x;
const dy = this.targetY - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
this.x += (dx / distance) * this.speed;
this.y += (dy / distance) * this.speed;
} else {
this.x += Math.random() * 4 - 2;
this.y += Math.random() * 4 - 2;
}
const timeFactor = Date.now() / 100 + this.oscillationOffset;
const oscillationX = this.oscillationDirX * Math.sin(timeFactor) * 3;
const oscillationY = this.oscillationDirY * Math.cos(timeFactor) * 3;
this.x += oscillationX;
this.y += oscillationY;
if (Date.now() - this.lastDirChange > Math.random() * 1000 + 500) {
this.oscillationDirX *= -1;
this.oscillationDirY *= -1;
this.lastDirChange = Date.now();
}
for (const otherFly of flies) {
if (otherFly !== this) {
const otherDx = otherFly.x - this.x;
const otherDy = otherFly.y - this.y;
const otherDistanceSquared = otherDx * otherDx + otherDy * otherDy;
const thresholdSquared = 10 * 10;
if (otherDistanceSquared < thresholdSquared) {
const otherDistance = Math.sqrt(otherDistanceSquared);
this.x -= otherDx / otherDistance;
this.y -= otherDy / otherDistance;
}
}
}
}
draw() {
ctx.beginPath();
ctx.moveTo(this.x, this.y - this.size);
ctx.lineTo(this.x - this.size, this.y + this.size);
ctx.lineTo(this.x + this.size, this.y + this.size);
ctx.closePath();
ctx.fill();
}
}
const digits = [
[1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
];
const digitLookupTable = {
0: digits[0],
1: digits[1],
2: digits[2],
3: digits[3],
4: digits[4],
5: digits[5],
6: digits[6],
7: digits[7],
8: digits[8],
9: digits[9],
":": digits[10],
};
const flies = [];
let lastTime = "";
function createFlies() {
flies.length = 0;
for (let i = 0; i < 750; i++) {
flies.push(new Fly(Math.random() * canvas.width, Math.random() * canvas.height));
}
}
function updateTime() {
const now = new Date();
const hours = now.getHours().toString().padStart(2, "0");
const minutes = now.getMinutes().toString().padStart(2, "0");
const currentTime = `${hours}:${minutes}`;
if (currentTime !== lastTime) {
lastTime = currentTime;
// set random fly targets to disperse flies
setRandomFlyTargets();
// after 2 seconds fly to digits
setTimeout(() => {
setFlyTargets(currentTime);
}, 5000);
}
}
function setFlyTargets(time) {
let flyIndex = 0;
let startX = -80;
for (let i = 0; i < 5; i++) {
const digitPattern = digitLookupTable[time[i]];
startX += time[i] === ":" ? 130 : parseInt(time[i]) === 1 ? 140 : 160;
const startY = canvas.height / 2 - 100;
for (let y = 0; y < 5; y++) {
for (let x = 0; x < 4; x++) {
if (digitPattern[y * 4 + x]) {
for (let f = 0; f < 12; f++) {
if (flyIndex < flies.length) {
flies[flyIndex].targetX = startX + x * 30;
flies[flyIndex].targetY = startY + y * 40;
flyIndex++;
}
}
}
}
}
if (time[i] === ":") startX -= 50;
}
}
function setRandomFlyTargets() {
for (const fly of flies) {
fly.targetX = Math.random() * canvas.width;
fly.targetY = Math.random() * canvas.height;
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
updateTime();
for (const fly of flies) {
fly.update(flies);
fly.draw();
}
requestAnimationFrame(animate);
}
createFlies();
animate();
setFlyTargets(lastTime);
let flyerTimeout;
// on button down or touch start, set random fly targets
window.addEventListener("pointerdown", () => {
setRandomFlyTargets();
// Make sure to return to digits after 5 seconds
flyerTimeout = setTimeout(() => {
setFlyTargets(lastTime);
}, 5000);
});
// on button up or touch end, set fly targets
window.addEventListener("pointerup", () => {
clearTimeout(flyerTimeout);
setFlyTargets(lastTime);
});
</script>
</body>
</html>