The Gamepad API is a browser interface, specified by the W3C, that lets a web page read the state of game controllers such as buttons, triggers and analog sticks through JavaScript. Among Web APIs, it is unusual because it is primarily polling-based: instead of firing an event for every button press, it exposes snapshots of controller state that a page reads on each animation frame.
The API underpins browser games, cloud gaming clients, accessibility tools and controller-driven interfaces for kiosks or television apps. It is supported in all major engines, works only in a secure context over HTTPS, and is complemented by the WebXR Gamepads Module for virtual reality controllers.
How the Gamepad API exposes controller state
A page obtains controllers by calling navigator.getGamepads(), which returns an array of Gamepad objects or null slots, indexed by the order in which the devices were connected. Each object is a snapshot of controller state at a given moment, with a timestamp that changes only when the hardware reports new data. Comparing timestamps lets a game skip processing when nothing has changed.
Two window events, gamepadconnected and gamepaddisconnected, announce when a controller becomes available or disappears. For privacy reasons, browsers usually hide connected controllers until the user presses a button or moves a stick while the page is visible, so a page cannot silently enumerate attached hardware. The table lists the most important properties.
| Property | Type or range | Meaning |
|---|---|---|
id | String | Device description reported by the browser |
index | Integer | Stable slot number while connected |
connected | Boolean | Whether the device is still attached |
mapping | “standard” or empty string | Whether the standard layout applies |
axes | Numbers from -1.0 to 1.0 | Analog stick positions |
buttons | GamepadButton list | Pressed, touched and value from 0 to 1 |
timestamp | High-resolution time | Moment of the last state update |
The polling loop and why it matters
Because the API does not emit events for individual inputs, a page must poll inside a `requestAnimationFrame` loop, reading navigator.getGamepads() once per frame and comparing the result with the previous frame. Detecting a button press edge, the moment a button changes from released to pressed, is therefore the application’s job rather than the browser’s.
Polling matches how game engines already work, since game logic usually runs once per frame. It also means input latency is bounded by the frame rate: at 60 frames per second, a press can wait up to about 16.7 milliseconds before the page sees it. In some engines the returned objects are live and in others they are snapshots, so robust code calls `getGamepads()` again every frame rather than caching a reference.
Standard mapping and device differences
The specification defines a standard gamepad layout modeled on common console controllers: 17 buttons and 4 axes in fixed positions. When the browser recognizes a device and can remap it, mapping equals "standard", and button 0 is always the bottom face button regardless of manufacturer. When the value is an empty string, indices follow the raw hardware order, and the page must offer manual remapping.
The following table shows representative entries of the standard layout.
| Index | Input | Value type |
|---|---|---|
| Button 0 | Bottom face button | Digital, 0 or 1 |
| Button 6 and 7 | Left and right triggers | Analog, 0 to 1 |
| Buttons 12 to 15 | Directional pad | Digital, 0 or 1 |
| Button 16 | Center or home button | Digital, 0 or 1 |
| Axes 0 and 1 | Left stick horizontal and vertical | Analog, -1 to 1 |
| Axes 2 and 3 | Right stick horizontal and vertical | Analog, -1 to 1 |
Recognition depends on the browser, operating system and connection type. The same controller can report standard mapping over USB in one browser and a raw layout over Bluetooth in another, so production games keep a remapping screen as a fallback.
Haptic feedback and extensions
Many implementations expose a vibrationActuator on the Gamepad object with a playEffect() method. The dual-rumble effect drives a strong low-frequency motor and a weak high-frequency motor with magnitudes from 0 to 1 and a duration in milliseconds, while some browsers also support trigger-rumble for controllers with motors in the triggers. Haptics support varies more across browsers than basic input, so it should be treated as an enhancement.
Extensions cover hardware the original layout did not anticipate. The table compares the input interfaces a web developer is likely to meet.
| Interface | Input model | Typical devices |
|---|---|---|
| Gamepad API | Polling of state snapshots | Console-style controllers |
| Keyboard and pointer events | Event per input | Keyboard, mouse, touch, pen |
| WebXR Gamepads Module | Gamepad objects attached to XR input sources | VR and AR hand controllers |
| WebHID API | Raw HID reports, permission per device | Specialized controllers and peripherals |
Common mistakes and practical safeguards
Most problems with the Gamepad API come from treating controllers as precise or identical devices. Analog sticks rarely rest at exactly zero, so a dead zone of roughly 0.1 to 0.2 on each axis is usual to prevent drift, and the right value depends on the hardware’s wear and quality.
| Symptom | Cause | Fix |
|---|---|---|
| Controller not detected | No button pressed since page load | Prompt the user to press any button |
| Character drifts slowly | Stick noise near the center | Apply a radial dead zone |
| Single press triggers twice | State read without edge detection | Compare with the previous frame |
| Wrong buttons in one browser | Non-standard mapping reported | Check mapping and offer remapping |
| Input ignored in an iframe | Permissions policy blocks the feature | Allow the gamepad feature for the frame |
Developers should also consider fingerprinting risk: the id string and number of axes reveal hardware details, which is why browsers gate exposure behind a user gesture. A page should never depend on the `id` text for core logic, because its format differs between engines.
A plain-language summary
The Gamepad API lets a website use a game controller the way a game console does. When you plug in a controller and press a button, the page can see which buttons are held and how far the sticks are pushed.
Instead of being told about every press, the page checks the controller many times per second, much like glancing at a dashboard while driving. That is why browser games can feel smooth and responsive.
An example from practice
A small studio released a browser version of its platform game and received reports that players using older wireless controllers saw their character walk left on its own. Logs showed resting stick values between 0.05 and 0.12, well above zero, on worn hardware.
The developers added a radial dead zone of 0.15, rescaled the remaining range so full deflection still reached 1.0, and added edge detection so a jump fired once per press. They also displayed a short prompt asking players to press any button, since controllers were not visible to the page before that. After the update, drift reports stopped and controller support worked consistently across the major browsers, with a remapping menu covering devices that reported no standard mapping.