指南针方向-多次旋转

时间:2018-10-17 15:46:54

标签: javascript math compass

我目前正在制作一个网络应用程序的原型,该应用程序使用浏览器中的指南针标题。取值范围是0〜360。

每当我旋转一圈,该值就会从360跳回0。 我不知道如何在第二次旋转时将其从360增加到例如720。

我缺少什么数学魔术?

谢谢!

这是我的代码

        if ('ondeviceorientationabsolute' in window) {
            window.addEventListener('deviceorientationabsolute', handleOrientationAbsolute);
        } else if ('ondeviceorientation' in window) {
            window.addEventListener('deviceorientation', handleOrientation);
        }

        function handleOrientationAbsolute(e) {
            alpha = Math.round(360 - e.alpha);
            update();
        }

        function handleOrientation(e) {
            alpha = Math.round(e.webkitCompassHeading);
            update();
        }


        function update() {
            headingEl.innerHTML =  alpha; //This value should be able to return higher than 360
        }

1 个答案:

答案 0 :(得分:1)

我会从Mario Kart那里窃取技术。

在您的圈子周围具有某些阈值。您需要将圈子至少分为3组。 (假设您使用的是120°点。)存储您所在的组(例如012),并经常检查哪个组分组。如果您在01之间或在12之间进行更改,则只需将其存储。但是,如果您在20之间切换,请增加或减少计数器。

这将使您返回大于360°的值。

相关问题