-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoypad.js
More file actions
55 lines (48 loc) · 1.24 KB
/
Copy pathjoypad.js
File metadata and controls
55 lines (48 loc) · 1.24 KB
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
class JoyPad {
constructor(cbs) {
this.calls = cbs;
this.blobsContainer = document.querySelectorAll(".blobs");
this.blobs = document.querySelectorAll(".blob");
window.addEventListener("gamepadconnected", (event) => {
this.stateCheck();
});
}
stateCheck() {
window.requestAnimationFrame(() => {
this.gamepads = navigator.getGamepads();
this.gamepad = this.gamepads[0];
this.padStatus();
this.stateCheck();
});
}
padStatus() {
this.gamepad.buttons.forEach((button, index) => {
if (this.blobs[index]) {
this.blobs[index].classList.remove("pressed");
if (button.pressed) {
this.blobs[index].classList.add("pressed");
console.log("button ID " + index);
this.calls.forEach((call) => {
if (call.index == index && !call.isDone) {
call.fn();
call.isDone = true;
}
});
}
}
});
const x = this.gamepad.axes[0];
const y = this.gamepad.axes[1];
// this.blobsContainer[0].style.transform = `translate(${x}em, ${y}em)`;
}
}
let cbs = [
{
index: 0,
fn: function () {
console.log("it works!");
},
isDone: false,
},
];
new JoyPad(cbs);