update
This commit is contained in:
parent
4bb1df140b
commit
23ffb6ea2a
17
.editorconfig
Normal file
17
.editorconfig
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# editorconfig.org
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = space
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[**.html]
|
||||||
|
indent_style = tab
|
||||||
|
tab_width = 4
|
||||||
|
|
||||||
|
[**.md]
|
||||||
|
trim_trailing_whitespace = false
|
27
README.md
27
README.md
@ -1,28 +1 @@
|
|||||||
# HTML5 Gamepad Test
|
# HTML5 Gamepad Test
|
||||||
|
|
||||||
Mostly because I didn't trust others ;)
|
|
||||||
|
|
||||||
https://greggman.github.io/html5-gamepad-test/
|
|
||||||
|
|
||||||
## Licence: MIT
|
|
||||||
|
|
||||||
Copyright 2019 Gregg Tavares
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
copy of this software and associated documentation files (the "Software"),
|
|
||||||
to deal in the Software without restriction, including without limitation
|
|
||||||
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
Software is furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
|
45
app.js
Normal file
45
app.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// app.js
|
||||||
|
const http = require("http");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
const url = require("url");
|
||||||
|
|
||||||
|
const port = 8080;
|
||||||
|
const host = "0.0.0.0";
|
||||||
|
const mimeTypes = {
|
||||||
|
".html": "text/html",
|
||||||
|
".css": "text/css",
|
||||||
|
".js": "application/javascript",
|
||||||
|
".svg": "image/svg+xml",
|
||||||
|
".png": "image/png",
|
||||||
|
".jpg": "image/jpeg",
|
||||||
|
".jpeg": "image/jpeg",
|
||||||
|
".gif": "image/gif",
|
||||||
|
".ico": "image/x-icon",
|
||||||
|
".json": "application/json",
|
||||||
|
".txt": "text/plain",
|
||||||
|
};
|
||||||
|
|
||||||
|
http
|
||||||
|
.createServer((req, res) => {
|
||||||
|
// Parse the URL and extract just the pathname, ignoring query parameters
|
||||||
|
const parsedUrl = url.parse(req.url);
|
||||||
|
let filePath = "." + decodeURIComponent(parsedUrl.pathname);
|
||||||
|
if (filePath === "./") filePath = "./index.html";
|
||||||
|
|
||||||
|
const ext = path.extname(filePath).toLowerCase();
|
||||||
|
const contentType = mimeTypes[ext] || "application/octet-stream";
|
||||||
|
|
||||||
|
fs.readFile(filePath, (err, content) => {
|
||||||
|
if (err) {
|
||||||
|
res.writeHead(404);
|
||||||
|
res.end("404 Not Found");
|
||||||
|
} else {
|
||||||
|
res.writeHead(200, { "Content-Type": contentType });
|
||||||
|
res.end(content);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.listen(port, host, () => {
|
||||||
|
console.log(`Server running at http://${host}:${port}/`);
|
||||||
|
});
|
68
index.html
68
index.html
@ -70,7 +70,7 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.svg text {
|
.svg text {
|
||||||
color: #CCC;
|
color: #ccc;
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,8 @@ body {
|
|||||||
.buttons svg text {
|
.buttons svg text {
|
||||||
font-size: 1.2px;
|
font-size: 1.2px;
|
||||||
}
|
}
|
||||||
.axes>div, .buttons>div {
|
.axes > div,
|
||||||
|
.buttons > div {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
background: #222;
|
background: #222;
|
||||||
}
|
}
|
||||||
@ -92,16 +93,14 @@ body {
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<h1>HTML5 Gamepad Test</h1>
|
<h1>HTML5 Gamepad Test</h1>
|
||||||
<div>running: <span id="running"></span></div>
|
<div>running: <span id="running"></span></div>
|
||||||
<div id="gamepads"></div>
|
<div id="gamepads"></div>
|
||||||
</body>
|
</body>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
const fudgeFactor = 2; // because of bug in Chrome related to svg text alignment font sizes can not be < 1
|
const fudgeFactor = 2; // because of bug in Chrome related to svg text alignment font sizes can not be < 1
|
||||||
const runningElem = document.querySelector('#running');
|
const runningElem = document.querySelector("#running");
|
||||||
const gamepadsElem = document.querySelector('#gamepads');
|
const gamepadsElem = document.querySelector("#gamepads");
|
||||||
const gamepadsByIndex = {};
|
const gamepadsByIndex = {};
|
||||||
|
|
||||||
const controllerTemplate = `
|
const controllerTemplate = `
|
||||||
@ -122,7 +121,7 @@ const axisTemplate = `
|
|||||||
<circle cx="0" cy="0" r="0.22" fill="red" class="axis" />
|
<circle cx="0" cy="0" r="0.22" fill="red" class="axis" />
|
||||||
<text text-anchor="middle" fill="#CCC" x="0" y="2">0</text>
|
<text text-anchor="middle" fill="#CCC" x="0" y="2">0</text>
|
||||||
</svg>
|
</svg>
|
||||||
`
|
`;
|
||||||
|
|
||||||
const buttonTemplate = `
|
const buttonTemplate = `
|
||||||
<svg viewBox="-2.2 -2.2 4.4 4.4" width="64" height="64">
|
<svg viewBox="-2.2 -2.2 4.4 4.4" width="64" height="64">
|
||||||
@ -134,33 +133,33 @@ const buttonTemplate = `
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
function addGamepad(gamepad) {
|
function addGamepad(gamepad) {
|
||||||
console.log('add:', gamepad.index);
|
console.log("add:", gamepad.index);
|
||||||
const elem = document.createElement('div');
|
const elem = document.createElement("div");
|
||||||
elem.innerHTML = controllerTemplate;
|
elem.innerHTML = controllerTemplate;
|
||||||
|
|
||||||
const axesElem = elem.querySelector('.axes');
|
const axesElem = elem.querySelector(".axes");
|
||||||
const buttonsElem = elem.querySelector('.buttons');
|
const buttonsElem = elem.querySelector(".buttons");
|
||||||
|
|
||||||
const axes = [];
|
const axes = [];
|
||||||
for (let ndx = 0; ndx < gamepad.axes.length; ndx += 2) {
|
for (let ndx = 0; ndx < gamepad.axes.length; ndx += 2) {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement("div");
|
||||||
div.innerHTML = axisTemplate;
|
div.innerHTML = axisTemplate;
|
||||||
axesElem.appendChild(div);
|
axesElem.appendChild(div);
|
||||||
axes.push({
|
axes.push({
|
||||||
axis: div.querySelector('.axis'),
|
axis: div.querySelector(".axis"),
|
||||||
value: div.querySelector('text'),
|
value: div.querySelector("text"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const buttons = [];
|
const buttons = [];
|
||||||
for (let ndx = 0; ndx < gamepad.buttons.length; ++ndx) {
|
for (let ndx = 0; ndx < gamepad.buttons.length; ++ndx) {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement("div");
|
||||||
div.innerHTML = buttonTemplate;
|
div.innerHTML = buttonTemplate;
|
||||||
buttonsElem.appendChild(div);
|
buttonsElem.appendChild(div);
|
||||||
div.querySelector('.index').textContent = ndx;
|
div.querySelector(".index").textContent = ndx;
|
||||||
buttons.push({
|
buttons.push({
|
||||||
circle: div.querySelector('.button'),
|
circle: div.querySelector(".button"),
|
||||||
value: div.querySelector('.value'),
|
value: div.querySelector(".value"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,10 +168,10 @@ function addGamepad(gamepad) {
|
|||||||
elem,
|
elem,
|
||||||
axes,
|
axes,
|
||||||
buttons,
|
buttons,
|
||||||
index: elem.querySelector('.index'),
|
index: elem.querySelector(".index"),
|
||||||
id: elem.querySelector('.id'),
|
id: elem.querySelector(".id"),
|
||||||
mapping: elem.querySelector('.mapping'),
|
mapping: elem.querySelector(".mapping"),
|
||||||
connected: elem.querySelector('.connected'),
|
connected: elem.querySelector(".connected"),
|
||||||
};
|
};
|
||||||
gamepadsElem.appendChild(elem);
|
gamepadsElem.appendChild(elem);
|
||||||
}
|
}
|
||||||
@ -199,22 +198,22 @@ function addGamepadIfNew(gamepad) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleConnect(e) {
|
function handleConnect(e) {
|
||||||
console.log('connect');
|
console.log("connect");
|
||||||
addGamepadIfNew(e.gamepad);
|
addGamepadIfNew(e.gamepad);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleDisconnect(e) {
|
function handleDisconnect(e) {
|
||||||
console.log('disconnect');
|
console.log("disconnect");
|
||||||
removeGamepad(e.gamepad);
|
removeGamepad(e.gamepad);
|
||||||
}
|
}
|
||||||
|
|
||||||
const t = String.fromCharCode(0x26AA);
|
const t = String.fromCharCode(0x26aa);
|
||||||
const f = String.fromCharCode(0x26AB);
|
const f = String.fromCharCode(0x26ab);
|
||||||
function onOff(v) {
|
function onOff(v) {
|
||||||
return v ? t : f;
|
return v ? t : f;
|
||||||
}
|
}
|
||||||
|
|
||||||
const keys = ['index', 'id', 'connected', 'mapping', /*'timestamp'*/];
|
const keys = ["index", "id", "connected", "mapping" /*'timestamp'*/];
|
||||||
function processController(info) {
|
function processController(info) {
|
||||||
const { elem, gamepad, axes, buttons } = info;
|
const { elem, gamepad, axes, buttons } = info;
|
||||||
const lines = [`gamepad : ${gamepad.index}`];
|
const lines = [`gamepad : ${gamepad.index}`];
|
||||||
@ -223,14 +222,14 @@ function processController(info) {
|
|||||||
}
|
}
|
||||||
axes.forEach(({ axis, value }, ndx) => {
|
axes.forEach(({ axis, value }, ndx) => {
|
||||||
const off = ndx * 2;
|
const off = ndx * 2;
|
||||||
axis.setAttributeNS(null, 'cx', gamepad.axes[off ] * fudgeFactor);
|
axis.setAttributeNS(null, "cx", gamepad.axes[off] * fudgeFactor);
|
||||||
axis.setAttributeNS(null, 'cy', gamepad.axes[off + 1] * fudgeFactor);
|
axis.setAttributeNS(null, "cy", gamepad.axes[off + 1] * fudgeFactor);
|
||||||
value.textContent = `${gamepad.axes[off].toFixed(2).padStart(5)},${gamepad.axes[off + 1].toFixed(2).padStart(5)}`;
|
value.textContent = `${gamepad.axes[off].toFixed(2).padStart(5)},${gamepad.axes[off + 1].toFixed(2).padStart(5)}`;
|
||||||
});
|
});
|
||||||
buttons.forEach(({ circle, value }, ndx) => {
|
buttons.forEach(({ circle, value }, ndx) => {
|
||||||
const button = gamepad.buttons[ndx];
|
const button = gamepad.buttons[ndx];
|
||||||
circle.setAttributeNS(null, 'r', button.value * fudgeFactor);
|
circle.setAttributeNS(null, "r", button.value * fudgeFactor);
|
||||||
circle.setAttributeNS(null, 'fill', button.pressed ? 'red' : 'gray');
|
circle.setAttributeNS(null, "fill", button.pressed ? "red" : "gray");
|
||||||
value.textContent = `${button.value.toFixed(2)}`;
|
value.textContent = `${button.value.toFixed(2)}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -242,7 +241,7 @@ function processController(info) {
|
|||||||
function addNewPads() {
|
function addNewPads() {
|
||||||
const gamepads = navigator.getGamepads();
|
const gamepads = navigator.getGamepads();
|
||||||
for (let i = 0; i < gamepads.length; i++) {
|
for (let i = 0; i < gamepads.length; i++) {
|
||||||
const gamepad = gamepads[i]
|
const gamepad = gamepads[i];
|
||||||
if (gamepad) {
|
if (gamepad) {
|
||||||
addGamepadIfNew(gamepad);
|
addGamepadIfNew(gamepad);
|
||||||
}
|
}
|
||||||
@ -253,12 +252,13 @@ window.addEventListener("gamepadconnected", handleConnect);
|
|||||||
window.addEventListener("gamepaddisconnected", handleDisconnect);
|
window.addEventListener("gamepaddisconnected", handleDisconnect);
|
||||||
|
|
||||||
function process() {
|
function process() {
|
||||||
runningElem.textContent = ((performance.now() * 0.001 * 60 | 0) % 100).toString().padStart(2, '0');
|
runningElem.textContent = (((performance.now() * 0.001 * 60) | 0) % 100)
|
||||||
|
.toString()
|
||||||
|
.padStart(2, "0");
|
||||||
addNewPads(); // some browsers add by polling, others by event
|
addNewPads(); // some browsers add by polling, others by event
|
||||||
|
|
||||||
Object.values(gamepadsByIndex).forEach(processController);
|
Object.values(gamepadsByIndex).forEach(processController);
|
||||||
requestAnimationFrame(process);
|
requestAnimationFrame(process);
|
||||||
}
|
}
|
||||||
requestAnimationFrame(process);
|
requestAnimationFrame(process);
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user