A browser API for Sony DualShock 4 controllers built over the WebHID API.
Provides high level APIs to access most of the controller's features over USB and Bluetooth.
npm install --save webhid-ds4
import { DualShock4 } from 'webhid-ds4'
// The WebHID device can only be requested upon user interaction
document.getElementById('connectButton').addEventListener('click', async () => {
const DS4 = new DualShock4()
// This will request the WebHID device and initialize the controller
await DS4.init()
// Define a custom lightbar color
await DS4.lightbar.setColorRGB(170, 255, 0)
// The state object is updated periodically with the current controller state
function logInputs () {
requestAnimationFrame(logInputs)
document.getElementById('triangle').innerText = `Triangle Button: ${DS4.state.buttons.triangle}`
document.getElementById('circle').innerText = `Circle Button: ${DS4.state.buttons.circle}`
document.getElementById('cross').innerText = `Cross Button: ${DS4.state.buttons.cross}`
document.getElementById('square').innerText = `Square Button: ${DS4.state.buttons.square}`
document.getElementById('leftStick').innerText = `Left Stick: ${DS4.state.axes.leftStickX}, ${DS4.state.axes.leftStickY}`
document.getElementById('rightStick').innerText = `Right Stick: ${DS4.state.axes.rightStickX}, ${DS4.state.axes.rightStickY}`
}
logInputs()
})
API Documentation is available here
Generated using TypeDoc