cordova device-motion插件
来源:互联网 发布:薛之谦淘宝店不开了吗 编辑:程序博客网 时间:2024/06/05 06:36
介绍
这个插件提供了访问设备的加速度计。加速度计是一个运动传感器检测到的变化(三维维度)在相对运动的当前设备的方向,在三个维度上沿X,Y,和Z坐标轴。用来在三个维度来跟踪设备的运动。
安装
cordova plugin add cordova-plugin-device-motion
支持的平台Supported Platforms
· Amazon Fire OS
· Android
· BlackBerry 10
· Browser
· Firefox OS
· iOS
· Tizen
· Windows Phone 8
· Windows
使用方法
访问是通过一个全局navigator.accelerometer对象
虽然对象连接到全局navigator,但是需要在deviceready事件之后才可用
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.accelerometer);
}
方法Methods
· navigator.accelerometer.getCurrentAcceleration
· navigator.accelerometer.watchAcceleration
· navigator.accelerometer.clearWatch
navigator.accelerometer.getCurrentAcceleration
拿到当前加速度的X,Y,和Z轴
这些加速度值返回accelerometerSuccess回调函数
navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
Example
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);
特性
Browser 特性
X、Y、Z运动的值都是随机生成的,以模拟加速度计。
Android 特性
加速度计被称为与SENSOR_DELAY_UI标记,这限制了最大读出频率之间为20-60赫兹,这取决于设备。period 的值对应于更高频率将导致重复的样本。更多的细节可以查看Android API指南
iOS 特性
iOS不承认在任何给定的点取当前加速度的概念。
你必须看的加速度和在给定的时间间隔采集数据。
因此,该getCurrentAcceleration 函数产生的最后一个返回值从调用watchAccelerometer
navigator.accelerometer.watchAcceleration
检索器的电流加速度定期考核,accelerometersuccess每一次回调函数。通过指定间隔毫秒acceleratoroptions对象的频率参数.
回看ID引用加速度计的观看间隔,可以使用navigator.accelerometer.clearwatch停止看加速度计
var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
accelerometerError, accelerometerOptions);
· accelerometerOptions: 下列可选键对象
frequency: 请求调用accelerometerSuccess在毫秒的加速度数据。(数字型)(默认值:10000)
Example
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);
特性
iOS Quirks
在定时请求这个API返回的成功的回调时,但是限制要求的设备之间的40ms-1000ms范围。例如,如果你要求一个3秒的间隔,(3000ms),API从设备每1秒请求数据,但执行成功回调是每3秒执行一次。
navigator.accelerometer.clearWatch
停止查看加速度引用的watchID 参数.
navigator.accelerometer.clearWatch(watchID);
· watchID: navigator.accelerometer.watchAcceleration返回的ID
Example
var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
// ... later on ...
navigator.accelerometer.clearWatch(watchID);
对象Objects
· Acceleration
加速度Acceleration
包含加速度计在特定时间点捕获的数据。加速度值包括重力的影响(9.81米/秒^ 2),所以当一个平和面对,X,Y,和Z返回值应该是0,0,和9.81
属性Properties
· x: 加速度在X轴的坐标。(米/秒^ 2)(数字型)
· y: 加速度在y轴的坐标。(米/秒^ 2)(数字型)
· z: 加速度在Z轴的坐标。(米/秒^ 2)(数字型)
· timestamp: 以毫秒为单位创建时间戳。(DOMTimeStamp)
示例
示例一:
index.html:
<!DOCTYPE html><html><head> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> <meta name="format-detection" content="telephone=no"> <meta name="msapplication-tap-highlight" content="no"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <title>Hello World</title> <style> .line{ padding: 10px; } </style></head><body><div class="app"> <h1>device-motion插件</h1> <div class="line"><button id="current">获取当前加速度</button></div> <div class="line"><button id="watch">监视加速度</button></div></div><script type="text/javascript" src="cordova.js"></script><script type="text/javascript" src="js/index.js"></script></body></html>
index.js:
var app = { initialize: function() { document.addEventListener('deviceready', this.onDeviceReady.bind(this), false); }, onDeviceReady: function() { var _this = this; document.getElementById("current").onclick = function(){ _this.getCurrentAcceleration(); } document.getElementById("watch").onclick = function(){ _this.getWatchAcceleration(); } }, // 获取当前加速度 getCurrentAcceleration: function(){ function onSuccess(acceleration) { console.log('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n'); } function onError() { console.log('onError!'); } navigator.accelerometer.getCurrentAcceleration(onSuccess, onError); }, // 监视加速度,每隔3秒输出当前加速度 getWatchAcceleration: function(){ function onSuccess(acceleration) { console.log('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n'); } function onError() { console.log('onError!'); } var options = { frequency: 3000 }; // Update every 3 seconds var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options); var timmer = setTimeout(function(){ navigator.accelerometer.clearWatch(watchID); },10000); }};app.initialize();
运行:
点击“获取当前加速度”,输出内容
点击“监视加速度”,每隔3秒会输出一个内容
- cordova device-motion插件
- ionic添加cordova插件-Device
- cordova device-orientation插件——罗盘
- cordova device插件——获取设备信息
- cordova device插件 无法在page load 时获取device.uuid 等信息
- Cordova插件
- cordova插件
- cordova插件
- Cordova学习——使用jquery框架+device设备插件完整过程
- Cordova-----3、Cordova使用插件
- Cordova-----4、Cordova自定义插件
- admob cordova插件 / cordova admob 插件 教程
- Cordova插件开发
- AngularJS Cordova插件介绍
- AngularJS Cordova插件介绍
- Cordova插件开发
- Cordova文件传输插件fileTransfer
- cordova 插件代码
- 部署zinnia的问题
- 剪切子文件夹下的文件到当前文件夹的bat文件
- 遇到org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.sql.
- javascript基础—事件兼容EventUtil.js
- java 下载文件功能实现
- cordova device-motion插件
- 程序员面试金典:
- 蛇形填数
- 系统调用的文件编程实现cp命令
- CentOS7 安装 Oracle11R2 报error in invoking target 'agent nmhs' 解决方法
- mysql Install/Remove of the Service Denied解决办法
- Android-真机应用安装失败原因
- Lucas定理推导过程(全网最全,哈哈哈哈)
- 解决myeclipse里面git的pull操作问题