Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
poc accel api
<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
You can’t perform that action at this time.