-
-
Save ekoneko/7c5feed78341286988fb to your computer and use it in GitHub Desktop.
mobile motion test
This file contains 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
<!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