LoginSignup
0
0

More than 5 years have passed since last update.

jsdoでdeviceorientation その5

Posted at

概要

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);

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0