-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
121 lines (115 loc) · 4.17 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="favicon.png" type="image/x-icon">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js"></script>
<title>xPad live test</title>
<style>
.stick {
width: 80px;
height: 80px;
}
.stick > div {
position: relative;
transition: all 100ms ease;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
width: 30px;
height: 30px;
display: grid;
place-items: center;
}
#buttons > div {
transition: all 100ms ease;
}
</style>
</head>
<body>
<div id="detection" class="alert text-center alert-danger m-0 p-1">
Xbox Controller not detected! (try to press some gamepad button)
</div>
<nav class="navbar navbar-light bg-light d-flex justify-content-center m-0 shadow-sm">
<span class="navbar-brand font-weight-bolder text-dark">
<img src="favicon.png" alt="" height="30px">Pad<span class="text-secondary">.</span><span class="text-success">js</span>
</span>
<span class="flex-grow-1"></span>
<div class="btn-group" role="group" aria-label="Basic example">
<a class="btn btn-outline-dark" href="https://CamilleAbella.github.io" role="button">Author</a>
<a class="btn btn-outline-dark" href="https://raw.githubusercontent.com/CamilleAbella/xPad/master/xPad.js" role="button">Get Lib</a>
<a class="btn btn-outline-dark" href="https://github.com/CamilleAbella/xPad" role="button">Docs</a>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-sm pt-4">
<h2>Buttons</h2>
<div id="buttons" class="d-flex flex-wrap"></div>
</div>
<div class="col-sm pt-4">
<h2>Axis</h2>
<div id="axis" class="d-flex flex-wrap">
<div class="m-1 stick LEFT rounded shadow-sm">
<div class="rounded shadow-sm bg-secondary">
<span></span>
</div>
</div>
<div class="m-1 stick RIGHT rounded shadow-sm">
<div class="rounded shadow-sm bg-secondary">
<span></span>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="xPad.js"></script>
<script>
const
detection = document.getElementById("detection"),
buttons = document.getElementById("buttons"),
axis = document.getElementById("axis")
xPad.on("connected", () => {
detection.className = "alert text-center alert-success m-0 p-1"
detection.innerText = "Xbox Controller detected :)"
})
xPad.on("disconnected", () => {
detection.className = "alert text-center alert-danger m-0 p-1"
detection.innerText = "Xbox Controller disconnected!"
})
xPad.on("buttonUpdate", (name, value) => {
const button = document.getElementById(name)
if(button){
if(value === 0){
button.className = "m-1 p-2 badge badge-dark"
}else if(value === 1){
button.className = "m-1 p-2 badge badge-success"
}else{
button.className = "m-1 p-2 badge badge-secondary"
}
}
})
xPad.on("joysticksUpdate", joysticks => {
for(const side in joysticks) {
const joystickViewer = document.querySelector(`.stick.${side.toUpperCase()} > div`)
for (const orientation in joysticks[side]) {
const percent = String(50 + 50 * joysticks[side][orientation]) + "%"
const prop = orientation === "x" ? "left" : "top"
joystickViewer.style[prop] = percent
}
}
})
xPad.buttonsConfig.forEach(name => {
const button = document.createElement("div")
button.className = "m-1 p-2 badge badge-dark"
button.innerText = name
button.id = name
buttons.appendChild(button)
})
</script>
</body>
</html>