Skip to content

Instantly share code, notes, and snippets.

@ekoneko
Created January 7, 2016 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekoneko/7c5feed78341286988fb to your computer and use it in GitHub Desktop.
Save ekoneko/7c5feed78341286988fb to your computer and use it in GitHub Desktop.
mobile motion test
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="count" style="font-size:2em;font-weight=bold;"></div>
</body>
<script>
var lastX, lastY, lastZ, lastTime, lastSpeed,
count = 0;
function deviceMotionHandler(eventData) {
var x,y,z, speed;
var acceleration = eventData.accelerationIncludingGravity;
var time = Date.now();
if (time - lastTime < 1000) return;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
speed = (x + y + z - lastX - lastY - lastZ) / (time - lastTime) * 1000
speed = speed || 0
document.body.innerHTML += '<p>'+speed+'</p>'
if (speed * lastSpeed < 0 && (Math.abs(speed) + Math.abs(lastSpeed)) > 3 ) count++
lastX = x;
lastY = y;
lastZ = z;
lastTime = time;
lastSpeed = speed;
document.getElementById('count').innerHTML = count;
}
window.onload = function () {
window.addEventListener('devicemotion', deviceMotionHandler, false);
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment