-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle.js
38 lines (34 loc) · 929 Bytes
/
rectangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Class Rectangle
class Rectangle {
// Constructor of the class
constructor({ x, y, w, h }) {
// Rectangle size
this.x = x;
this.y = y;
this.w = w;
this.h = h;
// Rectangle random color
this.colR = random(0, 255);
this.colG = random(0, 255);
this.colB = random(0, 255);
}
// Adding a method 'draw()' to the constructor
draw() {
// Set rectangles color and transparancy
fill(this.colR, this.colG, this.colB, 150);
rect(this.x, this.y, this.w, this.h);
}
// Adding a method 'delete(nb, recA)' to the constructor
// Delete the rectangle number 'nb' in the array 'recA'
delete(nb, recA) {
let x2 = this.x + this.w;
let y2 = this.y + this.h;
// Test if clicked in a rectangle
if (mouseX > this.x && mouseX < x2) {
if (mouseY > this.y && mouseY < y2) {
// Remove rectangle from array
recA.splice(nb, 1);
}
}
}
}