-
Notifications
You must be signed in to change notification settings - Fork 15
/
ui-helper.js
219 lines (171 loc) · 6.41 KB
/
ui-helper.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// element selectors
const sourceVideo = document.querySelector('video');
const drawCanvas = document.querySelector('canvas');
const loader = document.getElementById('loader');
// Stats panel
const userMessage = document.querySelector('#userMessage');
const showStats = document.querySelector('#statsDiv');
const touchesDisplay = document.querySelector('#touches');
const lastTouchDisplay = document.querySelector('#lastTouch');
const perHourDisplay = document.querySelector('#perHour');
const fpsDisplay = document.querySelector('#fps');
// controls
const showMaskToggle = document.querySelector('#showMask');
const showPointsToggle = document.querySelector('#showPoints');
const beepToggle = document.querySelector('#beepToggle');
const notifyToggle = document.querySelector('#notifyToggle');
const alertTimeoutEntry = document.querySelector('#alertTimeOut');
const resetButton = document.querySelector('button#reset');
// Model control buttons
const fastButton = document.querySelector('button#highSpeed');
const normalButton = document.querySelector('button#normalSpeed');
const slowerButton = document.querySelector('button#lowerSpeed');
const slowButton = document.querySelector('button#lowSpeed');
// Get video camera
function handleSuccess(stream) {
const video = document.querySelector('video');
console.log(`Using video device: ${stream.getVideoTracks()[0].label}`);
video.srcObject = stream;
}
function handleError(error) {
if (error.name === 'ConstraintNotSatisfiedError') {
console.error(`The resolution requested is not supported by your device.`);
} else if (error.name === 'PermissionDeniedError') {
console.error("User denied access to media devices");
}
console.error(`getUserMedia error: ${error.name}`, error);
}
document.querySelector('#start').addEventListener('click', () => {
document.querySelector('#content').hidden = true;
resetButton.style.display = "block";
// ToDo: learn proper CSS
loader.style.display = "block";
document.querySelector("div#usageNoteSide").innerHTML = document.querySelector('#usageNoteMain').innerHTML;
// Fix constraints to 640 px width; higher resolutions are more accurate but slower
navigator.mediaDevices.getUserMedia({video: {width: 640}, audio: false})
.then(handleSuccess)
.catch(handleError)
});
// Refresh page
resetButton.addEventListener('click', () => window.location.reload());
// Initialize the dashboard
function enableDashboard(initial=false) {
drawCanvas.style.display = "block";
userMessage.innerText = "Monitor running";
showStats.hidden = false;
loader.style.display = "none";
startTime = new Date().getTime();
if(initial){
fastButton.disabled = false;
normalButton.disabled = false;
slowerButton.disabled = false;
slowButton.disabled = false;
}
firstRun = false;
}
// Update stats
let touches = 0;
let lastFrameTime = new Date().getTime();
let lastTouchTime = false;
let lastTouchStatus = false;
let startTime;
function updateStats(touched) {
// FPS display
let now = new Date().getTime();
let fps = 1000 / (now - lastFrameTime);
lastFrameTime = now;
fpsDisplay.innerText = fps.toFixed(1);
// Touch counter
if (touched && lastTouchStatus === false)
touches++;
lastTouchStatus = touched;
touchesDisplay.innerText = touches;
// Last Touch timer
if (touched)
lastTouchTime = now;
if (lastTouchTime) {
lastTouchDisplay.innerHTML = ((now - lastTouchTime) / 1000).toFixed(0) + " sec ago";
// 1 / ((new Date().getTime() - startTime)/(60*1000*60))
perHourDisplay.innerHTML = (touches / ((now - startTime) / (60 * 60 * 1000))).toFixed(2);
} else {
lastTouchDisplay.innerHTML = "None yet";
}
}
// Beep tone
function beep(tone, duration) {
let audioCtx = new AudioContext;
let oscillator = audioCtx.createOscillator();
oscillator.frequency.value = tone;
oscillator.connect(audioCtx.destination);
oscillator.start();
oscillator.stop(audioCtx.currentTime + duration / 1000);
}
// Adjust BodyPix model settings
fastButton.addEventListener('click', () => {
fastButton.disabled = true;
normalButton.disabled = false;
slowerButton.disabled = false;
slowButton.disabled = false;
stopPrediction = true;
load(0.5, 16);
});
normalButton.addEventListener('click', () => {
fastButton.disabled = false;
normalButton.disabled = true;
slowerButton.disabled = false;
slowButton.disabled = false;
stopPrediction = true;
load(0.75, 16);
});
slowerButton.addEventListener('click', () => {
fastButton.disabled = false;
normalButton.disabled = false;
slowerButton.disabled = true;
slowButton.disabled = false;
stopPrediction = true;
load(.75, 8);
});
slowButton.addEventListener('click', () => {
fastButton.disabled = false;
normalButton.disabled = false;
slowerButton.disabled = false;
slowButton.disabled = true;
stopPrediction = true;
load(1, 8);
});
// Notification functions
function checkNotificationPermission() {
if (Notification.permission !== "granted")
Notification.requestPermission().then(permission => {
if (permission !== "granted") {
notifyToggle.checked = false;
notifyToggle.disabled = true;
} else {
notifyToggle.removeEventListener('click', checkNotificationPermission)
}
});
}
// Prompt the user to allow notifications the first time they click
notifyToggle.addEventListener('click', () => checkNotificationPermission());
// Browser notifications on touches
function notify(message) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("Sorry, browser notifications not supported");
notifyToggle.disabled = true;
}
// Check if notification permissions have been granted
else if (Notification.permission === "granted")
new Notification(message);
// Ask the user for permission first
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(permission => {
// If the user accepts, let's create a notification
if (permission === "granted")
new Notification(message);
else
notifyToggle.disabled = true;
});
} else
console.log("notifications rejected")
}