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.
On iOS 13+ and some Android browsers, accessing motion sensors requires explicit user permission. The component automatically handles the permission request flow when necessary.
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
Always use the throttleValue parameter to limit the frequency of motion updates. Motion events can fire very frequently (up to 60Hz), which can impact performance if not throttled appropriately.
Permission Handling
On iOS 13+ and some Android devices, motion sensor access requires user permission. Always handle the permission flow gracefully and provide feedback to users about why you need access to motion sensors.
Coordinate System Understanding
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
Continuous motion sensor monitoring can drain battery. Consider:
Disabling motion tracking when the app is in the background
Using appropriate throttle values
Stopping tracking when not actively needed
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
unavailableFired when the Device Motion API is not supported by the device or browser.
permission-granted
permission-grantedFired when the user grants permission to access motion sensors. The motion listener is now attached and updated events will start firing.
permission-denied
permission-deniedFired when the user denies permission to access motion sensors, or an error occurs during the permission request.
updated
updatedFired on every device motion event (as delivered by the browser and subject to throttling). Contains detailed motion data.
Event detail structure:
Coordinate System:
acceleration: Device acceleration without gravity influenceaccelerationIncludingGravity: Total acceleration including Earth's gravity (9.81 m/s²)rotationRate: Angular velocity around each axis in degrees per secondinterval: Time interval between motion samples in milliseconds
Related Components
Device Orientation - Access device orientation angles (alpha, beta, gamma)
Geolocation - Track device position and movement
Touch - Handle touch gestures and interactions
Resources
Last updated
Was this helpful?