0

I leaned that html5 contains a device motion detector and wrote this html. I opened this html with my cell phone browser.

<!DOCTYPE html>
...
var color = new Array('#FFF8DC', '#DC143C', '#00FFFF', '#00008B', '#008B8B', '#B8860B', '#A9A9A9', '#006400', '#BDB76B', '#8B008B');
function mark(n0)
{
    do 
        {
            n = Math.floor(Math.random()*11)%10
        } 
    while n == n0;
    return n;
}
if(window.DeviceMotionEvent) {
    var speed = 15;
    var x = y = z = x0 = y0 = z0 = n0 = 0;

    window.addEventListener('motionListener', function(){
        var acceleration =event.accelerationIncludingGravity;
        x = acceleration.x;
        y = acceleration.y;
        n = mark(n0)
        if(Math.abs(x-x0) > speed || Math.abs(y-y0) > speed) {
            document.body.style.backgroundColor = color[n];
        }
        x0 = x;
        y0 = y;
        n0 = n;
    }, false);
}
..

But the body color cannot be changed.

zhngnmng
  • 23
  • 3
  • 1
    Does it enters your `if` condition? Is the event listener getting registered? @mplungjan He has an event listener, isnt he? – Zee May 16 '15 at 13:09
  • The handler should be attached to devicemotion: `window.addEventListener('devicemotion'` – mplungjan May 16 '15 at 13:34

0 Answers0