Created
January 25, 2013 12:02
-
-
Save atleastimtrying/4633919 to your computer and use it in GitHub Desktop.
an example of using accelerometers on the ipad in js
This file contains hidden or 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
(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