xxxxxxxxxx
194
let buf1;
let buf2;
let vspace;
let hspace;
let hslope;
let rh;
let rw;
let cols;
function drawPat(buf) {
buf.background(0, 0);
buf.strokeWeight(0.5);
let y = 0;
let row = 0;
let last_xlo = buf.width;
while (y <= buf.height - vspace) {
let cidx = 0;
let xhi;
let xlo;
let ny;
if (row == 0) {
xhi = buf.width;
xlo = buf.width;
ny = y + rh;
} else {
xhi = last_xlo - hspace;
xlo = xhi + vspace / hslope;
ny = y + vspace;
}
last_xlo = xlo;
while (min(xlo, xhi) > rw) {
buf.fill(cols[cidx]);
buf.stroke(cols[cidx]);
buf.beginShape();
buf.vertex(xhi, y);
buf.vertex(xhi - hspace, y);
buf.vertex(xlo - hspace, ny);
buf.vertex(xlo, ny);
buf.endShape(CLOSE);
cidx = (cidx + 1) % cols.length;
xlo -= hspace;
xhi -= hspace;
}
buf.beginShape();
buf.vertex(xhi, y);
buf.vertex(0, y);
buf.vertex(0, ny);
buf.vertex(xlo, ny);
buf.endShape(CLOSE);
++row;
y = ny;
}
}
function setup() {
createCanvas(400, 600);
makeCarpet();
}
function makeCarpet()
{
const w = random(300, 400);
const h = random(200, 300);
buf1 = createGraphics(w, h);
buf2 = createGraphics(w, h);
vspace = random(30, 50);
hspace = vspace * random(1.5, 2);
hslope = random(1.25, 1.75);
rh = random(5, 20);
rw = random(0, 10);
colorMode(HSB, 255);
cols = [];
for (let idx = 0; idx < 20; ++idx) {
const b = idx % 2 == 0 ? random(50, 150) : random(150, 250);
cols.push(color(random(255), random(200, 256), b));
}
drawPat(buf1);
cols = [];
for (let idx = 0; idx < 20; ++idx) {
const b = idx % 2 == 0 ? random(50, 150) : random(150, 250);
cols.push(color(random(255), random(200, 256), b));
}
drawPat(buf2);
}
function diamond(buf) {
push();
image(buf, 0, 0);
pop();
push();
scale(-1, 1);
image(buf, 0, 0);
pop();
push();
scale(1, -1);
image(buf, 0, 0);
pop();
push();
scale(-1, -1);
image(buf, 0, 0);
pop();
}
function draw() {
// background(random(255),200,50);
background(0, 0, 0);
const hgap = 0.75 * buf1.width;
const hsep = 2 * buf1.width + hgap;
const vgap = 0.75 * buf1.height;
const vsep = 2 * buf1.height + vgap;
const nx = int(random(2, 5));
const wx = (nx - 1) * hsep + 2 * buf1.width;
const ny = int(ceil((1.5 * wx + vgap) / (2 * buf1.height + vgap)));
const wy = (ny - 1) * vsep + 2 * buf1.height;
const sx = (width - 50) / wx;
const sy = (height - 50) / wy;
push();
translate(width / 2, height / 2);
scale(min(sx, sy));
fill(random(255), random(150, 250), 50);
rect(-wx / 2, -wy / 2, wx, wy);
for (let y = 0; y < ny; ++y) {
for (let x = 0; x < nx; ++x) {
const xx = -wx / 2 + x * hsep + buf1.width;
const yy = -wy / 2 + y * vsep + buf1.height;
push();
translate(xx, yy);
diamond(buf1);
pop();
}
}
for (let y = 0; y < ny - 1; ++y) {
for (let x = 0; x < nx - 1; ++x) {
const xx = -wx / 2 + (x + 0.5) * hsep + buf1.width;
const yy = -wy / 2 + (y + 0.5) * vsep + buf1.height;
push();
translate(xx, yy);
diamond(buf2);
pop();
}
}
fill(cols[0]);
noStroke();
rect(-wx / 2, -wy / 2, buf1.width, wy);
rect(wx / 2 - buf1.width, -wy / 2, buf1.width, wy);
rect(-wx / 2, -wy / 2, wx, buf1.height);
rect(-wx / 2, wy / 2 - buf1.height, wx, buf1.height);
noFill();
stroke(0, 0, 255);
strokeWeight(5);
rect(-wx / 2, -wy / 2, wx, wy);
pop();
noLoop();
}
function keyPressed()
{
if( key == ' ' ) {
makeCarpet();
loop();
} else if( key == 's' ) {
save( 'output.png' );
}
}