-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
111 lines (103 loc) · 2.16 KB
/
script.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
(async function()
{
const paired_devices = await analogsense.getDevices();
if (paired_devices.length != 0)
{
window.dev = paired_devices[0];
initDevice();
}
window.onclick = window.onkeydown = async function(event)
{
if (!window.dev)
{
window.dev = await analogsense.requestDevice();
if (window.dev)
{
initDevice();
}
}
};
})();
function time()
{
return Date.now() / 1000;
}
let full_press_since = 0;
let max_frame = 6;
let zero_since = 0;
function initDevice()
{
document.querySelector("img").src = "images/laser.png";
window.dev.startListening(function(active_keys)
{
let value = 0;
for (const key of active_keys)
{
if (key.value > value)
{
value = key.value;
}
}
const frame = parseInt(value * max_frame);
if (frame == max_frame)
{
if (full_press_since == 0)
{
document.querySelector("img").src = "images/press" + frame + ".png";
full_press_since = time();
}
}
else
{
document.querySelector("img").src = "images/press" + frame + ".png";
full_press_since = 0;
}
if (value == 0)
{
max_frame = 6;
zero_since = time();
}
else
{
zero_since = 0;
}
});
}
window.setInterval(function()
{
if (full_press_since != 0)
{
if (max_frame != 8)
{
if ((time() - full_press_since) > 0.15)
{
max_frame++;
full_press_since = time();
document.querySelector("img").src = "images/press" + max_frame + ".png";
}
}
}
if (zero_since != 0)
{
if ((time() - zero_since) > 20.5)
{
document.querySelector("img").src = "images/neutral.png"; // 5 - Acceptable
}
else if ((time() - zero_since) > 15.5)
{
document.querySelector("img").src = "images/cry.png"; // 4 - Depression
}
else if ((time() - zero_since) > 10.5)
{
document.querySelector("img").src = "images/think.png"; // 3 - Bargaining
}
else if ((time() - zero_since) > 5.5)
{
document.querySelector("img").src = "images/knife.png"; // 2 - Anger
}
else if ((time() - zero_since) > 0.5)
{
document.querySelector("img").src = "images/sweating.png"; // 1 - Denial
}
}
}, 100);