Accelerazione

Contiene Accelerometer dati acquisiti in un punto specifico nel tempo.

Proprietà

  • x: quantità di accelerazione sull'asse x. (in m/s ^ 2) (Numero)
  • y: quantità di accelerazione sull'asse y. (in m/s ^ 2) (Numero)
  • z: quantità di accelerazione sull'asse z. (in m/s ^ 2) (Numero)
  • timestamp: creazione timestamp in millisecondi. (DOMTimeStamp)

Descrizione

Un Acceleration oggetto è popolato e restituito da uno qualsiasi dell'API Accelerometer metodi. I valori di accelerazione includono l'effetto della gravità (9,81 m/s ^ 2), in modo che quando un dispositivo si trova piatta e rivolto in su, x, y, e z valori restituiti devono essere , , e9.81.

Piattaforme supportate

  • Android
  • BlackBerry WebWorks (OS 5.0 e superiori)
  • iOS
  • Tizen
  • Windows Phone 7 e 8
  • Windows 8

Esempio rapido

function onSuccess(acceleration) {
    alert('Acceleration X: ' + acceleration.x + '\n' +
          'Acceleration Y: ' + acceleration.y + '\n' +
          'Acceleration Z: ' + acceleration.z + '\n' +
          'Timestamp: '      + acceleration.timestamp + '\n');
};

function onError() {
    alert('onError!');
};

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

Esempio completo

<!DOCTYPE html>
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
        alert('Acceleration X: ' + acceleration.x + '\n' +
              'Acceleration Y: ' + acceleration.y + '\n' +
              'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>getCurrentAcceleration</p>
  </body>
</html>