xxxxxxxxxx
43
class Logo {
constructor(size, speed) {
this.size = createVector(size, size);
this.pos = createVector(random(width - size), random(height - size));
this.vel = p5.Vector.fromAngle(PI * .25, speed);
this.newColor();
}
newColor() {
this.color = createVector(random(), random(), random()).setMag(255);
}
update() {
this.pos.add(this.vel);
const left = this.pos.x < 0;
const top = this.pos.y < 0;
const right = this.pos.x + this.size.x >= width;
const bottom = this.pos.y + this.size.y >= height;
const horz = left || right;
const vert = top || bottom;
const any = horz || vert;
if (horz) {
this.pos.x = (left ? 0 : (width - this.size.x) * 2) - this.pos.x;
this.vel.x *= -1;
}
if (vert) {
this.pos.y = (top ? 0 : (height - this.size.y) * 2) - this.pos.y;
this.vel.y *= -1;
}
if (any) this.newColor();
return this;
}
render() {
fill(this.color.array());
rect(this.pos.x, this.pos.y, this.size.x, this.size.y);
}
}