我想用Cordova检测实时设备旋转(不是方向,旋转度数)。我使用了设备运动,但我得到了一堆加速度x,y和z的值,我如何从这些值中计算旋转?
我想要检测一个完整的360度旋转(想象一下把设备放在棍子或钟摆上,然后敲击它,让它摆动)。
谢谢!
发布于 2017-07-14 21:09:59
您可以通过安装甘地在评论中提到的plugin来使用The Screen Orientation API。
cordova plugin add cordova-plugin-screen-orientation
然后,您可以使用事件来检测deviceReady之后的方向更改;
window.addEventListener('orientationchange', function(){
console.log(screen.orientation.type); // e.g. portrait
});或
screen.orientation.onchange = function({
console.log(screen.orientation.type);
});老实说,你应该可以在没有插件的情况下做到这一点,但添加cordova插件会更安全。
因为你想检测360度的旋转,这不包括在内,你需要自己数一数…类似于…的东西;
var startingPoint = 0;
window.addEventListener('orientationchange', monitorOrientation);
function monitorOrientation(e) {
console.log('Device orientation is: ', e. orientation);
if(e. orientation == 180 || e.orientation == -180) {
// You can extend this or remove it completely and increment the variable as you wish i.e. 4x90 = 360
startingPoint += 180;
}
if(startingPoint == 360) {
// Do whatever you want and reset starting point back to 0
startingPoint = 0;
}
}发布于 2017-07-20 22:06:09
也许你正在寻找的是指南针:cordova-plugin-device-orientation
发布于 2017-07-21 18:43:29
Device orientation plugin以度数的形式为您提供设备旋转详细信息。
但如果你正在寻找检测设备运动的插件,device motion是我能看到的唯一接近这一要求的插件。但是应该使用需要在x,y,z坐标之上构建的自定义逻辑来跟踪设备旋转计数。据我所知,在插件中还没有现成的解决方案
https://stackoverflow.com/questions/44968469
复制相似问题