cordova device-motion插件

版权声明:本文为博主原创文章,转载务必注明出处,http://blog.csdn.net/michael_ouyang。 https://blog.csdn.net/michael_ouyang/article/details/76036239


介绍

这个插件提供了访问设备的加速度计。加速度计是一个运动传感器检测到的变化(三维维度)在相对运动的当前设备的方向,在三个维度上沿XY,和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

拿到当前加速度XY,和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 特性

XYZ运动的值都是随机生成的,以模拟加速度计

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);

· watchIDnavigator.accelerometer.watchAcceleration返回的ID

Example

var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

 

// ... later on ...

 

navigator.accelerometer.clearWatch(watchID);

对象Objects

· Acceleration

 加速度Acceleration

包含加速度计在特定时间点捕获的数据。加速度值包括重力的影响(9.81/^ 2),所以当一个平和面对,XY,和Z返回值应该是00,和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秒会输出一个内容



猜你喜欢

转载自blog.csdn.net/michael_ouyang/article/details/76036239
今日推荐