javascript - Find if device is tilted left or right - Stack Overflow

I would like to find if a device, in landscape mode, is tilted left, right or not at all. Example: Not

I would like to find if a device, in landscape mode, is tilted left, right or not at all. Example:

Not at all:

Left:

Right:

I have looked at the deviceorientation API but found no clear way to find none, left or right. I would prefer to have it considered none if the phone is tilted only a small amount. It would be awesome if I could get a number for the rotation, positive if left, negative if right, 0 if none, but this is not required. I would also like a answer that has the most browser support possible.

I would like to find if a device, in landscape mode, is tilted left, right or not at all. Example:

Not at all:

Left:

Right:

I have looked at the deviceorientation API but found no clear way to find none, left or right. I would prefer to have it considered none if the phone is tilted only a small amount. It would be awesome if I could get a number for the rotation, positive if left, negative if right, 0 if none, but this is not required. I would also like a answer that has the most browser support possible.

Share Improve this question asked Mar 12, 2017 at 3:00 user7586097user7586097
Add a ment  | 

1 Answer 1

Reset to default 9

The section "4.1 deviceorientation Event" of the DeviceOrientation Eventdraft says that

Thus the angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''.

AFAIU, what you want is convert that to Z-X'-Z'' instrinsic Euler angels and get the angle that represents the last rotation. Well, let's dust off 3D trigonometric skills and with help from quite useful table in the Wikipedia we can see that

spin = atan2(cos(beta)*sin(gamma), sin(beta))

or if we put it into some JS code

window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
    // those angles are in degrees
    var alpha = event.alpha;  
    var beta = event.beta;
    var gamma = event.gamma;

    // JS math works in radians
    var betaR = beta / 180 * Math.PI;
    var gammaR = gamma / 180 * Math.PI;
    var spinR = Math.atan2(Math.cos(betaR) * Math.sin(gammaR), Math.sin(betaR));

    // convert back to degrees
    var spin = spinR * 180 / Math.PI;
}

Note that calculated as such spin is 0 when the device is in natural portrait orientation. And your landscape example corresponds to -90 degrees.

Beware also that Orientation and motion data explained MDN page says that

Warning: Currently, Firefox and Chrome do not handle these coordinates the same way. Take heed when using them.

I tested my code only on iOS in Chrome. You can see a live demo at https://plnkr.co/edit/78IAUlbkQZMmogdlxGAH?p=preview. Note that you need to click on an icon "Launch the preview in a separate window" in the top-right of the preview area or it won't work because access to the deviceorientation API from iframe is prohibit at least on iOS.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744987444a4604694.html

相关推荐

  • javascript - Find if device is tilted left or right - Stack Overflow

    I would like to find if a device, in landscape mode, is tilted left, right or not at all. Example: Not

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信