Detecting Shaking In Html5 Mobile
I'm building a web app in html5 at the moment that needs to be able to detect when the user shakes their phone and how fast they are shaking it. I've been browsing around but can't
Solution 1:
shake.js looks to be a good library for this - it provides a custom 'shake' event and is supported on modern browsers. Here's an example setup from their documentation on how you could implement it:
<scriptsrc="shake.js"></script><script>var myShakeEvent = newShake({
threshold: 15, // optional shake strength thresholdtimeout: 1000// optional, determines the frequency of event generation
});
// Start listening to device motion
myShakeEvent.start();
// Register a shake event listener on window with your callbackwindow.addEventListener('shake', shakeEventDidOccur, false);
//function to call when shake occursfunctionshakeEventDidOccur () {
//put your own code here etc.alert('shake!');
}
</script>
Note that this does not work in some browsers when in an insecure browsing context (eg. http://
)
Post a Comment for "Detecting Shaking In Html5 Mobile"