Shake

This API enables the user to register when the mobile device is shaken.

Shake counts discrete shake motions. The shake gesture is defined by translating the device back-and-forth with a rapid, jerky movement.

Shake relies on the device's Accelerometer sensor.

Example

var shake = new MotionStack.Shake({
  direction: "all"
});

shake.start(function (e) {
  console.log(e.shakeCount);

  if (e.shakeCount === 10) {
    shake.stop();
  }
});

Constructor

new MotionStack.Shake([options])

Create a new Shake instance object.

Argument Type Description
options Object Options to pass in for the new Shake instance (optional)

  • minStrength {Number}
  • Specifies the minimum strength of a shake required to be registered.


    Default: 0

  • direction {String}
  • Specifies the direction of a shake required to be registered.


    Default: all

    • all - All shakes are detected.
    • forward - Only forward shakes are detected.
    • side - Only side shakes are detected.
  • fireOnAnimationFrame {Boolean}
  • Only emit the latest event within requestAnimationFrame intervals.


    Default: false

var shake = new MotionStack.Shake({
  direction: "all",
  minStrength: 0
});

Instance Methods

start(callback, [context])

Start receiving shake updates.

Argument Type Description
callback Function Callback function that receives an object as an argument

  • function(e)
  • e.shakeCount {Number}
    Total number of shakes detected since the shake instance has been started.
  • e.direction {Number}
    The direction of the detected shake is either:
    • Equal to shake.DIRECTION_FORWARD = 1
    • Equal to shake.DIRECTION_SIDE = 2
  • e.strength {Number}
    The strength of the detected shake in meters per seconds squared.
  • e.timeStamp {Number}
    The time in milliseconds when the shake event was detected.
context Object Context in which to invoke the callback (optional).
shake.start(function(e) {
  console.log(e.shakeCount);
});

stop()

Stop shake updates.

shake.stop();

Source

src/interfaces/Shake.js