Hi everybody, I have this code from a tutorial. So basically I want to draw a grid of boxes using WEBGL.
I have a hard time to understand the logic of this translate( size/2+ i * size - size * cols/2, size/2+ j * size - size * rows/2);
Why would this help center the box grid to the center of the canvas ? And how can the tutorial come up with this logic?
Thank you!
let size = 50;
let rows;
let cols;
function setup() {
createCanvas(400, 400, WEBGL);
angleMode(DEGREES);
rows=3;
cols=3;
}
function draw() {
background(220);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
fill(255);
push();
translate( size/2+ i * size - size * cols/2, size/2+ j * size - size * rows/2);
box(size);
pop();
}
fill(255, 0, 0);
box(50);
}
}