【发布时间】:2015-12-28 18:13:12
【问题描述】:
我正在使用window.DeviceOrientationEvent 来监听设备方向的变化。但是,我想校准我的应用程序以报告相对于原始方向的方向变化相对。为此,我提出了以下解决方案:
originalOrientation.freeze = false;
window.addEventListener('deviceorientation', function(e){
var orientation = {g: Math.round(e.gamma), b: Math.round(e.beta), a: Math.round(e.alpha), o: window.orientation || 0};
if(!originalOrientation.freeze){
originalOrientation = orientation;
originalOrientation.freeze = true;
}
});
这实质上是采用deviceorientation 侦听器返回的第一个值并“冻结”它,因此它不会继续更新。我不喜欢这种方法,因为我宁愿在代码中的其他地方进行校准,而不是在收集实际方向的地方。我也不想两次附加监听器,因为回调中返回的值将丢失。
TLDR;
有没有办法我可以调用类似 window.getDeviceOrientation() 的方法来同步返回 alpha、beta 和 gamma 值,而不是附加回调?
【问题讨论】:
标签: javascript html device-orientation