-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
65 lines (53 loc) · 1.52 KB
/
sketch.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
let button = document.getElementById("darkmodeToggle");
let darkmode = JSON.parse(localStorage.getItem("darkmode")) || false;
let foregroundColor = "#202020";
let backgroundColor = "#F7F0F0";
let algorithms = new Map();
algorithms.set("marchingSquares", new MarchingSquares());
algorithms.set("boids", new Boids());
function toggleDarkmode(toggle) {
darkmode = toggle ? !darkmode : darkmode;
if (darkmode) {
button.classList.add("on");
document.body.classList.add("darkmode");
foregroundColor = "#F7F0F0";
backgroundColor = "#202020";
} else {
button.classList.remove("on");
document.body.classList.remove("darkmode");
foregroundColor = "#202020";
backgroundColor = "#F7F0F0";
}
algorithms.forEach((algorithm) => {
algorithm.setColors(backgroundColor, foregroundColor);
});
localStorage.setItem("darkmode", darkmode);
}
toggleDarkmode(false);
function setAlgorithm(name) {
if (algorithms.has(name)) {
currentAlgorithm = algorithms.get(name);
return true;
} else {
return false;
}
}
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
let selectedAlgorithm = round(random(algorithms.size - 1));
let cur = 0;
algorithms.forEach((algorithm) => {
algorithm
.setSize(width, height)
.setColors(backgroundColor, foregroundColor)
.init();
if (cur == selectedAlgorithm) currentAlgorithm = algorithm;
cur++;
});
}
function draw() {
currentAlgorithm.run();
}
button.addEventListener("click", () => {
toggleDarkmode(true);
});