This commit is contained in:
Rim 2025-04-23 20:40:48 -04:00
parent 23ffb6ea2a
commit ae9710d724
2 changed files with 217 additions and 211 deletions

44
app.js
View File

@ -7,21 +7,20 @@ 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",
".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) => {
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);
@ -31,15 +30,14 @@ http
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);
}
if (err) {
res.writeHead(404);
res.end("404 Not Found");
} else {
res.writeHead(200, { "Content-Type": contentType });
res.end(content);
}
});
})
.listen(port, host, () => {
}).listen(port, host, () => {
console.log(`Server running at http://${host}:${port}/`);
});
});

View File

@ -22,88 +22,88 @@
*/
-->
<style>
body {
background: #444;
color: white;
font-family: monospace;
}
#gamepads > * {
background: #333;
padding: 1em;
margin: 10px 5px 0 0;
}
#gamepads pre {
white-space: pre-wrap;
}
body {
background: #444;
color: white;
font-family: monospace;
}
#gamepads > * {
background: #333;
padding: 1em;
margin: 10px 5px 0 0;
}
#gamepads pre {
white-space: pre-wrap;
}
.head {
display: flex;
}
.head .id {
flex: 1 1 auto;
}
.head .index,
.head .id {
display: inline-block;
background: #222;
padding: 0.5em;
}
.head .index {
}
.head {
display: flex;
}
.head .id {
flex: 1 1 auto;
}
.head .index,
.head .id {
display: inline-block;
background: #222;
padding: 0.5em;
}
.head .index {
}
.info .label {
width: 7em;
display: inline-block;
}
.info > div {
padding: 0.25em;
background: #222;
margin: 0.25em 0.25em 0 0;
}
.info .label {
width: 7em;
display: inline-block;
}
.info > div {
padding: 0.25em;
background: #222;
margin: 0.25em 0.25em 0 0;
}
.inputs {
display: flex;
}
.axes {
display: flex;
align-items: flex-start;
}
.inputs {
display: flex;
}
.axes {
display: flex;
align-items: flex-start;
}
.svg text {
color: #ccc;
font-family: monospace;
}
.svg text {
color: #ccc;
font-family: monospace;
}
.axes svg text {
font-size: 0.6px;
}
.buttons svg text {
font-size: 1.2px;
}
.axes > div,
.buttons > div {
display: inline-block;
background: #222;
}
.axes > div {
margin: 2px 5px 0 0;
}
.buttons > div {
margin: 2px 2px 0 0;
}
.axes svg text {
font-size: 0.6px;
}
.buttons svg text {
font-size: 1.2px;
}
.axes > div,
.buttons > div {
display: inline-block;
background: #222;
}
.axes > div {
margin: 2px 5px 0 0;
}
.buttons > div {
margin: 2px 2px 0 0;
}
</style>
<body>
<h1>HTML5 Gamepad Test</h1>
<div>running: <span id="running"></span></div>
<div id="gamepads"></div>
<h1>HTML5 Gamepad Test</h1>
<div>running: <span id="running"></span></div>
<div id="gamepads"></div>
</body>
<script>
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 gamepadsElem = document.querySelector("#gamepads");
const gamepadsByIndex = {};
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 gamepadsElem = document.querySelector("#gamepads");
const gamepadsByIndex = {};
const controllerTemplate = `
const controllerTemplate = `
<div>
<div class="head"><div class="index"></div><div class="id"></div></div>
<div class="info"><div class="label">connected:</div><span class="connected"></span></div>
@ -114,7 +114,7 @@
</div>
</div>
`;
const axisTemplate = `
const axisTemplate = `
<svg viewBox="-2.2 -2.2 4.4 4.4" width="128" height="128">
<circle cx="0" cy="0" r="2" fill="none" stroke="#888" stroke-width="0.04" />
<path d="M0,-2L0,2M-2,0L2,0" stroke="#888" stroke-width="0.04" />
@ -123,7 +123,7 @@
</svg>
`;
const buttonTemplate = `
const buttonTemplate = `
<svg viewBox="-2.2 -2.2 4.4 4.4" width="64" height="64">
<circle cx="0" cy="0" r="2" fill="none" stroke="#888" stroke-width="0.1" />
<circle cx="0" cy="0" r="0" fill="none" fill="red" class="button" />
@ -132,133 +132,141 @@
</svg>
`;
function addGamepad(gamepad) {
console.log("add:", gamepad.index);
const elem = document.createElement("div");
elem.innerHTML = controllerTemplate;
function addGamepad(gamepad) {
console.log("add:", gamepad.index);
const elem = document.createElement("div");
elem.innerHTML = controllerTemplate;
const axesElem = elem.querySelector(".axes");
const buttonsElem = elem.querySelector(".buttons");
const axesElem = elem.querySelector(".axes");
const buttonsElem = elem.querySelector(".buttons");
const axes = [];
for (let ndx = 0; ndx < gamepad.axes.length; ndx += 2) {
const div = document.createElement("div");
div.innerHTML = axisTemplate;
axesElem.appendChild(div);
axes.push({
axis: div.querySelector(".axis"),
value: div.querySelector("text"),
});
}
const axes = [];
for (let ndx = 0; ndx < gamepad.axes.length; ndx += 2) {
const div = document.createElement("div");
div.innerHTML = axisTemplate;
axesElem.appendChild(div);
axes.push({
axis: div.querySelector(".axis"),
value: div.querySelector("text"),
});
}
const buttons = [];
for (let ndx = 0; ndx < gamepad.buttons.length; ++ndx) {
const div = document.createElement("div");
div.innerHTML = buttonTemplate;
buttonsElem.appendChild(div);
div.querySelector(".index").textContent = ndx;
buttons.push({
circle: div.querySelector(".button"),
value: div.querySelector(".value"),
});
}
const buttons = [];
for (let ndx = 0; ndx < gamepad.buttons.length; ++ndx) {
const div = document.createElement("div");
div.innerHTML = buttonTemplate;
buttonsElem.appendChild(div);
div.querySelector(".index").textContent = ndx;
buttons.push({
circle: div.querySelector(".button"),
value: div.querySelector(".value"),
});
}
gamepadsByIndex[gamepad.index] = {
gamepad,
elem,
axes,
buttons,
index: elem.querySelector(".index"),
id: elem.querySelector(".id"),
mapping: elem.querySelector(".mapping"),
connected: elem.querySelector(".connected"),
};
gamepadsElem.appendChild(elem);
}
gamepadsByIndex[gamepad.index] = {
gamepad,
elem,
axes,
buttons,
index: elem.querySelector(".index"),
id: elem.querySelector(".id"),
mapping: elem.querySelector(".mapping"),
connected: elem.querySelector(".connected"),
};
gamepadsElem.appendChild(elem);
}
function removeGamepad(gamepad) {
const info = gamepadsByIndex[gamepad.index];
if (info) {
delete gamepadsByIndex[gamepad.index];
info.elem.parentElement.removeChild(info.elem);
}
}
function removeGamepad(gamepad) {
const info = gamepadsByIndex[gamepad.index];
if (info) {
delete gamepadsByIndex[gamepad.index];
info.elem.parentElement.removeChild(info.elem);
}
}
function addGamepadIfNew(gamepad) {
const info = gamepadsByIndex[gamepad.index];
if (!info) {
addGamepad(gamepad);
} else {
// This broke sometime in the past. It used to be
// the same gamepad object was returned forever.
// Then Chrome only changed to a new gamepad object
// is returned every frame.
info.gamepad = gamepad;
}
}
function addGamepadIfNew(gamepad) {
const info = gamepadsByIndex[gamepad.index];
if (!info) {
addGamepad(gamepad);
} else {
// This broke sometime in the past. It used to be
// the same gamepad object was returned forever.
// Then Chrome only changed to a new gamepad object
// is returned every frame.
info.gamepad = gamepad;
}
}
function handleConnect(e) {
console.log("connect");
addGamepadIfNew(e.gamepad);
}
function handleConnect(e) {
console.log("connect");
addGamepadIfNew(e.gamepad);
}
function handleDisconnect(e) {
console.log("disconnect");
removeGamepad(e.gamepad);
}
function handleDisconnect(e) {
console.log("disconnect");
removeGamepad(e.gamepad);
}
const t = String.fromCharCode(0x26aa);
const f = String.fromCharCode(0x26ab);
function onOff(v) {
return v ? t : f;
}
const t = String.fromCharCode(0x26aa);
const f = String.fromCharCode(0x26ab);
function onOff(v) {
return v ? t : f;
}
const keys = ["index", "id", "connected", "mapping" /*'timestamp'*/];
function processController(info) {
const { elem, gamepad, axes, buttons } = info;
const lines = [`gamepad : ${gamepad.index}`];
for (const key of keys) {
info[key].textContent = gamepad[key];
}
axes.forEach(({ axis, value }, ndx) => {
const off = ndx * 2;
axis.setAttributeNS(null, "cx", gamepad.axes[off] * 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)}`;
});
buttons.forEach(({ circle, value }, ndx) => {
const button = gamepad.buttons[ndx];
circle.setAttributeNS(null, "r", button.value * fudgeFactor);
circle.setAttributeNS(null, "fill", button.pressed ? "red" : "gray");
value.textContent = `${button.value.toFixed(2)}`;
});
const keys = ["index", "id", "connected", "mapping" /*'timestamp'*/];
function processController(info) {
const { elem, gamepad, axes, buttons } = info;
const lines = [`gamepad : ${gamepad.index}`];
for (const key of keys) {
info[key].textContent = gamepad[key];
}
axes.forEach(({ axis, value }, ndx) => {
const off = ndx * 2;
axis.setAttributeNS(null, "cx", gamepad.axes[off] * 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)}`;
});
buttons.forEach(({ circle, value }, ndx) => {
const button = gamepad.buttons[ndx];
circle.setAttributeNS(null, "r", button.value * fudgeFactor);
circle.setAttributeNS(
null,
"fill",
button.pressed ? "red" : "gray",
);
value.textContent = `${button.value.toFixed(2)}`;
});
// lines.push(`axes : [${gamepad.axes.map((v, ndx) => `${ndx}: ${v.toFixed(2).padStart(5)}`).join(', ')} ]`);
// lines.push(`buttons : [${gamepad.buttons.map((v, ndx) => `${ndx}: ${onOff(v.pressed)} ${v.value.toFixed(2)}`).join(', ')} ]`);
// elem.textContent = lines.join('\n');
}
// lines.push(`axes : [${gamepad.axes.map((v, ndx) => `${ndx}: ${v.toFixed(2).padStart(5)}`).join(', ')} ]`);
// lines.push(`buttons : [${gamepad.buttons.map((v, ndx) => `${ndx}: ${onOff(v.pressed)} ${v.value.toFixed(2)}`).join(', ')} ]`);
// elem.textContent = lines.join('\n');
}
function addNewPads() {
const gamepads = navigator.getGamepads();
for (let i = 0; i < gamepads.length; i++) {
const gamepad = gamepads[i];
if (gamepad) {
addGamepadIfNew(gamepad);
}
}
}
function addNewPads() {
const gamepads = navigator.getGamepads();
for (let i = 0; i < gamepads.length; i++) {
const gamepad = gamepads[i];
if (gamepad) {
addGamepadIfNew(gamepad);
}
}
}
window.addEventListener("gamepadconnected", handleConnect);
window.addEventListener("gamepaddisconnected", handleDisconnect);
window.addEventListener("gamepadconnected", handleConnect);
window.addEventListener("gamepaddisconnected", handleDisconnect);
function process() {
runningElem.textContent = (((performance.now() * 0.001 * 60) | 0) % 100)
.toString()
.padStart(2, "0");
addNewPads(); // some browsers add by polling, others by event
function process() {
runningElem.textContent = (((performance.now() * 0.001 * 60) | 0) % 100)
.toString()
.padStart(2, "0");
addNewPads(); // some browsers add by polling, others by event
Object.values(gamepadsByIndex).forEach(processController);
requestAnimationFrame(process);
}
requestAnimationFrame(process);
Object.values(gamepadsByIndex).forEach(processController);
requestAnimationFrame(process);
}
requestAnimationFrame(process);
</script>