Gamepad.axes

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

Свойство Gamepad.axes интерфейса Gamepad возвращает массив, показывающий состояние элементов управления, имеющих оси направления, такие как джойстики на геймпаде.

Каждый элемент массива - дробное число в диапазоне от -1.0 до 1.0, показывающий состояние от самого маленького значения (-1.0) до самого высокого (1.0).

Синтаксис

readonly    attribute double[]            axes;

Пример

js
function gameLoop() {
  if (navigator.webkitGetGamepads) {
    var gp = navigator.webkitGetGamepads()[0];
  } else {
    var gp = navigator.getGamepads()[0];
  }

  if (gp.axes[0] != 0) {
    b -= gp.axes[0];
  } else if (gp.axes[1] != 0) {
    a += gp.axes[1];
  } else if (gp.axes[2] != 0) {
    b += gp.axes[2];
  } else if (gp.axes[3] != 0) {
    a -= gp.axes[3];
  }

  ball.style.left = a * 2 + "px";
  ball.style.top = b * 2 + "px";

  var start = rAF(gameLoop);
}

Значение

Массив значений double

Спецификации

Specification
Gamepad
# dom-gamepad-axes

Совместимость с браузерами

Смотрите также