Created
April 23, 2015 12:31
-
-
Save bgola/b850760d052cd5da6cac to your computer and use it in GitHub Desktop.
poc accel api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>yay</title> | |
<script type="text/javascript"> | |
var arrx = []; | |
var arry = []; | |
var arrz = []; | |
for (var i=0; i<100; i++) { | |
arrx.push(0); | |
arry.push(0); | |
arrz.push(0); | |
} | |
function mean(arr) { | |
var t = 0; | |
for (var i=0; i<arr.length; i++) | |
t = t + arr[i]; | |
return t / (i+1); | |
} | |
</script> | |
</head> | |
<body style="background-color: Aquamarine;"> | |
<div id="color">no accel</div> | |
<script type="text/javascript"> | |
window.ondevicemotion = function(event) { | |
var accX = event.acceleration.x; | |
var accY = event.acceleration.y; | |
var accZ = event.acceleration.z; | |
if (!isNaN(accX)) { | |
arrx.push(accX); | |
arrx.splice(0, 1); | |
} | |
if (!isNaN(accY)) { | |
arry.push(accY); | |
arry.splice(0, 1); | |
} | |
if (!isNaN(accZ)) { | |
arrz.push(accZ); | |
arrz.splice(0, 1); | |
} | |
if (mean(arrx) > 0.25 || mean(arry) > 0.25 || mean(arrz) > 0.25) { | |
document.getElementsByTagName("body")[0].style['backgroundColor'] = "red"; | |
} | |
document.getElementById("color").innerHTML = "X: " + mean(arrx); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment