xxxxxxxxxx
107
let vertical = [];
let horizontal = [];
let num = 2;
function setup() {
createCanvas(400, 400);
background(20);
angleMode(DEGREES);
rectMode(CENTER);
}
function mouseDragged() {
for (let i = 0; i < num; i++) {
let vWidth = random(20, 150);
let vHeight = random(20, 150);
let verti = new RotatorBouncer(
random(vWidth / 2, width - vWidth / 2),
random(vHeight / 2, height - vHeight / 2),
vWidth,
vHeight,
random(-2, 2),
0,
//random(-2, -2),
int(random(0, 4))
);
vertical.push(verti);
let hWidth = random(20, 150);
let hHeight = random(20, 150);
let hori = new RotatorBouncer(
random(hWidth / 2, width - hWidth / 2),
random(hHeight / 2, height - hHeight / 2),
hWidth,
hHeight,
0,
random(-2, 2),
// random(-2, 2),
int(random(0, 4))
);
horizontal.push(hori);
}
}
function draw() {
// background(220);
for (let i = 0; i < vertical.length; i++) {
vertical[i].update();
horizontal[i].update();
}
}
class RotatorBouncer {
constructor(x, y, w, h, xSpeed, ySpeed, c) {
// constructor(x, y, w, h, xSpeed, ySpeed, rSpeed, c) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
// this.rSpeed = rSpeed;
this.c = c;
// this.a = 90;
}
update() {
push();
translate(this.x, this.y);
// rotate(this.a);
if (this.c == 0) {
fill(216, 40, 33);
} // red
if (this.c == 1) {
fill(11, 93, 151);
} // blue
if (this.c == 2) {
fill(242, 220, 122);
} // yellow
if (this.c == 3) {
fill(231);
} // white
strokeWeight(8);
rect(0, 0, this.w, this.h);
pop();
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
// this.a = this.a + this.rSpeed;
if (this.x > width - this.w / 2 ||
this.x < this.w / 2) {
this.xSpeed = -this.xSpeed;
}
if (this.y > height - this.h / 2 ||
this.y < this.h / 2) {
this.ySpeed = -this.ySpeed;
}
}
}