Device Motion

The Device Motion component provides access to motion sensor data from your device's accelerometer and gyroscope. This component listens to devicemotion events from the Device Motion API and emits normalized update events containing acceleration, acceleration including gravity, rotation rate, and sampling interval. It automatically manages the permission flow when required by the device.

This component is particularly useful for:

  • Creating motion-controlled games and interactive experiences

  • Implementing shake-to-undo or shake-to-refresh features

  • Building fitness and activity tracking applications

  • Detecting device orientation changes for UI adaptation

  • Creating augmented reality experiences

  • Monitoring device movement for security purposes

  • Building immersive storytelling and multimedia applications

  • Implementing gesture-based navigation and controls

Browser Support

The Device Motion API is part of the Generic Sensor API family and relies on the devicemotion event which is widely supported across modern browsers.

Support level: Good - Available on most modern browsers on mobile devices and laptops with accelerometers/gyroscopes.

circle-exclamation
circle-info

Desktop devices without built-in accelerometers or gyroscopes will not be able to use this API. The component will emit an unavailable event in such cases.

Usage

Basic Motion Detection

Shake Detection

Tilt-Based UI Control

Motion-Based Game Controller

Best Practices

Performance Optimization

circle-check

Permission Handling

circle-exclamation

Coordinate System Understanding

circle-info

The Device Motion API uses a standard coordinate system:

  • X-axis: Points to the right of the device

  • Y-axis: Points to the top of the device

  • Z-axis: Points up and out of the screen

Understanding this coordinate system is crucial for implementing motion-based features correctly.

Battery Consideration

circle-exclamation

Null Value Handling

Common Use Cases

1. Shake to Undo/Refresh

Implement a shake gesture to undo actions or refresh content, similar to many mobile apps.

2. Step Counter

Track device movement to count steps (simplified implementation).

3. Orientation-Adaptive Content

Display different content or adjust UI based on how the user is holding their device.

4. Gesture-Based Navigation

Navigate through content using tilt gestures.

API Reference

Parameters

throttleValue: Controls how often updated events are dispatched to avoid excessive UI updates. Value in milliseconds. Default varies by implementation.

Actions

None

Targets

None

Events

unavailable

Fired when the Device Motion API is not supported by the device or browser.

permission-granted

Fired when the user grants permission to access motion sensors. The motion listener is now attached and updated events will start firing.

permission-denied

Fired when the user denies permission to access motion sensors, or an error occurs during the permission request.

updated

Fired on every device motion event (as delivered by the browser and subject to throttling). Contains detailed motion data.

Event detail structure:

circle-info

Coordinate System:

  • acceleration: Device acceleration without gravity influence

  • accelerationIncludingGravity: Total acceleration including Earth's gravity (9.81 m/s²)

  • rotationRate: Angular velocity around each axis in degrees per second

  • interval: Time interval between motion samples in milliseconds

Resources

Last updated

Was this helpful?