Skip to content

Instantly share code, notes, and snippets.

@atleastimtrying
Created January 25, 2013 12:02
Show Gist options
  • Save atleastimtrying/4633919 to your computer and use it in GitHub Desktop.
Save atleastimtrying/4633919 to your computer and use it in GitHub Desktop.
an example of using accelerometers on the ipad in js
(function() {
var acceleration = {
x:0,
y:0
};
var div;
var init = function(){
div = document.getElementById("anObject");
div.x = 0;
div.y = 0;
};
var motionHandler = function(event){
acceleration.x = event.accelerationIncludingGravity.x;
acceleration.y = event.accelerationIncludingGravity.x;
moveDiv();
};
var moveDiv = function(){
div.x += acceleration.x;
div.y += acceleration.y;
updateDiv();
};
var updateOrientation = function(){
if (document.body.scrollWidth < 1024) {
this.orientation = "portrait";
} else if (document.body.scrollWidth > 768) {
this.orientation = "landscape";
}
}
var updateDiv = function(){
div.style.webkitTransform = "translate3d(" + div.x + "px, " + div.y + "px, 0)";
};
window.addEventListener("load", init,false);
window.addEventListener("devicemotion",motionHandler,false);
window.addEventListener("deviceorientation",orientationHandler,false);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment