gamepad-tester/index.html
2019-10-12 14:23:13 +09:00

133 lines
3.8 KiB
HTML

<!--
/*
* 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.
*/
-->
<style>
body {
background: #444;
color: white;
font-family: monospace;
}
#gamepads>* {
background: #333;
padding: 1em;
}
#gamepads pre {
white-space: pre-wrap;
}
</style>
<body>
<h1>HTML5 Gamepad Test</h1>
<div>running: <span id="running"></span></div>
<div id="gamepads"></div>
</body>
<script>
const runningElem = document.querySelector('#running');
const gamepadsElem = document.querySelector('#gamepads');
const gamepadsByIndex = {};
function addGamepad(gamepad) {
console.log('add:', gamepad.index);
const elem = document.createElement('pre');
gamepadsElem.appendChild(elem);
gamepadsByIndex[gamepad.index] = {
gamepad,
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 handleConnect(e) {
console.log('connect');
addGamepadIfNew(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 keys = ['id', 'connected', 'mapping', /*'timestamp'*/];
function processController(info) {
const {elem, gamepad} = info;
const lines = [`gamepad : ${gamepad.index}`];
for (const key of keys) {
lines.push(`${key.padEnd(9)}: ${gamepad[key]}`);
}
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);
}
}
}
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
Object.values(gamepadsByIndex).forEach(processController);
requestAnimationFrame(process);
}
requestAnimationFrame(process);
</script>