Skip to content

dandv/meteor-shake

Repository files navigation

Cross-platform shake detection for Meteor

This package brings together two shake detection plugins:

Curiously, the Cordova plugin isn't aligned with the W3C spec - a known issue.

Demo

Usage

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();

Debouncing

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.

Difference in sensitivities on Cordova vs. mobile browsers

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.

Supported environments

  • Android (tested with 4.4 on Samsung Galaxy S5)
  • Android Chrome and Opera
  • iOS Safari
  • iOS (tested with iPhone 5)

Unsupported environments

  • Xcode simulator (no idea why)
  • Android emulator - allegedly the emulator doesn't support the accelerometer, but that's dubious

License and author

Copyright (C) 2015 Dan Dascalescu.

License: MIT.