Gamepad: axes-Eigenschaft

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.

Sicherer Kontext: Diese Funktion ist nur in sicheren Kontexten (HTTPS) in einigen oder allen unterstützenden Browsern verfügbar.

Die Gamepad.axes-Eigenschaft des Gamepad-Interfaces gibt ein Array zurück, das die Steuerungen mit Achsen auf dem Gerät darstellt (z.B. analoge Steuerknüppel).

Jeder Eintrag im Array ist ein Fliesskommawert im Bereich von -1,0 bis 1,0, der die Achsposition vom niedrigsten Wert (-1,0) bis zum höchsten Wert (1,0) darstellt.

Wert

Ein Array mit Zahlen.

Beispiele

js
function gameLoop() {
  const [gp] = navigator.getGamepads();

  let a = 0;
  let b = 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`;

  const start = requestAnimationFrame(gameLoop);
}

Spezifikationen

Specification
Gamepad
# dom-gamepad-axes

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
axes

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch