概要
jsdoでdeviceorientationやってみた。
Lチカ作って見た。
環境
nexus 7
サンプルコード
var lastOrientation;
function onDeviceOrientationChange(event) {
lastOrientation.gamma = event.gamma;
lastOrientation.beta = event.beta;
}
function deviceOrientationTest(event) {
window.removeEventListener('deviceorientation', deviceOrientationTest);
if (event.beta != null && event.gamma != null)
{
window.addEventListener('deviceorientation', onDeviceOrientationChange, false);
}
}
lastOrientation = {};
window.addEventListener('deviceorientation', deviceOrientationTest, false);
function xhr(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(e) {
if (xhr.status != 200)
{
alert("ng");
}
else
{
document.getElementById('helloWorld').innerHTML = xhr.responseText;
}
};
xhr.onerror = function() {
alert("err");
}
xhr.send(null);
}
setInterval(function() {
var gamma = lastOrientation.gamma;
if (gamma < -40)
{
xhr("/assets/2/k/H/B/2kHBg");
}
else
{
xhr("/assets/O/M/u/H/OMuHB");
}
}, 500);
成果物
以上。