DeviceOrientation Event
スマホに搭載された加速度センサーやコンパス情報をリアルタイムに取得できるAPI。
- DeviceOrientationEvent
- 加速度センサーがデバイスの方向の変化を検出すると発生。
- DeviceMotionEvent
- 加速度が変化すると発生。
DeviceOrientation Event:
DeviceMotion Event:
DeviceAcceleration プロパティ:
DeviceRotationRote プロパティ:
<p>DeviceOrientation Event:<span id="output1"></span></p> <p>DeviceMotion Event:<span id="output2"></span></p> <p>DeviceAcceleration プロパティ:<span id="output3"></span></p> <p>DeviceRotationRote プロパティ:<span id="output4"></span></p>
window.addEventListener('deviceorientation', function(e) {
var DeviceOrientationEvent = '<br>absolute(現実空間内における(絶対的な姿勢情報が取得可能かどうか)):' + e.absolute +
'<br>alpha(z軸を中心にしたデバイスの動き):' + e.alpha +
'<br>beta(x軸を中心にしたデバイスの動き):' + e.beta +
'<br>ganma(y軸を中心にしたデバイスの動き):' + e.ganma;
document.getElementById('output1').innerHTML = DeviceOrientationEvent;
console.log(e.absolute);
console.log(e.alpha);
console.log(e.beta);
console.log(e.ganma);
}, false);
window.addEventListener("devicemotion", function(e) {
var DeviceMotionEvent = '<br>acceleration(DeviceAcceleationオブジェクト):' + e.acceleration +
'<br>rotationRate(DeviceRotationRateオブジェクト):' + e.rotationRate;
document.getElementById('output2').innerHTML = DeviceMotionEvent;
var DeviceAcceleration = '<br>acceleration.x(西から東に向かう軸)' + e.acceleration.x *
'<br>acceleration.y(南から北に向かう軸)' + e.acceleration.y +
'<bracceleration.z(地面から直立する軸)' + e.acceleration.z;
document.getElementById('output3').innerHTML = DeviceAcceleration;
var DeviceRotationRote = '<br>rotationRate.alpha(スクリーンから直立する軸)' + e.rotationRate.alpha *
'<brrotationRate.beta(スクリーンの面の左から右へ向かう軸に沿った回転量)' + e.rotationRate.beta +
'<brrotationRate.gamma(スクリーンの面の下から上へ向かう軸に沿った回転量)' + e.rotationRate.gamma;
document.getElementById('output4').innerHTML = DeviceRotationRote;
}, true);