xxxxxxxxxx
84
// tile class definition
class Tile {
constructor(x, y) {
this.x = x;
this.y = y;
// keeping track of original coordinates for resetting
this.xBeforeDisplacement = x;
this.yBeforeDisplacement = y;
// creating overlapping tiles
this.breadth = CELL_WIDTH + random(offset);
this.length = CELL_HEIGHT + random(offset);
// assigning random colors
this.r = random(255);
this.g = random(255);
this.b = random(255);
this.rotate_angle = random(-45,45); // keeps track of angle by which tile rotates
this.effect = false; // flag for if tile is within specified range of cursor
}
update() {
this.effect = false;
// checks if tiles are within a certain range of the cursor
if (dist(mouseX, mouseY, this.xBeforeDisplacement,
this.yBeforeDisplacement) < this.breadth * 4)
{
// assigns new coordinates to tiles relative to coordinates of the cursor
this.x = mouseX - this.xBeforeDisplacement;
this.y = mouseY - this.yBeforeDisplacement;
//
this.effect = true;
}
// resetting the tiles to their original positions
else {
if(this.x > this.xBeforeDisplacement) {
this.x -= resetSpeed;
}
if(this.x < this.xBeforeDisplacement) {
this.x += resetSpeed;
}
if(this.y > this.yBeforeDisplacement) {
this.y -= resetSpeed;
}
if(this.y < this.yBeforeDisplacement) {
this.y += resetSpeed;
}
}
}
// render function for the tiles
draw() {
this.update();
noStroke();
fill(this.r, this.g, this. b, ALPHA);
// decision structure to determine how tiles are rendered
if (this.effect) {
push();
// makes coordinates of the cursor reference for drawing to canvas
translate(mouseX, mouseY);
rotate(this.rotate_angle);
rect(this.x, this.y, this.breadth, this.length);
pop();
// setting coordinates of Tile to initial values with an offset
this.x = this.xBeforeDisplacement + random(-60, 60);
this.y = this.yBeforeDisplacement + random(-60, 60);
}
else {
rect(this.x, this.y, this.breadth, this.length);
}
}
}