简体   繁体   English

确定JavaScript在移动设备上哪个方向朝下?

[英]Determine which direction is down on mobile device in javascript?

I am creating a tool using Box2D and wanted to see if there was a way to match the direction of the real force of gravity acting on a mobile device. 我正在使用Box2D创建工具,希望查看是否有一种方法可以匹配作用在移动设备上的真实重力的方向。

I've got this completely working on iPad by binding to the devicemotion event. 通过绑定到devicemotion事件,我已经在iPad上完成了全部工作。 I check to see if the window's width is greater than its height in order to determine if the user is holding the device in landscape or portrait mode and I translate the data that the accelerometer provides based on that fact. 我检查窗口的宽度是否大于窗口的高度,以确定用户是横向还是纵向握住设备,然后根据该事实转换加速度计提供的数据。 For instance, if the iPad is being held with the home button to the bottom of the device, y is negative and corresponds to the force of gravity, as you might expect. 例如,如果将iPad按住主页按钮在设备底部,则y为负,并且与您预期的重力相对应。 If the device is rotated 90 degrees left or right, y moves toward 0 and x either increases to 9.8 or decreases to -9.8. 如果设备向左或向右旋转90度,则y向0移动,x增大至9.8或减小至-9.8。

The problem is that when I move to an Android device whose orientation is landscape by default (ie the device's hardware is arranged in such a way that it is clear it is intended to be used mainly in landscape orientation) the x and y coordinates are reversed and the fact that the window's height is greater than its width can't be used to determine whether the user is holding the device with it's top "up" and its bottom "down". 问题是,当我移动到默认情况下方向为横向的Android设备(即,设备的硬件布置方式很明显主要用于横向)时,x和y坐标相反并且窗口的高度大于宽度的事实不能用于确定用户是否握住设备的顶部“上”和底部“下”。

Is there a more universal way for me to determine the direction that a device is being held and as a result know how I should interpret the accelerometer data so that my gravity is always relative to the Earth? 对于我来说,是否有一种更通用的方法来确定设备的握持方向,从而知道如何解释加速度计数据,以便使我的重力始终相对于地球?

Here is a quick snippet of the method I'm using to determine the property values. 这是我用来确定属性值的方法的简要片段。

window.addEventListener('devicemotion', function(e) { console.log(e.accelerationIncludingGravity.x); console.log(e.accelerationIncludingGravity.y); })

After working on this for the rest of the day, it looks like the answer is that you need to add a listener to the window for the orientationchange event (and also the resize event since some Android devices don't fire orientationchange) and then checking window.orientation, which is either 0, 180 or +/-90 based on orientation. 在一天的其余时间中进行此工作之后,答案似乎是您需要在窗口中添加一个用于定向更改事件的监听器(以及由于某些Android设备不触发方向更改而导致的调整大小事件),然后检查window.orientation,根据方向为0、180或+/- 90。

var orientation = window.orientation;

//Optionally add an evenr listener on resize here
window.addEventListener('devicemotion', function() {
    if(Math.abs(orientation) == 90) {
        //device is sideways
    } else {
        //device is vertical
    }
});

window.addEventListener('orientationchange', function(e) {
    if(PanScaleTool.scaleToolData.accelerometer == true) {

        if(Math.abs(e.beta) > 45) {
            orientation = window.orientation;
        } else {
            orientation = window.orientation;
        }
    }
});

In my limited testing with an iOS device and two android devices, 0 and 180 seem to always correspond to device is vertical (front facing camera at top of screen for example) or upside down. 在我对一个iOS设备和两个android设备的有限测试中,0和180似乎始终对应于设备是垂直的(例如,屏幕顶部的前置摄像头)或上下颠倒的设备。 +/- 90 correspond to the other two orientations. +/- 90对应于其他两个方向。

Here's another question on SO that I referenced for this answer 这是我为此答案引用的另一个关于SO的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM