||
捕捉的x,y装置的动作和z方向。
· accelerometer.getCurrentAcceleration
· accelerometer.watchAcceleration
accelerometer.getCurrentAcceleration
相处的X,Y轴电流加速,和Z轴。
navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
Description说明加速度计是一个运动传感器,它检测到的运动变化(增量)相对于当前设备的方向。 加速度计可以检测沿的x,y三维运动,和Z轴。
加速返回使用 accelerometerSuccess 回调函数。
支持的平台· Android
· BlackBerry WebWorks (OS 5.0 and higher)
· iPhone
简单的例子:
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 PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is ready
//
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 onload="onLoad()">
<h1>Example</h1>
<p>getCurrentAcceleration
· 你必须看的加速和在给定的时间间隔捕获数据。
· 因此, getCurrentAcceleration 功能会给你的最后一个值phoneGap报告从 watchAccelerometer 电话。
accelerometer.watchAcceleration
在定期的时间间隔,相处的X,Y轴加速度,和Z轴。
var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
accelerometerError,
[accelerometerOptions]);
加速度计是一个运动传感器,它检测到的运动变化(增量)相对于当前的位置。 加速度计可以检测沿的x,y三维运动,和Z轴。
该 accelerometer.watchAcceleration 获得加速度定期间隔设备的电流。 每次 加速 的检索, accelerometerSuccess 回调函数被执行。 通过指定的时间间隔以毫秒为单位 的频率 参数在 acceleratorOptions 对象。
返回的手表ID引用引用加速度计手表的时间间隔。 这款手表ID可用于 accelerometer.clearWatch 停止观看加速度计。
支持的平台· Android
· BlackBerry WebWorks (OS 5.0 and higher)
· iPhone
简单的例子:
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!');
};
var options = { frequency: 3000 }; // Update every 3 seconds
var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
完整的例子
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// The watch id references the current `watchAcceleration`
var watchID = null;
// Wait for PhoneGap to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is ready
//
function onDeviceReady() {
startWatch();
}
// Start watching the acceleration
//
function startWatch() {
// Update acceleration every 3 seconds
var options = { frequency: 3000 };
watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}
// Stop watching the acceleration
//
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z + '<br />' +
'Timestamp: ' + acceleration.timestamp + '<br />';
}
// onError: Failed to get the acceleration
//
function onError() {
alert('onError!');
}
</script>
</head>
<body onload="onLoad()">
<div id="accelerometer">Waiting for accelerometer...</div>
</body>
</html>
· 在要求的时间间隔,PhoneGap将调用成功的回调函数并传递加速度计的结果。
· 然而,在对设备的要求PhoneGap限制到最小间隔每40毫秒和每1000毫秒最大。
· 例如,如果您要求的3秒(3000ms)间隔,PhoneGap将要求从设备的时间间隔为1秒,但在调用请求3秒间隔的成功回调。
accelerometer.clearWatch
不看 加速 引用手表ID参数。
navigator.accelerometer.clearWatch(watchID);
· watchID: 返回的ID accelerometer.watchAcceleration.
支持的平台
· Android
· BlackBerry WebWorks (OS 5.0 and higher)
· iPhone
简单的例子:
var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
// ... later on ...
navigator.accelerometer.clearWatch(watchID);
完整的例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// The watch id references the current `watchAcceleration`
var watchID = null;
// Wait for PhoneGap to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is ready
//
function onDeviceReady() {
startWatch();
}
// Start watching the acceleration
//
function startWatch() {
// Update acceleration every 3 seconds
var options = { frequency: 3000 };
watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}
// Stop watching the acceleration
//
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z + '<br />' +
'Timestamp: ' + acceleration.timestamp + '<br />';
}
// onError: Failed to get the acceleration
//
function onError() {
alert('onError!');
}
</script>
</head>
<body onload="onLoad()">
<div id="accelerometer">Waiting for accelerometer...</div>
<button onclick="stopWatch();">Stop Watching</button>
</body>
</html>
包含 加速度计 数据捕获在特定的时间点。
· 谢: 对运动量的X轴。 区间[0,1]( 数 )
· Ÿ: 轴金 额的运动在Y。 区间[0,1]( 数 )
· 张: 轴金 额的运动在z。 区间[0,1]( 数 )
· 时间戳: 时间戳毫秒创作研究。 ( DOMTimeStamp )
这个对象是通过创建和PhoneGap人口,以及返回的 加速度 的方法。
· Android
· BlackBerry WebWorks (OS 5.0 and higher)
· iPhone
简单的例子:
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 PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Acceleration Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is ready
//
function onDeviceReady() {
navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
}
// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess() {
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 onload="onLoad()">
<h1>Example</h1>
<p>getCurrentAcceleration
onSuccess 回调函数,提供了 Acceleration 的信息。
function(acceleration) {
// 做什么
}
参数
· acceleration: 加速时间在一个单一的时刻。 (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回调函数。
function() {
// Handle the error
}
一个可选的参数来定制加速检索。
· 频率: 多久检索 Acceleration 以毫秒为单位。 (数目) (默认:10000)
Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )
GMT+8, 2024-5-5 21:22 , Processed in 0.037366 second(s), 22 queries .
Powered by Discuz! X3.2
© 2001-2013 Comsenz Inc.