-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
179 lines (161 loc) · 4.46 KB
/
main.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
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
import { default as p5Class } from "p5";
import "./style.css";
const p5 = new p5Class(() => {});
const colorPalettes = [
["#0f0f0f"],
["#AD2E24"],
["#9BC995", "#5171A5"],
["#242038", "#F7ECE1", "#A167A5"],
["#0f0f0f", "#FEDB25"],
["#191923", "#0E79B2"],
["#E5E059", "#BDD358"],
["#54494B", "#91C7B1"],
];
let colorPaletteIndex = 0;
const regenerateBtn = document.getElementById("regenerate-button");
const saveBtn = document.getElementById("save-button");
const paletteBtn = document.getElementById("palette-button");
const r = 100;
const sw = 80;
const dimensions = 380;
let lineCoords = [];
let history = [];
let historyIndex = 0;
p5.setup = () => {
p5.createCanvas(dimensions, dimensions);
p5.strokeCap(p5.PROJECT);
p5.strokeWeight(sw);
generateShape();
regenerateBtn.addEventListener("click", () => {
generateShape();
});
saveBtn.addEventListener("click", () => {
savePNG();
});
paletteBtn.addEventListener("click", () => {
changeColorPalette();
});
};
const generateShape = () => {
lineCoords = [];
drawRandomShape();
if (history.length) {
history.length = historyIndex + 1;
}
history.push(lineCoords);
historyIndex = history.length - 1;
};
const drawRandomShape = () => {
const colors = colorPalettes[colorPaletteIndex];
p5.clear();
for (let y = sw / 2; y < dimensions; y += r) {
for (let x = sw / 2; x < dimensions; x += r) {
const xEdge = dimensions - r * 2 <= x;
const yEdge = dimensions - r * 2 <= y;
const d = [
[x, y - r, "⬆"],
[x + r, y, "➡"],
[x, y + r, "⬇"],
[x - r, y, "⬅"],
];
const directions = [];
if (x === r && y === r) {
directions.push(d[1]);
directions.push(d[2]);
} else if (x === r && y > r && !yEdge) {
directions.push(d[0]);
directions.push(d[1]);
directions.push(d[2]);
} else if (x > r && !xEdge && y === r) {
directions.push(d[1]);
directions.push(d[2]);
directions.push(d[3]);
} else if (xEdge && y === r) {
directions.push(d[2]);
directions.push(d[3]);
} else if (xEdge && y > r && !yEdge) {
directions.push(d[0]);
directions.push(d[2]);
directions.push(d[3]);
} else if (xEdge && yEdge) {
directions.push(d[0]);
directions.push(d[3]);
} else if (x > r && !xEdge && yEdge) {
directions.push(d[0]);
directions.push(d[1]);
directions.push(d[3]);
} else if (x === r && yEdge) {
directions.push(d[0]);
directions.push(d[1]);
} else {
directions.push(d[0]);
directions.push(d[1]);
directions.push(d[2]);
directions.push(d[3]);
}
const randDir = directions[Math.floor(Math.random() * directions.length)];
if (randDir[0] !== x || randDir[1] !== y) {
p5.stroke(colors[Math.floor(Math.random() * colors.length)]);
p5.line(x, y, randDir[0], randDir[1]);
lineCoords.push({
x1: x,
y1: y,
x2: randDir[0],
y2: randDir[1],
});
}
}
}
};
const drawShapeFromCoords = (linesArr) => {
const colors = colorPalettes[colorPaletteIndex];
p5.clear();
for (const { x1, y1, x2, y2 } of linesArr) {
p5.stroke(colors[Math.floor(Math.random() * colors.length)]);
p5.line(x1, y1, x2, y2);
}
};
const changeColorPalette = () => {
const colorsToChooseFrom = colorPalettes.length;
if (colorPaletteIndex === colorsToChooseFrom - 1) {
colorPaletteIndex = 0;
} else {
colorPaletteIndex++;
}
drawShapeFromCoords(history[historyIndex]);
};
const savePNG = () => {
const dateObj = new Date();
const month = dateObj.getUTCMonth() + 1;
const day = dateObj.getUTCDate();
const year = dateObj.getUTCFullYear();
const hours = dateObj.getHours();
const minutes = dateObj.getMinutes();
const seconds = dateObj.getSeconds();
const date = `${year}-${month}-${day}_${hours}:${minutes}:${seconds}`;
p5.save(`${date}.png`);
};
p5.keyPressed = () => {
if (history.length > 0) {
if (p5.keyCode === 37) {
undo();
}
if (p5.keyCode === 39) {
redo();
}
}
};
const undo = () => {
if (historyIndex > 0) {
historyIndex--;
const previousShape = history[historyIndex];
drawShapeFromCoords(previousShape);
}
};
const redo = () => {
if (historyIndex < history.length - 1) {
historyIndex++;
const previousShape = history[historyIndex];
drawShapeFromCoords(previousShape);
}
};