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.
Usage
Basic Motion Detection
Shake Detection
Tilt-Based UI Control
Motion-Based Game Controller
Best Practices
Performance Optimization
Permission Handling
Coordinate System Understanding
Battery Consideration
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:
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?