-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.js
63 lines (56 loc) · 1.34 KB
/
cell.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Cell {
constructor(x, scale, hex) {
this.x = x;
this.scale = scale;
this.hex = hex;
}
static render(x, y, scale, hex) {
push();
let coords = createVector(x * scale, y * scale);
let col = color(hex);
let dist = scale * 0.06;
noStroke();
// upper triangle
fill(brighten(hex, 40));
beginShape();
vertex(coords.x, coords.y);
vertex(coords.x + scale, coords.y);
vertex(coords.x, coords.y + scale);
endShape();
// lower triangle
fill(darken(hex, 40));
beginShape();
vertex(coords.x + scale, coords.y);
vertex(coords.x + scale, coords.y + scale);
vertex(coords.x, coords.y + scale);
endShape();
// center rectangle
fill(col);
rect(coords.x + dist, coords.y + dist, scale - dist * 2);
pop();
}
render(y) {
Cell.render(this.x, y, this.scale, this.hex);
}
}
function darken(hex, percent) {
let result = color(hex);
result.levels[0] -= result.levels[0] * (percent / 100);
result.levels[1] -= result.levels[1] * (percent / 100);
result.levels[2] -= result.levels[2] * (percent / 100);
return result;
}
function brighten(hex, percent) {
return darken(hex, -percent);
}
function spreadCells(count) {
for (let i = 0; i < count; i++) {
let col = darken(random(colors), 60);
Cell.render(
ceil(random(0, width / tetris.scale)),
ceil(random(0, height / tetris.scale)),
tetris.scale,
col
);
}
}