This package brings together two shake detection plugins:
- Alex Gibson's shake.js, which uses the devicemotion W3C API implemented by mobile browsers
- Lee Crossley's Cordova shake detection plugin,
using the apache.cordova.device-motion
plugin, which exposes the
navigator.accelerometer
API.
Curiously, the Cordova plugin isn't aligned with the W3C spec - a known issue.
- http://shaker.meteor.com
- see the example
Since the shake.js API is needlessly complicated, this package will let you use a simple and isomorphic (Cordova or mobile browsers) API almost identical to that of Lee Crossley's package:
// sensitivity will be passed to Cordova or as `threshold` to shake.js
shake.startWatch(callback, sensitivity);
or
// passed as-is to shake.js - won't work for Cordova!
shake.startWatch(callback, optionsObject);
To stop listening for shakes,
shake.stopWatch();
Note that on both Cordova and in mobile browsers, shakes are debounced, so that at least 750ms must pass between two shakes, in order for separate shakes to register.
Debouncing is a technique by which a repeatedly occurring event is handled only once, after a number of miliseconds have passed since the event last happened. It's commonly used to handle resize events and perform complex resizing calculations only after the user appears to have stopped resizing. Without debouncing, Cordova would fire the shake callback twice by the time the user has stopped shaking, or even three times.
Mobile browsers and Cordova use different APIs, as explained in the introduction. A sensitivity of 25 is too much for mobile browsers, while 30 is the default for Cordova and 40 is given as an example. A value of "15" fortunately isn't too low for Cordova and seems to work well on all supported platforms.
- Android (tested with 4.4 on Samsung Galaxy S5)
- Android Chrome and Opera
- iOS Safari
- iOS (tested with iPhone 5)
- Xcode simulator (no idea why)
- Android emulator - allegedly the emulator doesn't support the accelerometer, but that's dubious
Copyright (C) 2015 Dan Dascalescu.
License: MIT.