-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightboard.js
85 lines (74 loc) · 2.55 KB
/
Lightboard.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
class Lightboard {
constructor(ctx, config = {}) {
this.ctx = ctx;
this.config = {
numRows: 3,
numCols: 9,
lightRadius: 14,
lightGap: 10,
lightColor: "#333",
startX: 0,
startY: 0,
keys: null,
...config,
};
this.activeLight = null; // Property to track the active light
}
draw() {
for (let row = 0; row < this.config.numRows; row++) {
let cols = row === 1 ? this.config.numCols - 1 : this.config.numCols;
for (let col = 0; col < cols; col++) {
let pad =
row === 1 ? this.config.lightRadius + this.config.lightRadius / 2 : 0;
let x =
this.config.startX +
pad +
col * (2 * this.config.lightRadius + this.config.lightGap);
let y =
this.config.startY +
row * (2 * this.config.lightRadius + this.config.lightGap);
// Determine if this light is the active one
const idx =
row < 2
? row * this.config.numCols + col
: this.config.numCols * 2 - 1 + col;
//console.log("INSIDE LIGhtbOARD", idx, " INDEX", row, "row");
let isActive = idx === this.activeLight;
this.drawLight(x, y, row, col, isActive);
}
}
}
clearHighlights() {
this.activeLight = null; // Reset the active light
this.draw(); // Redraw the lightboard without any highlights
}
drawLight(x, y, row, col, isActive) {
this.ctx.beginPath();
this.ctx.arc(x, y, this.config.lightRadius, 0, 2 * Math.PI);
// Set the fill style for the light
this.ctx.fillStyle = isActive ? "#FFFF00" : this.config.lightColor; // Yellow for active, for example
this.ctx.fill();
// Draw the letter on the light
this.drawLetter(x, y, row, col, isActive);
}
highlightLight(letter) {
// Find the index of the letter in the lightboard configuration
const upperLetter = letter.toUpperCase();
this.activeLight = this.config.keys.indexOf(upperLetter);
if (this.activeLight !== -1) {
this.draw(); // Redraw the lightboard with the highlighted light
}
}
drawLetter(x, y, row, col, isActive) {
// Set the text color based on whether the lamp is active
this.ctx.fillStyle = isActive ? "#000000" : "#ffffff"; // Example: Red for active, black for inactive
// Existing text drawing code
this.ctx.font = "12px serif";
const idx =
row < 2
? row * this.config.numCols + col
: row * this.config.numCols + col - 1;
const letter = this.config.keys[idx];
this.ctx.fillText(letter, x - 4, y + 4);
}
}