Gamepad API

On this page 7

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.

PropertyType or rangeMeaning
idStringDevice description reported by the browser
indexIntegerStable slot number while connected
connectedBooleanWhether the device is still attached
mapping“standard” or empty stringWhether the standard layout applies
axesNumbers from -1.0 to 1.0Analog stick positions
buttonsGamepadButton listPressed, touched and value from 0 to 1
timestampHigh-resolution timeMoment of the last state update
Key properties of a Gamepad object

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.

IndexInputValue type
Button 0Bottom face buttonDigital, 0 or 1
Button 6 and 7Left and right triggersAnalog, 0 to 1
Buttons 12 to 15Directional padDigital, 0 or 1
Button 16Center or home buttonDigital, 0 or 1
Axes 0 and 1Left stick horizontal and verticalAnalog, -1 to 1
Axes 2 and 3Right stick horizontal and verticalAnalog, -1 to 1
Selected positions in the standard gamepad layout

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.

InterfaceInput modelTypical devices
Gamepad APIPolling of state snapshotsConsole-style controllers
Keyboard and pointer eventsEvent per inputKeyboard, mouse, touch, pen
WebXR Gamepads ModuleGamepad objects attached to XR input sourcesVR and AR hand controllers
WebHID APIRaw HID reports, permission per deviceSpecialized controllers and peripherals
Gamepad API compared with related input interfaces

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.

SymptomCauseFix
Controller not detectedNo button pressed since page loadPrompt the user to press any button
Character drifts slowlyStick noise near the centerApply a radial dead zone
Single press triggers twiceState read without edge detectionCompare with the previous frame
Wrong buttons in one browserNon-standard mapping reportedCheck mapping and offer remapping
Input ignored in an iframePermissions policy blocks the featureAllow the gamepad feature for the frame
Frequent symptoms and their fixes

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.