Detect mobile phone shake in Javascript

Asked

Viewed 48 times

0

Hello, I have a question, I have a small Mobile Internet provider and I create create a game in html and javascript that the user to shake the phone can receive letters that later can exchange for Internet. I already know how to capture cell phone movement:

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 don’t know how to really develop the algorithm that detects the shake up and down of the device and ignore the rest of the moves! Can someone help me?

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>  
    //Ler evento Chacoalhar
    var shakeEvent = new Shake({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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.