-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.js
71 lines (62 loc) · 1.82 KB
/
animation.js
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
const content = document.querySelectorAll("body > :not(#lightspeed)");
for (const e of content) e.hidden = true;
document.body.style.margin = 0;
document.body.style.padding = 0;
document.body.style.overflow = 'hidden';
const ls = document.createElement('canvas');
ls.style.position = 'absolute';
document.body.appendChild(ls);
const sleep = ms => new Promise(r => setTimeout(r, ms));
const ctx = ls.getContext('2d');
const starRatio = 500;
const speed = 5;
const [w, h] = [window.innerWidth, window.innerHeight];
const [x, y, z] = [w / 2, h / 2, (w + h) / 8];
const starColorRatio = 1 / z;
const n = w * h / 300; // # of stars
const stars = Array.from({ length: n }, () => {
const actx = Math.random() * w - x;
const acty = Math.random() * h - y;
const z0 = Math.random() * z + 200;
return {
x: actx * z0 / starRatio,
y: acty * z0 / starRatio,
z0,
z1: z0 - 1,
get lineWidth() { return (1 - starColorRatio * this.z0) * 2; },
get x0() { return this.x / this.z0 * starRatio; },
get y0() { return this.y / this.z0 * starRatio; },
get x1() { return this.x / this.z1 * starRatio; },
get y1() { return this.y / this.z1 * starRatio; },
};
});
ls.width = w;
ls.height = h;
ctx.fillStyle = "black";
ctx.strokeStyle = 'white';
function draw() {
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
for (const star of stars) {
ctx.lineWidth = star.lw;
ctx.beginPath();
ctx.moveTo(x + star.x0, y + star.y0);
ctx.lineTo(x + star.x1, y + star.y1);
ctx.stroke();
ctx.closePath();
}
}
draw();
await sleep(400);
for (let i = 0; i < 50; i++) {
for (const star of stars) star.z1 -= 2.5;
draw();
await sleep(1);
}
await sleep(500);
for (let i = 0; i < 25; i++) {
for (const star of stars) star.z1 += 5;
draw();
await sleep(1);
}
ls.remove();
for (const e of content) e.hidden = false;