API Reference

Class List

GamePad

A GamePad stores information about a gamepad from the Gamepad API.

Summary

Properties

axes

The values from analog axes present on the GamePad.

buttons

The buttons present on the GamePad.

connected

Whether the gamepad is connected.

hand

The hand this gamepad is usually handled on.

id

The identifier for the gamepad.

index

The index for this controller.

map

The buttons and axes map.

mapping

The gamepad mapping detected by the browser.

Methods

getAxis

Get the value of one of the analog axes of the pad.

getButton

Retrieve a button from its index.

getValue

Returns the value of a button between 0 and 1, with 0 representing a button that is not pressed, and 1 representing a button that is fully pressed.

isPressed

Returns true if the button is pressed.

isTouched

Returns true if the button is touched.

pulse

Make the gamepad vibrate.

resetMap

Reset gamepad mapping to default.

updateMap

Update the map for this gamepad.

wasPressed

Return true if the button was pressed since the last update.

wasReleased

Return true if the button was released since the last update.

wasTouched

Return true if the button was touched since the last update.

Details

Properties

number[]axes

The values from analog axes present on the GamePad. Values are between -1 and 1.

GamePadButton[]buttons

The buttons present on the GamePad.

booleanconnected

Whether the gamepad is connected.

stringhand

The hand this gamepad is usually handled on. Only relevant for XR pads. Value is either "left", "right" or "none".

stringid

The identifier for the gamepad. Its structure depends on device.

numberindex

The index for this controller. A gamepad that is disconnected and reconnected will retain the same index.

objectmap

The buttons and axes map.

stringmapping

The gamepad mapping detected by the browser. Value is either "standard", "xr-standard", "" or "custom". When empty string, you may need to update the mapping yourself. "custom" means you updated the mapping.

Methods

getAxis(axis)

Get the value of one of the analog axes of the pad.

Parameters

axisnumber

The axis to get the value of, use constants PAD_L_STICK_X, etc.

Returns

number

The value of the axis between -1 and 1.

getButton(index)

Retrieve a button from its index.

Parameters

indexnumber

The index to return the button for.

Returns

GamePadButton

The button for the searched index. May be a placeholder if none found.

getValue(button)

Returns the value of a button between 0 and 1, with 0 representing a button that is not pressed, and 1 representing a button that is fully pressed.

Parameters

buttonnumber

The button to retrieve, use constants PAD_FACE_1, etc.

Returns

number

The value of the button between 0 and 1.

isPressed(button)

Returns true if the button is pressed.

Parameters

buttonnumber

The button to test, use constants PAD_FACE_1, etc.

Returns

boolean

True if the button is pressed.

isTouched(button)

Returns true if the button is touched.

Parameters

buttonnumber

The button to test, use constants PAD_FACE_1, etc.

Returns

boolean

True if the button is touched.

pulse(intensity, duration, [options])

Make the gamepad vibrate.

Parameters

intensitynumber

Intensity for the vibration in the range 0 to 1.

durationnumber

Duration for the vibration in milliseconds.

optionsobject

Options for special vibration pattern.

options.startDelaynumber

Delay before the pattern starts, in milliseconds. Defaults to 0.

options.strongMagnitudenumber

Intensity for strong actuators in the range 0 to 1. Defaults to intensity.

options.weakMagnitudenumber

Intensity for weak actuators in the range 0 to 1. Defaults to intensity.

Returns

Promise.

Return a Promise resulting in true if the pulse was successfully completed.

resetMap()

Reset gamepad mapping to default.

updateMap(map)

Update the map for this gamepad.

this.pad.updateMap({
    buttons: [[
        'PAD_FACE_1',
        'PAD_FACE_2',
        'PAD_FACE_3',
        'PAD_FACE_4',
        'PAD_L_SHOULDER_1',
        'PAD_R_SHOULDER_1',
        'PAD_L_SHOULDER_2',
        'PAD_R_SHOULDER_2',
        'PAD_SELECT',
        'PAD_START',
        'PAD_L_STICK_BUTTON',
        'PAD_R_STICK_BUTTON',
        'PAD_VENDOR'
    ],
    axes: [
        'PAD_L_STICK_X',
        'PAD_L_STICK_Y',
        'PAD_R_STICK_X',
        'PAD_R_STICK_Y'
    ],
    synthesizedButtons: {
        PAD_UP: { axis: 0, min: 0, max: 1 },
        PAD_DOWN: { axis: 0, min: -1, max: 0 },
        PAD_LEFT: { axis: 0, min: -1, max: 0 },
        PAD_RIGHT: { axis: 0, min: 0, max: 1 }
    }
});

Parameters

mapobject

The new mapping for this gamepad.

map.buttonsstring[]

Buttons mapping for this gamepad.

map.axesstring[]

Axes mapping for this gamepad.

map.synthesizedButtonsobject

Information about buttons to pull from axes for this gamepad. Requires definition of axis index, min value and max value.

map.mapping"custom"

New mapping format. Will be forced into "custom".

wasPressed(button)

Return true if the button was pressed since the last update.

Parameters

buttonnumber

The button to test, use constants PAD_FACE_1, etc.

Returns

boolean

Return true if the button was pressed, false if not.

wasReleased(button)

Return true if the button was released since the last update.

Parameters

buttonnumber

The button to test, use constants PAD_FACE_1, etc.

Returns

boolean

Return true if the button was released, false if not.

wasTouched(button)

Return true if the button was touched since the last update.

Parameters

buttonnumber

The button to test, use constants PAD_FACE_1, etc.

Returns

boolean

Return true if the button was touched, false if not.