GamepadHelper

Gamepad Helper Module This module provides a set of utilities for working with gamepads in web applications.

Constructor

new GamepadHelper()

Members

CONTROLLER_TYPES :Object

Controller Types

Type:
  • Object
Properties
NameTypeDescription
XBOXstring

Xbox family of controllers

PLAYSTATIONstring

PlayStation family of controllers

SWITCHstring

Nintendo Switch controllers

STANDARDstring

Generic or standard controllers

controllerMappings :Object.<string, {buttonMap: Object.<number, string>, axisMap: Object.<number, string>}>

Controller Mappings Maps controller types to their respective button and axis mappings.

Type:
  • Object.<string, {buttonMap: Object.<number, string>, axisMap: Object.<number, string>}>

exactGamepadMappings :Object.<number, {name: string, gamepad_api_ids: Array.<string>, type: string}>

Exact Gamepad Mappings This object maps specific gamepad API IDs to controller types and names.

Type:
  • Object.<number, {name: string, gamepad_api_ids: Array.<string>, type: string}>

exactIdLookup :Object.<string, {name: string, gamepad_api_ids: Array.<string>, type: string}>

Exact ID Lookup This object maps gamepad API IDs to their respective controller mappings.

Type:
  • Object.<string, {name: string, gamepad_api_ids: Array.<string>, type: string}>

Methods

detectControllerType(gamepadId) → {string}

Detect the controller type based on the gamepad ID

Parameters:
NameTypeDescription
gamepadIdstring | null

The ID of the gamepad as given by the Gamepad API

Returns:

The type of controller (XBOX, PLAYSTATION, SWITCH, STANDARD)

Type: 
string

getAxisName(controllerType, axisIndex) → {string}

Get axis name for given controller type and axis index

Parameters:
NameTypeDescription
controllerTypestring

The type of controller (XBOX, PLAYSTATION, SWITCH, STANDARD)

axisIndexnumber

The index of the axis

Returns:

The name of the axis (e.g., 'Left Stick X', 'Right Stick Y', etc.)

Type: 
string

getButtonImagePath(controllerType, buttonIndex, basePathopt, buttonColoropt, buttonTypeopt) → {string|null}

Get the image path for a specific button on a controller

Parameters:
NameTypeAttributesDefaultDescription
controllerTypestring

The type of controller (XBOX, PLAYSTATION, SWITCH)

buttonIndexnumber

The index of the button

basePathstring<optional>
'/assets/img/gamepads/'

The base path for the images

buttonColorstring<optional>
'White'

The color of the button ('Black' or 'White')

buttonTypestring<optional>
'Outline'

The type of button ('Outline', 'Solid', 'Full Solid')

Returns:

The path to the button image or null if not found

Type: 
string | null

getButtonName(controllerType, buttonIndex) → {string}

Get button name for given controller type and button index

Parameters:
NameTypeDescription
controllerTypestring

The type of controller (XBOX, PLAYSTATION, SWITCH, STANDARD)

buttonIndexnumber

The index of the button

Returns:

The name of the button (e.g., 'A', 'B', 'X', etc.)

Type: 
string

getConnectedGamepads() → {Array.<Gamepad>}

Get all connected gamepads

Returns:

Array of connected gamepad objects

Type: 
Array.<Gamepad>

getGamepadInfo(gamepadId) → {Object}

Get gamepad information based on the gamepad ID

Parameters:
NameTypeDescription
gamepadIdstring | null

The ID of the gamepad as given by the Gamepad API

Returns:

Controller type and name information

Type: 
Object

getVibrationCapabilities(gamepad) → {Object}

Get vibration capabilities of the gamepad

Parameters:
NameTypeDescription
gamepadGamepad | null

The gamepad object from the Gamepad API

Returns:

Vibration capabilities information

Type: 
Object

isSupported() → {boolean}

Check if the Gamepad API is supported in the current browser

Returns:

True if supported, false otherwise

Type: 
boolean

isVibrationSupported(gamepad) → {boolean}

Check if vibration is supported on the gamepad

Parameters:
NameTypeDescription
gamepadGamepad | null

The gamepad object from the Gamepad API

Returns:

True if vibration is supported, false otherwise

Type: 
boolean

stopVibration(gamepad) → {Promise.<(GamepadHapticsResult|Error)>}

Stop vibration on the gamepad

Parameters:
NameTypeDescription
gamepadGamepad | null

The gamepad object from the Gamepad API

Returns:

Promise that resolves when vibration stops

Type: 
Promise.<(GamepadHapticsResult|Error)>

vibrate(gamepad, optionsopt) → {Promise.<(GamepadHapticsResult|Error)>}

Vibrate the gamepad

Parameters:
NameTypeAttributesDefaultDescription
gamepadGamepad | null

The gamepad object from the Gamepad API

optionsObject<optional>
{}

Options for vibration

Properties
NameTypeAttributesDefaultDescription
weakMagnitudenumber<optional>
0.5

Weak rumble magnitude (0-1)

strongMagnitudenumber<optional>
0.5

Strong rumble magnitude (0-1)

durationnumber<optional>
1000

Duration in milliseconds

startDelaynumber<optional>
0

Start delay in milliseconds

Returns:

Promise that resolves when vibration completes

Type: 
Promise.<(GamepadHapticsResult|Error)>