Skip to content

Commit

Permalink
state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mounir Lamouri committed Sep 23, 2024
1 parent b39521c commit c2d226a
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions latency/gamepad-button.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<script>
let gamepad = null;
let isBlack = false;
let isPressed = false;

window.addEventListener('gamepadconnected', (event) => {
gamepad = event.gamepad;
Expand All @@ -22,18 +23,30 @@
gamepad = null;
});

function runRAFUpdate() {
requestAnimationFrame(update);
}

function update() {
if (gamepad && gamepad.buttons[0].pressed) {
if (isBlack) {
document.body.style.backgroundColor = 'white';
isBlack = false;
} else {
document.body.style.backgroundColor = 'black';
isBlack = true;
}
// No gamepads yet.
if (!gamepad)
return runRAFUpdate();

// State hasn't changed.
if (isPressed == gamepad.buttons[0].pressed)
return runRAFUpdate();

isPressed = !isPressed;

if (isBlack) {
document.body.style.backgroundColor = 'white';
isBlack = false;
} else {
document.body.style.backgroundColor = 'black';
isBlack = true;
}

requestAnimationFrame(update);
runRAFUpdate();
}

update();
Expand Down

0 comments on commit c2d226a

Please sign in to comment.