I am using the DeviceMotionEvent web API, which gives me a device's acceleration split up into x-, y- and z-dimensions and a time interval.
Here's my attempt at moving an object on screen based on these accelerations in data (just the x-dimension, for clarity):
velocity.x = data.acceleration.x * data.interval;
var newLeft = $('.object').position().left + (velocity.x);
$('.object').css({ 'left': newLeft });
This sort of works, but after moving, it goes back to where it started from instead of slowing to a stop.
What am I doing wrong? How do I do this right?
Here's an example of some data the API returns:
{ acceleration:
{ x: -0.048474962000711816,
y: 0.025074772074713834,
z: -0.0035549827887938587 },
accelerationIncludingGravity:
{ x: -0.07826046448306878,
y: 0.34760814211749236,
z: -9.804854269410603 },
rotationRate:
{ alpha: -0.15093386712448556,
beta: 0.051146754356783085,
gamma: -0.019824252207518427 },
interval: 0.05000000074505806 }