Detect the shake of the phone in JavaScript

0

Hello, I have a question, I have a small mobile Internet provider and I create a game in html and javascript that the user shaking the phone can receive letters that later can be exchanged over the Internet. I already know how to capture the movement of the mobile phone:

window.addEventListener("deviceorientation", handleOrientation, true);
    function handleOrientation(event) {
        var absolute = event.absolute;
        var alpha    = event.alpha;
        var beta     = event.beta;
        var gamma    = event.gamma;
}

But I do not know how to actually develop the algorithm that detects shaking up and down the device and ignores the rest of the moves! Can someone help me?

    
asked by anonymous 05.09.2018 / 21:13

1 answer

0

Friend, see if this example below helps you.

<script type="text/javascript" src="https://cdn.rawgit.com/alexgibson/shake.js/master/shake.js"></script><script>//LereventoChacoalharvarshakeEvent=newShake({threshold:15});shakeEvent.start();window.addEventListener('shake',function(){alert("Chacoalhado.");
    }, false);

    //Parar Evento Chacoalhar
    function stopShake(){
        shakeEvent.stop();
    }

    //Verifique se o agitar é suportado ou não.
    if(!("ondevicemotion" in window)){alert("Não suportado.");}
</script>
    
05.09.2018 / 21:43