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)

circle-exclamation

Usage

Basic Orientation Display

Visual Device Representation

Compass Application

Spirit Level (Leveling Tool)

Tilt-Based Game

360° Photo Viewer

Parameters

throttle

Type: 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-50ms

  • Normal updates: 100ms

  • Battery saving: 200-500ms

Actions

None - This component automatically monitors device orientation when connected.

Targets

None

Events

pwa--device-orientation:updated

Dispatched 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

  1. Request permission on iOS: On iOS 13+, trigger permission request from a user gesture

  2. Use appropriate throttle: Balance between smoothness and performance

  3. Handle null values: Orientation values can be null on unsupported devices

  4. Test on real devices: Emulators don't accurately simulate device orientation

  5. Provide visual feedback: Show users how to interact with orientation-based features

  6. Consider battery impact: Continuous orientation monitoring drains battery

  7. Calibrate when needed: Allow users to reset/calibrate the initial orientation

  8. Handle screen rotation: Account for portrait/landscape mode changes

  9. Graceful degradation: Provide alternative controls for devices without sensors

  10. 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

Platform
Support

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)

circle-info

Device Orientation is primarily a mobile feature. Always test on real mobile devices with motion sensors for accurate results.

Last updated

Was this helpful?