Skip to content

Instantly share code, notes, and snippets.

@benhodgson
Created May 28, 2013 17:47
Show Gist options
  • Save benhodgson/5664633 to your computer and use it in GitHub Desktop.
Save benhodgson/5664633 to your computer and use it in GitHub Desktop.
Playing about with ondevicemotion in MobileSafari on iOS to detect free fall
<!DOCTYPE html>
<html>
<head>
<title>Freefall detection test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div>
accel: x, y, z
</div>
<div id="log-container"></div>
<script>
var logContainer = document.getElementById("log-container"),
i = 0;
window.ondevicemotion = function(event) {
var a = event.accelerationIncludingGravity,
logLine = document.createElement("div");
if (Math.abs(a.x) < 1 && Math.abs(a.y) < 1 && Math.abs(a.z) < 1) {
// freefall(ish)!
document.body.style.backgroundColor = "red";
} else {
document.body.style.backgroundColor = "white";
}
logLine.innerText = a.x.toFixed(3) + ", " + a.y.toFixed(3) + ", " + a.z.toFixed(3);
logContainer.insertBefore(logLine, logContainer.firstChild);
i++;
if (i > 25) {
logContainer.removeChild(logContainer.lastChild);
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment