Skip to content

v3.0.0: switch to devicemotion events

Compare
Choose a tag to compare
@mgh mgh released this 06 Mar 09:27
· 3 commits to master since this release

Summary

  • accelerometer plugin emits devicemotion events with same motion event api as an HTML5 browser on all platforms
  • fixes incompatibilities between native and mobile browsers, namely:
    • mobile Safari (iOS): acceleration values are inverted (relative to other platforms)
    • iOS native: acceleration values are scaled by -9.8 (relative to other platforms)
  • also exposes native APIs over window.addEventListener for compatibility (existing html5 browser code can run unmodified on native), but this is not recommended for new code since direct use of the accelerometer plugin API avoids browser differences

Usage

  • place the import at the top of your Application.js:

     import accelerometer;
    
  • use the devicemotion event:

     accelerometer.on('devicemotion', function (evt) {
        // these values should be identical on all devices
        evt.acceleration.x...
     });
    
  • (not recommended, but supported for compatibility) native DOM API window.addEventListener:

     window.addEventListener('devicemotion', function (evt) {
        // WARNING: these values will not be the same in all browsers.  Use the devkit
        // accelerometer api directly for consistent values.
        evt.acceleration.x...
     });
    
  • event objects have the following properties:

     evt.acceleration.x
     evt.acceleration.y
     evt.acceleration.z
     evt.accelerationIncludingGravity.x
     evt.accelerationIncludingGravity.y
     evt.accelerationIncludingGravity.z
     evt.rotationRate.alpha
     evt.rotationRate.beta
     evt.rotationRate.gamma