-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphic.js
110 lines (85 loc) · 3.24 KB
/
graphic.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let myCanvas = document.createElement("canvas");
//just for auto completion
myCanvas = document.getElementById("tableCan");
let ctx = myCanvas.getContext("2d")
//canvas setup
function generateSquareOutline (x, y, width, height, innerColor, outlineColor, thickness, context = ctx) {
context.save();
context.fillStyle = outlineColor;
context.fillRect(x, y, width, height);
context.fillStyle = innerColor;
context.fillRect(x + thickness, y + thickness, width - 2*thickness, height - 2*thickness);
context.restore()
}
// -- main model -----
let modelClone = Array(20).fill().map(() => (Array(10).fill(10)));
let oldPoints = points;
function updateGraphik() {
if (JSON.stringify(model) != JSON.stringify(modelClone)) {
model.forEach((element, y) => {
element.forEach((element, x) => {
if (model[y][x] != modelClone[y][x]) {
if (element > 0) {
generateSquareOutline(x * 32, y * 32, 32, 32, COLORS[element - 1], "black", 1)
}
else {
ctx.fillStyle = "#7f7f7f"
ctx.fillRect(x * 32, y * 32, 32, 32);
}
}
});
});
modelClone = JSON.parse(JSON.stringify(model));
}
// - update mainTable -
if (oldPoints != points) {
document.getElementById("pointsArea").innerHTML = points;
oldPoints = points;
}
//update Points
};
// -- Hold Canvas ----
let myCanvasHold = document.createElement("canvas");
myCanvasHold = document.getElementById("holdCan");
let holdCtx = myCanvasHold.getContext("2d");
holdCtx.fillStyle = "#7f7f7f";
holdCtx.fillRect(0,0,128,128)
//-- ------
function renderOnePeace(pieceLetter, context, posX, posY, size){
let coordinates =FORMS_DESIGN_STATIC[pieceLetter];
context.fillStyle = "#7f7f7f";
context.fillRect(posX, posY, size * 4, size * 4);
coordinates.forEach(element =>{
generateSquareOutline(element.x * size + posX, element.y * size + posY, size, size, COLORS[FORM_COLOR.indexOf(pieceLetter)], "black", 1, context);
});
};
//-- render Next Pieces -----
let nextCanvas = document.createElement("canvas");
nextCanvas = document.getElementById("nextCan");
let nextCtx = nextCanvas.getContext("2d");
nextCtx.fillStyle = "black"
for(let i = 1; i < 3; i++) nextCtx.fillRect(0,i*131-3, 128,3);
function renderNextPeaces(){
for(let i = 0; i < 3; i++){
let pieceLetter = following[i];
renderOnePeace(pieceLetter, nextCtx, 0, 131*i,32);
};
};
// -- future pos ----
let previewVisualisedTetrimo;
function previewVisualise (tetrimo) {
if (previewVisualisedTetrimo) {
previewVisualisedTetrimo.coordinates.forEach(element => {
if (model[element.y][element.x] == 0){
ctx.fillStyle = "#7f7f7f";
ctx.fillRect(element.x * 32, element.y * 32, 32, 32)
}
});
};
previewVisualisedTetrimo = tetrimo;
tetrimo.coordinates.forEach(element=>{
if (model[element.y][element.x] == 0) generateSquareOutline(element.x * 32, element.y * 32, 32, 32, "#7f7f7f", COLORS[tetrimo.color -1], 1);
});
}
// -- Intervall for main graphic
setInterval(updateGraphik, 1000 / 60)