当前位置:K88软件开发文章中心编程语言APP编程Cordova → 文章内容

Cordova 加速计

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:49:08

由 bjcl 创建,youj 最后一次修改 2016-12-26 该插件也称为 设备运动 。它用于在三维中跟踪设备运动。步骤1 - 安装加速计插件我们将使用 cordova-CLI 安装此插件。键入以下代码到命令提示符窗口。C:\Users\username\Desktop\CordovaProject>cordova plugin add cordova-plugin-device-motion步骤2 - 添加按钮接下来,我们需要做的是在 index.html 文件中添加两个按钮。一个用于获取当前加速度,另一个将监视加速度变化。<button id = "getAcceleration">GET ACCELERATION</button><button id = "watchAcceleration">WATCH ACCELERATION</button>步骤3 - 添加事件监听器让我们将按钮的事件监听器添加到 index.js 中的 onDeviceReady 函数。document.getElementById("getAcceleration").addEventListener("click", getAcceleration);document.getElementById("watchAcceleration").addEventListener("click", watchAcceleration);步骤4 - 创建函数我们将创建两个函数。第一个将用于获取当前加速度,第二个将观察加速度,并且每三秒通知一次。我们还添加了由 setTimeout 函数包装的 clearWatch ,以在指定时间范围后停止观看加速。frequency 参数用于每三秒触发一次回调函数。function getAcceleration(){ navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError); function accelerometerSuccess(acceleration) { alert('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n'); }; function accelerometerError() { alert('onError!'); };}function watchAcceleration(){ var accelerometerOptions = { frequency: 3000 } var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess, accelerometerError, accelerometerOptions); function accelerometerSuccess(acceleration) { alert('Acceleration X: ' + acceleration.x + '\n' + 'Acceleration Y: ' + acceleration.y + '\n' + 'Acceleration Z: ' + acceleration.z + '\n' + 'Timestamp: ' + acceleration.timestamp + '\n'); setTimeout(function() { navigator.accelerometer.clearWatch(watchID); }, 10000); }; function accelerometerError() { alert('onError!'); };}现在,如果我们按 GET ACCELERATION 按钮,我们将获得当前的加速度值。如果我们按WATCH ACCELERATION,警报将每三秒触发一次。在显示第三个警告后,将调用 clearWatch 函数,并且我们不会再收到任何警报,因为我们将超时设置为10000毫秒。

Cordova 加速计