-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
118 lines (100 loc) · 2.36 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
import { default as p5Class } from "p5";
import "./style.css";
const p5 = new p5Class(() => {});
const canvasWidth = 1000;
const canvasHeight = 1000;
const blockSize = canvasWidth;
const step = canvasHeight / 30;
const bgColor = "#CCDADD";
const strokeColor = "#202126";
const colors = ["#FE1A00", "#004FB4", "#F6B71E"];
const squares = [
{
x: 0,
y: 0,
width: blockSize,
height: blockSize,
color: bgColor,
},
];
function splitSquaresWith({ x, y }) {
for (let i = squares.length - 1; i >= 0; i--) {
const square = squares[i];
if (x && x > square.x && x < square.x + square.width) {
if (p5.random() > 0.5) {
squares.splice(i, 1);
splitOnX(square, x);
}
}
if (y && y > square.y && y < square.y + square.height) {
if (p5.random() > 0.5) {
squares.splice(i, 1);
splitOnY(square, y);
}
}
}
}
function splitOnX(square, splitAt) {
const squareA = {
x: square.x,
y: square.y,
width: square.width - (square.width - splitAt + square.x),
height: square.height,
color: bgColor,
};
const squareB = {
x: splitAt,
y: square.y,
width: square.width - splitAt + square.x,
height: square.height,
color: bgColor,
};
squares.push(squareA);
squares.push(squareB);
}
function splitOnY(square, splitAt) {
const squareA = {
x: square.x,
y: square.y,
width: square.width,
height: square.height - (square.height - splitAt + square.y),
color: bgColor,
};
const squareB = {
x: square.x,
y: splitAt,
width: square.width,
height: square.height - splitAt + square.y,
color: bgColor,
};
squares.push(squareA);
squares.push(squareB);
}
p5.setup = () => {
p5.createCanvas(canvasWidth, canvasHeight);
p5.background(bgColor);
};
p5.draw = () => {
p5.noLoop();
p5.noFill();
for (let i = 0; i < blockSize; i += step) {
splitSquaresWith({ x: i });
splitSquaresWith({ y: i });
}
for (const c of colors) {
const sq = squares[Math.floor(Math.random() * squares.length)];
sq.color = c;
}
for (const sq of squares) {
p5.strokeCap(p5.SQUARE);
p5.strokeWeight(25);
p5.stroke(strokeColor);
p5.fill(sq.color);
p5.rect(sq.x, sq.y, sq.width, sq.height);
}
};
p5.keyTyped = () => {
if (p5.key === "s" || p5.key === "S") {
p5.saveCanvas(`${new Date().toISOString()}`, "png");
}
};