-
Notifications
You must be signed in to change notification settings - Fork 0
/
404.mjs
130 lines (99 loc) · 2.59 KB
/
404.mjs
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
const sketch = (element) => ($) => {
const aspectRatio = 4 / 1;
const size = () => [element.offsetWidth, $.round(element.offsetWidth / aspectRatio)];
let rainDrops;
let ui;
$.setup = () => {
$.createCanvas(...size());
rainDrops = RainDrops();
ui = UI();
ui.pause();
};
$.windowResized = () => {
const [width, height] = size();
if (width !== $.width || height !== $.height) {
$.resizeCanvas(width, height);
rainDrops.reset();
}
};
$.draw = () => {
rainDrops.drawAndUpdate();
};
const UI = () => {
const playButton = $.select('[data-action="play"]', element);
const pauseButton = $.select('[data-action="pause"]', element);
const downloadButton = $.select('[data-action="download"]', element);
const play = () => {
playButton?.addClass("hidden");
pauseButton?.removeClass("hidden");
$.loop();
};
const pause = () => {
playButton?.removeClass("hidden");
pauseButton?.addClass("hidden");
$.noLoop();
};
const download = () => {
$.saveCanvas(element.id, "png");
};
playButton?.mouseClicked(play);
pauseButton?.mouseClicked(pause);
downloadButton?.mouseClicked(download);
return { play, pause, download };
};
const RainDrops = () => {
let drops = [];
const reset = () => {
drops.length = 0;
for (let i = 0, l = $.sqrt($.width * $.height) / 6; i < l; i++) {
drops.push(Drop());
}
$.textAlign($.CENTER, $.CENTER);
$.textStyle($.BOLD);
$.noFill();
};
const drawAndUpdate = () => {
$.clear();
drops.forEach((d) => {
d.drawAndUpdate();
});
};
reset();
return { reset, drawAndUpdate };
};
const Drop = () => {
const MIN_SIZE = 10;
const MAX_SIZE = 50;
const GROWTH = 0.25;
let x;
let y;
let size;
let maxSize;
const reset = () => {
x = $.random() * ($.width - MAX_SIZE) + MAX_SIZE / 2;
y = $.random() * ($.height - MAX_SIZE) + MAX_SIZE / 2;
size = ($.random() * MAX_SIZE) / 4 + MIN_SIZE;
maxSize = size * 2;
};
const drawAndUpdate = () => {
draw();
update();
};
const draw = () => {
const alpha = 64 - 64 * $.pow(size / maxSize, 6);
$.stroke(255, 255, 255, alpha);
$.textSize(size);
$.text("404", x, y);
};
const update = () => {
if (size < maxSize) {
size += GROWTH;
} else {
reset();
}
};
reset();
return { reset, drawAndUpdate };
};
};
export default (element) => new p5(sketch(element), element);