zoukankan      html  css  js  c++  java
  • Accelerometer

    Accelerometer
      采集设备在x、y、z方向上的动作。
    对象(只读):
    accelerometer.getCurrentAcceleration 
     
    返回当前沿x、y和z方向的加速度。
    复制
    navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
    
    说明:
    加速计是检测设备在当前方向上所做相对运动变化(增、减量)的运动传感器。加速计可以检测沿X、Y和Z轴的三维运动。
    加速度数据通过accelerometerSuccess回调函数返回。
    支持的平台:
      • Android
      • BlackBerry WebWorks (OS 5.0 and higher)
      • iPhone
      • Windows Phone 7 (Mango)
      • Bada 1.2 & 2.x
      • Tizen
    简单的范例:
     
    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);
    完整的范例:
    
    
     1 <!DOCTYPE html><html><head><title>Acceleration Example</title>
     2 
     3 <script type="text/javascript" charset="utf-8" src="phonegap.js"></script><script type="text/javascript" charset="utf-8">
     4 
     5         // 等待加载PhoneGap   
     6         document.addEventListener("deviceready", onDeviceReady, false); 
     7 
     8         // PhoneGap加载完毕
     9         function onDeviceReady() {
    10                 navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    11         }
    12 
    13         // onSuccess: 返回当前加速度数据的快照
    14         function onSuccess(acceleration) {
    15                 alert('Acceleration X: ' + acceleration.x + '\n' +
    16                         'Acceleration Y: ' + acceleration.y + '\n' +
    17                         'Acceleration Z: ' + acceleration.z + '\n' +
    18                         'Timestamp: '      + acceleration.timestamp + '\n');
    19         }
    20 
    21         // onError: 返回加速度数据失败
    22         function onError() {
    23                 alert('onError!');
    24         }
    25         
    26 </script></head><body>
    27         <h1>Example</h1>
    28         <p>getCurrentAcceleration</p></body></html>
    
    
    iPhone的特异情况:
    • iPhone没有获取在任何给定点当前加速度数据的概念。
    • 你必须通过给定时间间隔查看加速度并获得数据。
    • 因此,getCurrentAcceleration函数会返回从phoneGap watchAccelerometer调用开始后的最近一个返回值。
    accelerometer.watchAcceleration 
     
    在固定的时间间隔获取沿x、y和z轴的加速度。
    复制
    var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess,
                                                            accelerometerError,
                                                            [accelerometerOptions]);
    说明:
    加速计是检测设备在当前方向上所做相对运动变化(增、减量)的动作传感器。加速计可以检测沿X、Y和Z轴的三维运动。
    accelerometer.watchAcceleration 每隔固定时间就获取一次设备的当前加速度。每次取得加速度后,accelerometerSuccess回调函数会被执行。通过 acceleratorOptions对象的frequency参数可以设定以毫秒为单位的时间间隔。
    返回的watch id是加速度计监视周期的引用,可以通过accelerometer.clearWatch调用该watch ID以停止对加速度计的监视。
    支持的平台:
      • Android
      • BlackBerry WebWorks (OS 5.0 and higher)
      • iPhone
      • Windows Phone 7 (Mango)
      • Bada 1.2 & 2.x
      • Tizen
    简单的范例:
    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 };  // 每隔3秒更新一次
            
    var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    完整的范例:
     1 <!DOCTYPE html><html><head><title>Acceleration Example</title>
     2         
     3 <script type="text/javascript" charset="utf-8" src="phonegap.js"></script><script type="text/javascript" charset="utf-8">
     4 
     5         // watch id 是当前“watchAcceleration”的引用
     6         var watchID = null;
     7         
     8         // 等待加载PhoneGap
     9         document.addEventListener("deviceready", onDeviceReady, false);
    10         
    11         // PhoneGap加载完毕
    12         function onDeviceReady() {
    13                 startWatch();
    14         }
    15         
    16         // 开始监视加速度
    17         function startWatch() {
    18         
    19         // 每隔3秒钟更新一次加速度数据
    20         var options = { frequency: 3000 };
    21         
    22         watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    23         }
    24         
    25         // 停止监视加速度
    26         function stopWatch() {
    27                 if (watchID) {
    28                         navigator.accelerometer.clearWatch(watchID);
    29                         watchID = null;
    30                 }
    31         }
    32         
    33         // onSuccess: 获取当前加速度数据的快照
    34         function onSuccess(acceleration) {
    35                 var element = document.getElementById('accelerometer');
    36                 element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
    37                                                         'Acceleration Y: ' + acceleration.y + '<br />' +
    38                                                         'Acceleration Z: ' + acceleration.z + '<br />' +
    39                                                         'Timestamp: '      + acceleration.timestamp + '<br />';
    40         }
    41         
    42         // onError: 获取加速度失败
    43         function onError() {
    44                 alert('onError!');
    45         }
    46         
    47 </script></head><body>
    48         <div id="accelerometer">Waiting for accelerometer...</div></body></html>

    iPhone的特异情况:
    • 在请求的时间间隔,PhoneGap将调用success回调指向的函数,并传递加速度计数据。
    • 不过,PhoneGap将对设备的请求间隔时间限制为最小40ms,最大1000ms。
    • 例如,如果你设定每隔3秒(3000毫秒)请求一次,PhoneGap仍然每隔1秒请求一次设备,但是每隔3秒才调用一次success回调函数。
    accelerometer.clearWatch 
     
    停止watch ID参数指向的加速度监视。
    复制
    navigator.accelerometer.clearWatch(watchID);
    
    • watchID:由accelerometer.watchAcceleration返回的引用标识ID。
    支持的平台:
      • Android
      • BlackBerry WebWorks (OS 5.0 and higher)
      • iPhone
      • Windows Phone 7 (Mango)
      • Bada 1.2 & 2.x
      • Tizen
    简单的范例:
    复制
    var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    
    // ... 后续处理 ...
    
    navigator.accelerometer.clearWatch(watchID);
    完整的范例:
     1 <!DOCTYPE html><html><head><title>Acceleration Example</title>
     2 
     3 <script type="text/javascript" charset="utf-8" src="phonegap.js"></script><script type="text/javascript" charset="utf-8">
     4 
     5         // watch id 是当前“watchAcceleration”的引用
     6         var watchID = null;
     7         
     8         // 等待加载PhoneGap
     9         document.addEventListener("deviceready", onDeviceReady, false);
    10         
    11         // PhoneGap加载完毕
    12         function onDeviceReady() {
    13                 startWatch();
    14         }
    15         
    16         // 开始监视加速度
    17         function startWatch() {
    18         
    19                 // 每隔3秒钟更新一次加速度数据
    20                 var options = { frequency: 3000 };
    21                 watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    22         }
    23         
    24         // 停止监视加速度
    25         function stopWatch() {
    26                 if (watchID) {
    27                         navigator.accelerometer.clearWatch(watchID);
    28                         watchID = null;
    29                 }
    30         }
    31         
    32         // onSuccess: 获取当前加速度数据的快照
    33         function onSuccess(acceleration) {
    34                 var element = document.getElementById('accelerometer');
    35                 element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
    36                                                         'Acceleration Y: ' + acceleration.y + '<br />' +
    37                                                         'Acceleration Z: ' + acceleration.z + '<br />' +
    38                                                         'Timestamp: '      + acceleration.timestamp + '<br />';
    39         }
    40         
    41         // onError: 获取加速度失败
    42         function onError() {
    43                 alert('onError!');
    44         }
    45         
    46 </script></head><body>
    47         <div id="accelerometer">Waiting for accelerometer...</div>
    48         <button onclick="stopWatch();">Stop Watching</button></body></html>
    Acceleration 
     
    包含特定时间点采集到的加速计数据。
    属性:
    • x:在X轴的运动量,[0, 1]范围(数字类型)
    • y:在Y轴的运动量,[0, 1]范围(数字类型)
    • z:在Z轴的运动量,[0, 1]范围(数字类型)
    • timestamp:以毫秒为单位的创建时间戳。(DOMTimeStamp类型)
    说明:
    这个对象是由phoneGap创建和填充,并由Acce这个对象是由PhoneGap创建和填充,并由Accelerometer的方法返回。
    支持的平台:
      • Android
      • BlackBerry WebWorks (OS 5.0 and higher)
      • iOS
      • Windows Phone 7 (Mango)
      • Bada 1.2 & 2.x
      • webOS
      • Tizen
    简单的范例:
    复制
    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="phonegap.js"></script><script type="text/javascript" charset="utf-8">
            
            // 等待加载PhoneGap
            document.addEventListener("deviceready", onDeviceReady, false);
            
            // PhoneGap加载完毕
            function onDeviceReady() {
                    navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
            }
            
            // onSuccess: 获得加速度数据的快照
            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 onError() {
                    alert('onError!');
            }
            
    </script></head><body>
            <h1>Example</h1>
            <p>getCurrentAcceleration</p></body></html>
    accelerometerSuccess 
     
    提供加速度信息的onSuccess回调函数。
    复制
    function(acceleration) {
       // 进一步处理}
    
    参数:
    • acceleration: 在某一时刻的加速度(Acacceleration:在某一时刻的加速度。(Acceleration对象类型)
     
    function onSuccess(acceleration) {
            alert('Acceleration X: ' + acceleration.x + '\n' +
            'Acceleration Y: ' + acceleration.y + '\n' +
            'Acceleration Z: ' + acceleration.z + '\n' +
            'Timestamp: '      + acceleration.timestamp + '\n');
    }
    accelerometerError
     
    加速度方法的onError回调函数。
    复制
    function() {
       // 错误处理
    }
    accelerometerOptions 
     
    定制检索加速度计的可选参数。
    选项:
    • frequency:多少毫秒获取一次Acceleration。(数字类型)(默认值:10000)
  • 相关阅读:
    进程、线程和协程的区别(转)
    IO多路复用机制(转)
    防火墙及其功能(转)
    TCP连接的建立和终止。
    TCP和UDP细致刻画,区别。
    typename T和class T区别与联系
    TCP UDP的详解开始 ----UNIX网络编程
    关于UNIX网络编程的的OSI,1.7章的总结
    UNIX网络编程daytime服务端和客户端的实现过程
    linux shell脚本执行错误:bad substitution
  • 原文地址:https://www.cnblogs.com/qingkong/p/2835263.html
Copyright © 2011-2022 走看看