-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIManager.js
173 lines (140 loc) · 4.65 KB
/
UIManager.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Stats from "stats.js";
import handLandmarker from "./landmarker";
import { landmarkStore } from "./LandmarkStore";
import { drawConnectors, drawLandmarks } from "@mediapipe/drawing_utils";
import { HAND_CONNECTIONS } from "@mediapipe/hands";
class UIManager {
constructor() {
this.$canvas = document.getElementById("game_canvas");
this.$scoreContainer = document.getElementById("score_container");
this.$scoreCount = document.getElementById("score_count");
this.$debugMode = document.getElementById("debugMode");
this.$startGameButton = document.getElementById("start_game_button");
this.$video = document.getElementById("webcam");
this.$videoOutputContainer = document.getElementById("output_container");
this.$videoCanvas = document.getElementById("output_canvas");
this.$infoBox = document.getElementById("info-box");
this.$infoBoxButton = this.$infoBox.children[0];
this.webcamRunning = false;
//configs
document.getElementById("debug_toggle_container").style.display = "none";
}
bindEnableCam(handler) {
this.$startGameButton.addEventListener("click", async (e) => {
const hasGetUserMedia = !!(
navigator.mediaDevices && navigator.mediaDevices.getUserMedia
);
const constraints = {
video: true,
};
if (hasGetUserMedia) {
if (!!handLandmarker === false) {
console.warn("wait, hand detection model not ready");
}
if (this.webcamRunning === true) {
this.webcamRunning = false;
} else {
this.webcamRunning = true;
}
const handleStream = (stream) => {
this.$video.srcObject = stream;
this.$video.addEventListener(
"loadeddata",
this.predictWebcam.bind(this)
);
};
navigator.mediaDevices
.getUserMedia(constraints)
.then((stream) => handleStream(stream))
.catch((e) => {
console.error(e.message);
});
handler();
}
});
}
async predictWebcam() {
// Now let's start detecting the stream.
const nowInMs = Date.now();
const results = handLandmarker.detectForVideo(this.$video, nowInMs);
const ctx = this.$videoCanvas.getContext("2d");
ctx.save();
ctx.clearRect(0, 0, this.$videoCanvas.width, this.$videoCanvas.height);
if (results.landmarks) {
for (const landmarks of results.landmarks) {
landmarkStore.landmarks = landmarks;
//TODO: Find out why these dont work in PROD
const drawingUtils = !!(drawConnectors && drawLandmarks)
if(drawingUtils){
drawConnectors(ctx, landmarks, HAND_CONNECTIONS, {
color: "#00FF00",
lineWidth: 2,
});
drawLandmarks(ctx, landmarks, { color: "#FF0000", lineWidth: 2 });
}
}
}
ctx.restore();
// Call this function again to keep predicting when the browser is ready.
if (this.webcamRunning === true) {
window.requestAnimationFrame(this.predictWebcam.bind(this));
}
}
bindToggleDebugMode(handler) {
this.$debugMode.addEventListener("click", (e) => {
handler(e);
});
}
/**
* @param {number} count
*/
set score(count) {
const countStr = count.toString();
this.$scoreCount.innerText = countStr;
}
init() {
this.$scoreContainer.style.display = "flex";
this.$videoOutputContainer.style.display = "none";
this.$startGameButton.remove();
this.$startGameButton = null;
document.getElementById("debug_toggle_container").style.display = "block";
this._bindAccordion();
}
handleToggleDebugMode(debugMode) {
if (debugMode === true) {
this.stats = new Stats();
this.stats.showPanel(0);
document.body.appendChild(this.stats.dom);
this.$videoOutputContainer.style.display = "block";
} else {
this.$videoOutputContainer.style.display = "none";
document.body.removeChild(this.stats.dom);
this.stats = null;
}
}
_bindAccordion() {
document
.getElementById("info-box")
.children[0].addEventListener("click", (e) => {
e.target.classList.toggle('open')
const panel = e.target.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
statsBegin() {
this.stats.begin();
}
statsEnd() {
return this.stats.end();
}
}
export const ui = new UIManager();