2017-07-07 136 views
2

我想用Cordova检测活动设备旋转(不是方向,旋转度)。我用设备的运动,但我得到了一堆加速度x y和z值,我该如何计算旋转?如何检测科尔多瓦的设备旋转?

我想检测完整的360度旋转(想像把装置上的棒[或摆],然后打它,以便其摆动)。

谢谢!

+0

这个插件https://github.com/apache/cordova-plugin-device-orientation为您提供了用度设备旋转细节。这是你在找什么? – Gandhi

+0

嗨@Ghandi,请看看我留给Devrim的评论。 – trueicecold

+0

现在明白了。仍然不确定任何其他设备运动插件可用...将通过 – Gandhi

回答

1

您可以通过在评论中安装甘地提到的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); 
}); 

你应该能够做到这一点没有插件是诚实的,但将是更安全的,添加科尔多瓦插件。

当你要检测转动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; 
    } 
} 
+0

谢谢@Devrim,但设备根本不旋转。它站立。想象一下,在风景模式下,该设备粘在一根棍子上,然后旋转棍子。在这种情况下,方向不会触发,这就是我正在寻找的情况。 – trueicecold

+0

嗯我不确定这是诚实的@trueicecold。那么我想你正在寻找设备运动或传感器工具。玩这个插件https://github.com/apache/cordova-plugin-device-motion –

+0

是的,我看到了它,但我不知道如何正常化x,y,z来检测360度全方位摆动。 .. – trueicecold

0

Device orientation plugin为您提供了用度设备旋转细节。

但是,如果你正在寻找检测装置的运动,device motion是唯一的插件,我可以看到这是接近这个要求。但是应该使用需要在x,y,z坐标之上构建的自定义逻辑来跟踪设备旋转计数。在插件中没有可用的现成的解决方案还没有,据我检查