Polyfill for Generic Sensor API
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


W3C Generic Sensor API polyfills

This is a polyfill for Generic Sensor-based motions sensors to make migration from the old DeviceOrientationEvent/DeviceMotionEvent to the new APIs a smoother experience.

In particular, this polyfill will allow the users of modern browsers to get a feel of the new API shape before it ships more broadly. Chrome 63 has a native implementation behind a flag and starting Chrome 67 the feature is enabled by default.

src/motion-sensors.js implements the following interfaces:

src/geolocation.js implements the following interface:

How to use the polyfill

<script type="module">
// Import the objects you need.
import {
} from './src/motion-sensors.js';

// And they're ready for use!
let gyroscope = new Gyroscope({ frequency: 15 });
let orientation = new AbsoluteOrientationSensor({ frequency: 60 });

How to enable the native implementation in Chrome

Chrome 67 or later: the native implementation is enabled by default.

Chrome 63-66: the native implementation is behind the following feature flags:

Generic Sensor (chrome://flags/#enable-generic-sensor):

  • Accelerometer
  • Gyroscope
  • LinearAccelerationSensor
  • AbsoluteOrientationSensor
  • RelativeOrientationSensor

Generic Sensor Extra Classes (chrome://flags/#enable-generic-sensor-extra-classes):

  • AmbientLightSensor
  • Magnetometer

Test suite

Run web-platform-tests with this polyfill enabled here.

Known issues

  • GravitySensor and LinearAccelerationSensor polyfills do not work on Android with Pixel 2, since DeviceMotionEvent.acceleration returns only null values, see Chromium bug 796518.
  • AbsoluteOrientationSensor on iOS uses non-standard webkitCompassHeading that reports wrong readings if the device is held in its portrait-secondary orientation. Specifically, the webkitCompassHeading flips by 180 degrees when tilted only slightly.

Learn more

Reporting a security issue

If you have information about a security issue or vulnerability with an Intel-maintained open source project on https://github.com/intel, please send an e-mail to secure-opensource@intel.com. Encrypt sensitive information using our PGP public key. For issues related to Intel products, please visit https://security-center.intel.com.