The easiest way to convert orientation data to a 3D transform is basically to use the alpha, gamma, and beta values as rotateZ
, rotateX
and rotateY
values.
It is important to keep in mind, however, that the Device Orientation coordinate system is different from the CSS coordinate system. Namely, the former is right-handed and its Y axis is positive upwards, while the latter is a left-handed coordinate system whose Y axis is positive to the bottom. Furthermore, the Device Orientation angle rotations should always be done in a Z - X' - Y'' order that does not match the order of some CSS Transforms. These are some of the practical consequences of these differences:
Here's a simple code snippet to sum it up:
js
const elem = document.getElementById("view3d"); window.addEventListener("deviceorientation", (e) => { elem.style.transform = `rotateZ(${-e.alpha}deg) rotateX(${-e.beta}deg) rotateY(${ e.gamma }deg)`; });
Should you ever need to convert a rotate3d axis-angle to orientation Euler angles, you can use the following algorithm:
js
// convert a rotate3d axis-angle to deviceorientation angles function orient(aa) { const x = aa.x, y = aa.y, z = aa.z, a = aa.a, c = Math.cos(aa.a), s = Math.sin(aa.a), t = 1 - c, // axis-angle to rotation matrix rm00 = c + x * x * t, rm10 = z * s + y * x * t, rm20 = -y * s + z * x * t, rm01 = -z * s + x * y * t, rm11 = c + y * y * t, rm21 = x * s + z * y * t, rm02 = y * s + x * z * t, rm12 = -x * s + y * z * t, rm22 = c + z * z * t, TO_DEG = 180 / Math.PI, ea = [], n = Math.hypot(rm22, rm20); // rotation matrix to Euler angles ea[1] = Math.atan2(-rm21, n); if (n > 0.001) { ea[0] = Math.atan2(rm01, rm11); ea[2] = Math.atan2(rm20, rm22); } else { ea[0] = 0; ea[2] = (rm21 > 0 ? 1 : -1) * Math.atan2(-rm10, rm00); } return { alpha: -ea[0] * TO_DEG - 180, beta: -ea[1] * TO_DEG, gamma: ea[2] * TO_DEG, }; }
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Device_orientation_events/Using_device_orientation_with_3D_transforms