sketch = function( p ) {
let rows = 10, cols = 10;
let size = 80;
let grid = [];
let rotation, divisions;
p.setup = () => {
p.createCanvas(800, 800);
p.rectMode(p.CENTER);
rotation = p.createSlider(0, 90, 0, 1).position(10, p.height-50);
divisions = p.createSlider(4, 10, 10, 1).position(10, p.height-20);
makeGrid();
}
p.draw = () => {
p.background(0);
for (let c = 0; c < cols; c++) {
for (let r = 0; r < rows; r++) {
grid[c][r].display();
}
}
if(rows !== divisions.value()) {
makeGrid();
}
}
const makeGrid = () => {
rows = divisions.value();
cols = divisions.value();
grid = [];
for (let c = 0; c < cols; c++) {
grid.push([]);
for (let r = 0; r < rows; r++) {
let w = p.width/cols;
let h = p.height/rows;
let xOff = w/2;
let yOff = h/2;
let pos = p.createVector(c*w+xOff, r*h+yOff);
grid[c].push(new Cell(pos));
}
}
}
class Cell {
constructor(pos) {
this.x = pos.x;
this.y = pos.y;
}
display() {
p.push();
p.noFill();
p.stroke(255);
p.translate(this.x, this.y);
p.rotate(p.radians(rotation.value()));
p.rect(0, 0, size, size);
p.pop();
}
}
}