a day ago
- Gamepad API requires user interaction (plugging in or pressing a button) before it works to prevent information leaks.
- Browser support varies; Firefox, Chrome, Opera, Safari, and Edge support it, but IE does not; Edge has quirks.
- Use navigator.getGamepads() to get an array of gamepad objects; the array may contain null entries for disconnected slots.
- Gamepad objects have properties: index, id, axes, buttons, mapping, and connected; use the index property to identify specific gamepads.
- Poll gamepad state in a requestAnimationFrame loop rather than relying on events; events for connect/disconnect are unreliable.
- Standard Gamepad mapping sets mapping="standard" and defines button/axis layouts; non-standard controllers require manual handling.
- Buttons have pressed (boolean) and value (0.0 to 1.0 for analog); axes range from -1 to 1 for stick positions.
- Implement a dead zone to ignore small axis values near center and prevent unintended drift.
- Clamp joystick values to a circle of radius 1 to normalize maximum speed across different controllers.