Device Orientation
The Device Orientation component provides access to the Device Orientation API, allowing your application to detect and respond to changes in the physical orientation of a mobile device or tablet. This enables creating immersive experiences that react to how users hold and move their devices.
This component is particularly useful for:
Gaming experiences with tilt-based controls
Augmented reality applications
Compass and navigation apps
360° photo/video viewers
Virtual reality experiences
Interactive art installations
Balance and leveling tools
Educational simulations requiring device movement
Gesture-based interfaces
Browser Support
The Device Orientation API is primarily available on mobile devices (smartphones and tablets) with motion sensors. Desktop browsers typically don't support this API as most computers lack the necessary sensors.
Supported Platforms:
iOS Safari: Full support (requires permission on iOS 13+)
Android Chrome/Edge: Full support
Android Firefox: Full support
Desktop browsers: Limited support (requires device with sensors)
On iOS 13+, user permission is required and must be requested through a user gesture. The component handles this automatically but you need to trigger the initial action from a user interaction.
Usage
Basic Orientation Display
Visual Device Representation
Compass Application
Spirit Level (Leveling Tool)
Tilt-Based Game
360° Photo Viewer
Parameters
throttle
throttleType: number (milliseconds) Default: Inherited from controller configuration
Controls how often the updated event is dispatched. This prevents overwhelming the UI with too many updates.
Recommended values:
Smooth visualization:
30-50msNormal updates:
100msBattery saving:
200-500ms
Actions
None - This component automatically monitors device orientation when connected.
Targets
None
Events
pwa--device-orientation:updated
pwa--device-orientation:updatedDispatched when the device orientation changes (subject to throttle delay).
Payload:
alpha(number|null): Rotation around Z-axis in degrees (0-360°)Represents compass direction
0° = North, 90° = East, 180° = South, 270° = West
beta(number|null): Rotation around X-axis in degrees (-180 to 180°)Represents front-to-back tilt
0° = device lying flat
Positive = tilting forward
Negative = tilting backward
gamma(number|null): Rotation around Y-axis in degrees (-90 to 90°)Represents left-to-right tilt
0° = device upright
Positive = tilting right
Negative = tilting left
absolute(boolean): Whether the orientation is provided relative to Earth's coordinate system
Example:
Best Practices
Request permission on iOS: On iOS 13+, trigger permission request from a user gesture
Use appropriate throttle: Balance between smoothness and performance
Handle null values: Orientation values can be null on unsupported devices
Test on real devices: Emulators don't accurately simulate device orientation
Provide visual feedback: Show users how to interact with orientation-based features
Consider battery impact: Continuous orientation monitoring drains battery
Calibrate when needed: Allow users to reset/calibrate the initial orientation
Handle screen rotation: Account for portrait/landscape mode changes
Graceful degradation: Provide alternative controls for devices without sensors
Inform users: Explain that orientation features require device movement
Understanding Orientation Axes
Coordinate System
Practical Examples
iOS Permission Handling
On iOS 13+, you must request permission before accessing device orientation:
Complete Example: AR Pointer
Troubleshooting
No orientation data on desktop
Issue: Events don't fire on desktop browsers
Cause: Most desktops lack motion sensors
Solution: Test on actual mobile devices
Permission denied on iOS
Issue: iOS doesn't provide orientation data
Cause: Permission not granted or not requested from user gesture
Solution:
Erratic readings
Issue: Orientation values jump around
Causes:
Magnetic interference
Poor sensor calibration
Too low throttle value
Solutions:
Increase throttle value
Apply smoothing/averaging
Calibrate device sensors
Wrong orientation in landscape
Issue: Values don't match device orientation in landscape mode
Solution: Adjust calculations based on screen orientation:
Browser Compatibility
iOS Safari
✓ Full (requires permission on iOS 13+)
Android Chrome
✓ Full support
Android Firefox
✓ Full support
Desktop Chrome
△ Limited (requires sensors)
Desktop Firefox
△ Limited (requires sensors)
Desktop Safari
△ Limited (requires sensors)
Device Orientation is primarily a mobile feature. Always test on real mobile devices with motion sensors for accurate results.
Last updated
Was this helpful?