xxxxxxxxxx
52
// Sept. 13 2022
// In class exercise: working with class; breathing square
let rectangle = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 10; i++) {
rectangle[i] = new GrowingRect(i + 1);
}
}
function draw() {
background(0);
for (let i = 0; i < 10; i++) {
rectangle[i].grow();
rectangle[i].shrink();
rectangle[i].show();
}
}
class GrowingRect {
constructor(size) {
this.xPos = width / 2;
this.yPos = height / 2;
this.size = size;
this.changeSize = 1.25;
this.color = color(255, 255, 255);
}
grow() {
this.size += this.changeSize;
// print(this.size);
}
shrink() {
// when the rectangle fills the whole canvas, it shrinks back to 0
if (this.size > 400) {
this.size = -this.size;
}
}
show() {
rectMode(CENTER);
fill(this.color);
noStroke();
rect(this.xPos, this.yPos, this.size);
}
}