예 - 휴대 전화를 똑바로 세우고있는 경우. 그런 다음 우리는 휴대 전화의 X에 중력의 당김을 읽을 DeviceMotionEvents
을 사용할 수 있으며 Y 축의 : 전화가 평면 유지 또는 테이블에 누워하면 분명히
function watchOrientation(subscriber) {
if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', eventData => {
const hg = eventData.accelerationIncludingGravity.x;
const vg = eventData.accelerationIncludingGravity.y;
if (vg > 5) {
subscriber('up');
} else if (vg <= -5) {
subscriber('down');
} else if (hg > 0) {
subscriber('left');
} else if (hg <= 0) {
subscriber('right');
}
}, true);
} else {
console.warn('Device does not support "devicemotion" events');
}
}
이 작동하지 않습니다. 이 경우 유일한 기회는 DeviceOrientationEvent
의 나침반 데이터를 사용하는 것입니다 (단, 사용자가 북쪽을 마주 치는지 여부를 이미 알고있는 경우에만 해당).