(() => { $id = $('#info-id .value'); $timestamp = $('#info-timestamp .value'); $index = $('#info-index .value'); $mapping = $('#info-mapping .value'); $axes = $('.axes .container'); $buttons = $('.buttons .container'); gamepad = window.gamepad; activeGamepad = gamepad.getActiveGamepad(); if (!activeGamepad) { return; } $id.html(activeGamepad.id); $timestamp.html(activeGamepad.timestamp); $index.html(activeGamepad.index); $mapping.html(activeGamepad.mapping); for (let axisIndex = 0; axisIndex < activeGamepad.axes.length; axisIndex++) { $axes.append( '
' + '
' + '
AXIS ' + axisIndex + '
' + '
0
' + '
' + '
' ); } for (let buttonIndex = 0; buttonIndex < activeGamepad.buttons.length; buttonIndex++) { $buttons.append( '
' + '
' + '
B' + buttonIndex + '
' + '
0
' + '
' + '
' ); } gamepad.updateButton = function($button) { updateElem($button); } gamepad.updateAxis = function($axis) { updateElem($axis, 6); } function updateElem($elem, precision = 2) { updateTimestamp(); var value = parseFloat($elem.attr('data-value'), 10).toFixed(precision); $elem.html(value); var color = Math.floor(255 * 0.3 + 255 * 0.7 * Math.abs(value)); $elem.css({ 'color': 'rgb(' + color + ', ' + color + ', ' + color + ')' }); } function updateTimestamp() { $timestamp.html(activeGamepad.timestamp); } })();