Hi. In this code I have a rotating rectangle in the center of the canvas using the applyMatrix() function.
In the reference it’s said that I can combine these functions into one.
This is a powerful operation that can perform the equivalent of translate, scale, shear and rotate all at once.
I’ve read the provided links, but I can’t figure out how to do this. Summing the matrixes gives a different ‘scaling’ outcome.
Thanks in advance.
let a = 1 + cos_a;
let b = sin_a;
let c = -sin_a;
let d = 1 + cos_a;
let e = width / 2;
let f = height / 2;
// applyMatrix(1, 0, 0, 1, width / 2, height / 2);
// rotate(angle);
// applyMatrix(cos_a, sin_a, -sin_a, cos_a, 0, 0);
applyMatrix(a, b, c, d, e, f);
function setup() {
let c = createCanvas(250, 250);
rectMode(CENTER);
}
function draw() {
let step = frameCount % 200;
let angle = map(step, 0, 200, 0, TWO_PI);
let cos_a = cos(angle);
let sin_a = sin(angle);
background(200);
// translate(width / 2, height / 2);
applyMatrix(1, 0, 0, 1, width / 2, height / 2);
// rotate(angle);
applyMatrix(cos_a, sin_a, -sin_a, cos_a, 0, 0);
rect(0, 0, 50, 50);
}