-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5MondrainTapetki.js
109 lines (92 loc) · 2.36 KB
/
5MondrainTapetki.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
import { default as p5Class } from "p5";
import colors from "nice-color-palettes";
import "./style.css";
const p5 = new p5Class(() => {});
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
const blockSize = canvasWidth;
const step = (canvasWidth / 8.81) * 0.4;
const colorPaletteRandomIndex = Math.floor(Math.random() * colors.length);
const colorPalette = colors[colorPaletteRandomIndex];
const squares = [
{
x: 0,
y: 0,
width: blockSize,
height: blockSize,
},
];
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,
};
const squareB = {
x: splitAt,
y: square.y,
width: square.width - splitAt + square.x,
height: square.height,
};
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),
};
const squareB = {
x: square.x,
y: splitAt,
width: square.width,
height: square.height - splitAt + square.y,
};
squares.push(squareA);
squares.push(squareB);
}
p5.setup = () => {
p5.createCanvas(canvasWidth, canvasHeight);
p5.background("#fff");
};
p5.draw = () => {
p5.noLoop();
for (let i = 0; i < blockSize; i += step) {
splitSquaresWith({ x: i });
splitSquaresWith({ y: i });
}
for (const sq of squares) {
const color = colorPalette[Math.floor(Math.random() * colorPalette.length)];
p5.strokeCap(p5.SQUARE);
p5.stroke(color);
p5.fill(color);
const data = [sq.x, sq.y, sq.width, sq.height];
p5.rect(...data);
console.log(data);
}
};
p5.keyTyped = () => {
if (p5.key === "s" || p5.key === "S") {
p5.saveCanvas(`palette-${colorPaletteRandomIndex}-${new Date().toISOString()}`, "png");
}
};