From bf9a77419383c84821bd1dde7e2f8913393dfdf7 Mon Sep 17 00:00:00 2001 From: YoRu Tech Date: Sun, 10 Mar 2024 19:31:03 +0500 Subject: [PATCH] Added Rumble On Button Press --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 88a8882..95b2a44 100644 --- a/index.js +++ b/index.js @@ -123,11 +123,25 @@ function updateStick(elementId, leftRightAxis, upDownAxis) { stick.setAttribute("cx", x + stickLeftRight); stick.setAttribute("cy", y + stickUpDown); } +function handleRumble(gamepad) { + const rumbleOnButtonPress = document.getElementById("rumble-on-button-press"); + if (rumbleOnButtonPress.checked) { + if (gamepad.buttons.some((button) => button.value > 0)) { + gamepad.vibrationActuator.playEffect("dual-rumble", { + startDelay: 0, + duration: 25, + weakMagnitude: 1.0, + strongMagnitude: 1.0, + }); + } + } +} function gameLoop() { if (controllerIndex !== null) { const gamepad = navigator.getGamepads()[controllerIndex]; handleButtons(gamepad.buttons); handleSticks(gamepad.axes); + handleRumble(gamepad); } requestAnimationFrame(gameLoop); }