w3c / deviceorientation

W3C Device Orientation spec
https://www.w3.org/TR/orientation-event/
Other
49 stars 32 forks source link

Guidance needed: how to acquire compass headings in a future-compatible manner? #137

Open juj opened 5 months ago

juj commented 5 months ago

Today there exists an event deviceorientationabsolute that can be used to register to device orientation events that are relative to the magnetic north pole (for 1D compass heading calculations)

This event is available on other browsers except Safari.

On Safari window.addEventListener('deviceorientationabsolute', ...) does not work, but instead one must first call DeviceOrientationEvent.requestPermission() and after that Promise resolves to a success, register to the window.addEventListener('deviceorientation', ...) event, and use DeviceOrientationEvent.webkitCompassHeading to acquire the 1D compass heading direction.

In other browsers today, I understand that one does not need to call DeviceOrientationEvent.requestPermission() before registering a deviceorientationabsolute event handler, though I presume that since the function is in the spec, that in the future this may also be required.

But this gives a problem: what kind of code should one write in order to precisely register to get 1D compass heading directions in a manner that will be forward-compatible to future Firefox, Chrome and Safari browsers?

We can feature test whether DeviceOrientationEvent.requestPermission() exists and call it before registering a handler, but how does one know precisely whether to register 'deviceorientation' or 'deviceorientationabsolute' events?

anssiko commented 4 months ago

Summoning @marcoscaceres

Discussed in https://www.w3.org/2024/02/12-dap-minutes.html#t10

marcoscaceres commented 2 months ago

Just noting that registering a listener shouldn't have any side effects (i.e., it doesn't automatically enable the machinery to start firing the events).

One would still need to request permission.

In WebKit specifically, we don't implement "deviceorientationabsolute" it seems.

It seems Gecko does... but I haven't confirmed how it works there.

@rakuco, I saw you took an action to look into what Gecko does... could you still do that? I'd like to get a complete picture of what each browser is doing and then we can look at aligning everything in the spec.

reillyeon commented 1 month ago

@marcoscaceres, does WebKit have any objections to implementing deviceorientationabsolute? I'm not familiar with the history behind why WebKit decided to add a separate attribute for compass heading. The code was added in https://bugs.webkit.org/show_bug.cgi?id=125646 which upstreamed a number of iOS-specific changes and added webkitCompassHeading in parallel to the existing absolute attribute.