2017-07-07 65 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座標之上構建的自定義邏輯來跟蹤設備旋轉計數。在插件中沒有可用的現成的解決方案還沒有,據我檢查