xxxxxxxxxx
76
let timer;
let tw = 240;
let th = 240;
const colors = [
// [0, 0, 204],
// [0, 0, 128],
// [0, 0, 0],
[255, 255, 255],
[250, 250, 250],
[245, 245, 245],
];
function setup() {
createCanvas(windowWidth, windowHeight);
noLoop();
}
function draw() {
tw = windowWidth / 3;
th = windowWidth / 3;
noStroke();
for (var x = 0; x < width; x+=tw) {
for (var y = 0; y < height; y+=th) {
/* logic for two triangles per box */
let rand = random();
drawTriangles(x, y, rand);
}
}
}
//for splitting squares into two trianges
function drawTriangles(x, y, dir){
//dir > .5 draws line ul to lr
//dir < .5 draws line lr to ur
//define triangle points
let ulx = x;
let uly = y;
let llx = x;
let lly = y + th;
let urx = x + tw;
let ury = y;
let lrx = x + tw;
let lry = y + th;
//draw triangles by dir
if (dir > .5) {
fill(getColor());
triangle(ulx, uly, llx, lly, urx, ury);
fill(getColor());
triangle(llx, lly, urx, ury, lrx, lry);
} else if (dir <= .5) {
fill(getColor());
triangle(ulx, uly, llx, lly, lrx, lry);
fill(getColor());
triangle(ulx, uly, urx, ury, lrx, lry);
}
}
function getColor() {
let col;
col = colors[parseInt(random(colors.length))];
stroke(col);
return col;
}
function setResize() {
resizeCanvas(windowWidth, windowHeight);
draw();
}
function windowResized() {
clearTimeout(timer);
timer = setTimeout(setResize, 100);
}