How DeviceMotionEvent Reads Sensor Data in JavaScript

This article provides an overview of the JavaScript DeviceMotionEvent API, explaining how web browsers interface with device hardware to capture physical motion. You will learn how underlying Micro-Electro-Mechanical Systems (MEMS) sensors deliver real-time data on acceleration and rotational rates, how the browser processes these signals, and how to access this data securely in modern web applications.


The Underlying Hardware: MEMS Sensors

Mobile devices, tablets, and modern laptops contain tiny physical sensors built using Micro-Electro-Mechanical Systems (MEMS) technology:

  1. Accelerometers: Measure linear acceleration along three physical axes (\(X\), \(Y\), and \(Z\)). They detect changes in velocity as well as the constant pull of Earth’s gravity.
  2. Gyroscopes: Measure angular velocity, or the rate of rotation around the \(X\), \(Y\), and \(Z\) axes.

When a device moves, microscopic mechanical structures within these chips shift, altering electrical properties such as capacitance. The sensor’s onboard controller translates these physical changes into digital signals representing force and speed.


How the Browser Bridges Hardware to JavaScript

The operating system reads raw digital data from the device’s sensor drivers at regular intervals. The web browser queries the OS-level sensor APIs, normalizes the readings into standardized physical units, and packages them into a DeviceMotionEvent object dispatched on the global window object.


Key Properties of DeviceMotionEvent

When a devicemotion event fires, the event object contains four primary properties:


Basic Implementation

To capture sensor data, register an event listener on the window object:

window.addEventListener('devicemotion', (event) => {
  // Acceleration without gravity
  const { x, y, z } = event.acceleration;

  // Total acceleration including gravity (determines static tilt)
  const totalX = event.accelerationIncludingGravity.x;
  const totalY = event.accelerationIncludingGravity.y;
  const totalZ = event.accelerationIncludingGravity.z;

  // Rate of rotation (dynamic tilt changes)
  const { alpha, beta, gamma } = event.rotationRate;

  // Sensor sampling rate
  const interval = event.interval;
});

Permissions and Security Requirements

Because motion data can be used for device fingerprinting or user tracking, modern browsers enforce strict security rules:

  1. Secure Contexts (HTTPS): The DeviceMotionEvent API only works in secure contexts (https:// or localhost).
  2. Explicit User Permission: Operating systems like iOS require an explicit user gesture (such as clicking a button) to grant permission before sensor data can be streamed.
async function requestMotionAccess() {
  if (typeof DeviceMotionEvent.requestPermission === 'function') {
    const permissionState = await DeviceMotionEvent.requestPermission();
    if (permissionState === 'granted') {
      window.addEventListener('devicemotion', handleMotion);
    }
  } else {
    // Non-iOS 13+ devices typically do not require explicit permission prompts
    window.addEventListener('devicemotion', handleMotion);
  }
}

function handleMotion(event) {
  // Process sensor readings
}