xxxxxxxxxx
42
class Block {
constructor(c1, c2, c3, x, y, h) {
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
this.x = x;
this.y = y;
this.h = h;
this.d = 10;
}
display() {
this.setBlockColor(0, this.x, this.y, this.h);
this.setBlockColor(this.x, width-this.x, this.y, this.h);
}
move() {
if (this.x > width || this.x < 0) {
this.d *= -1;
}
this.x += this.d;
}
setBlockColor(min, range, y, h) {
let x = 0.5;
this.setGradient(min, min + x*range, this.c1, this.c2, y, h);
this.setGradient(min + x*range, min + range, this.c2, this.c3, y, h);
}
setGradient(x, w, c1, c2, y, h) {
for (let i = x; i <= w; i++) {
let amt = map(i, x, w, 0, 1);
let c = lerpColor(c1, c2, amt);
stroke(c);
line(i, y, i, y + h);
}
}
}