xxxxxxxxxx
// By Roni Kaufman
// https://ronikaufman.github.io
// https://twitter.com/KaufmanRoni
// noprotect
let randInt = (a, b) => (floor(random(a, b)));
function setup() {
createCanvas(500, 500, WEBGL);
noLoop();
noStroke();
}
function draw() {
translate(-width/2, -height/2);
let margin = 50;
let baseRect = {
x: margin,
y: margin,
w: width - 2*margin,
h: height - 2*margin
};
let f1, f2;
if (random() < 1/2) {
f1 = divideV;
f2 = divideH;
} else {
f1 = divideH;
f2 = divideV;
}
let colors = shuffle(["#021d34", "#228fca", "#dcedf0"]);
background(colors[0]);
fill(colors[1]);
let density = 25;
noiseRect(0, 0, width, margin, density);
noiseRect(0, margin, margin, height-margin, density);
noiseRect(0, height-margin, width, margin, density);
noiseRect(width-margin, margin, margin, height-margin, density);
stroke(colors[2]);
let sw = 4;
strokeWeight(sw);
line(margin-sw/2, margin, width-margin+sw/2, margin);
line(width-margin, margin, width-margin, height-margin);
line(width-margin+sw/2, height-margin, margin-sw/2, height-margin);
line(margin, height-margin, margin, margin);
noStroke();
let rectas1 = f1(baseRect, randInt(2, 7));
for (let r1 of rectas1) {
let rectas2 = f2(r1, randInt(1, 6));
for (let r2 of rectas2) {
shuffle(colors, true);
fill(colors[0]);
rect(r2.x, r2.y, r2.w, r2.h);
fill(colors[1]);
noiseRect(r2.x, r2.y, r2.w, r2.h, random([50, 100, 200]));
fill(colors[2]);
noiseRect(r2.x, r2.y, r2.w, r2.h, random([50, 100, 200]));
}
}
}
// divide vertically recta into n equal parts
function divideV(recta, n) {
let newW = recta.w/n;
let result = [];
for (let z = 0; z < recta.w; z += newW) {
result.push({
x: recta.x + z,
y: recta.y,
w: newW,
h: recta.h
});
}
return result;
}
// divide horizontally recta into n equal parts
function divideH(recta, n) {
let newH = recta.h/n;
let result = [];
for (let z = 0; z < recta.h; z += newH) {
result.push({
x: recta.x,
y: recta.y + z,
w: recta.w,
h: newH
});
}
return result;
}
function noiseRect(x, y, w, h, density) {
let res = 1/500;
let s = 0.5;
for (let xi = x; xi < x + w; xi += s) {
for (let yi = y; yi < y + h; yi += s) {
let noice = noise(xi * res, yi * res);
let digit = floor(noice * density);
if (digit % 2 == 0) {
square(xi, yi, s);
}
}
}
}