This version of the documentation is outdated!
Click here for the latest released version.
加速度
包含 Accelerometer
在時間中的特定點捕獲的資料。
屬性
- x: 在 X 軸上的加速度量。(在 m/s ^2)(人數)
- y: 在 y 軸上的加速度量。(在 m/s ^2)(人數)
- z: 在 Z 軸上的加速度量。(在 m/s ^2)(人數)
- 時間戳記: 創建時間戳記以毫秒為單位。() DOMTimeStamp
說明
Acceleration
物件是填充的返回的任何的 API 的 Accelerometer
方法。 加速度值包括引力的影響 (9.81 m/s ^2),因此當設備謊言平面和麵朝上, x、 y,和z返回的值應該是 ,
,和9.81
.
支援的平臺
- Android 系統
- 黑莓手機 WebWorks (OS 5.0 和更高)
- iOS
- Tizen
- Windows Phone 7 和 8
- Windows 8
快速的示例
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);
完整的示例
<!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>