Created
March 30, 2024 21:29
-
-
Save CougarTown/b05d01afe8215bbaef6163685b38e2da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Xbox Controller Extended Features</title> | |
</head> | |
<body> | |
<script> | |
window.addEventListener("gamepadconnected", (event) => { | |
console.log("Gamepad connected:", event.gamepad); | |
checkGamepadState(); | |
}); | |
window.addEventListener("gamepaddisconnected", (event) => { | |
console.log("Gamepad disconnected:", event.gamepad); | |
}); | |
function checkGamepadState() { | |
requestAnimationFrame(checkGamepadState); | |
const gamepads = navigator.getGamepads(); | |
const xboxController = findXboxController(gamepads); | |
if (xboxController) { | |
// Check button presses | |
for (let i = 0; i < xboxController.buttons.length; i++) { | |
if (xboxController.buttons[i].pressed) { | |
console.log("Button " + i + " pressed"); | |
} | |
} | |
// Check analog sticks | |
console.log("Left Stick:", xboxController.axes[0], xboxController.axes[1]); | |
console.log("Right Stick:", xboxController.axes[2], xboxController.axes[3]); | |
// Check triggers | |
console.log("Left Trigger:", xboxController.buttons[6].value); | |
console.log("Right Trigger:", xboxController.buttons[7].value); | |
// Vibration example (if supported by the browser) | |
if (xboxController.vibrationActuator) { | |
const strength = 0.5; // Vibration strength between 0 and 1 | |
const duration = 1000; // Vibration duration in milliseconds | |
xboxController.vibrationActuator.playEffect("dual-rumble", { | |
duration, | |
strongMagnitude: strength, | |
weakMagnitude: strength, | |
}); | |
} | |
} | |
} | |
function findXboxController(gamepads) { | |
for (let i = 0; i < gamepads.length; i++) { | |
const gamepad = gamepads[i]; | |
if (gamepad && gamepad.id.includes("Xbox")) { | |
return gamepad; | |
} | |
} | |
return null; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment