Skip to main content
added 378 characters in body
Source Link
Bálint
  • 15k
  • 2
  • 37
  • 56

You don't need to use the accelerometer, there's already a way to get the device's orientation with the deviceorientation event:

addEventListener("deviceorientation", function(e) {
    // This function is called when the phone rotates
    // e.alpha is the angle around the y axis (yaw)
    // e.beta is the angle around the x axis (pitch)
    // e.gamma is the angle around the z axis (roll)
});

If you really want to use the accelerometer, then here's how to do it:

Take the acceleration vector and subtract it from the accelerationWithGravity vector, the result is the gravity vector. This always points down (except if you are in space), so you can normalize the vector, get the angles of it and negate them. This should result in the orientatiom of the device.

You don't need to use the accelerometer, there's already a way to get the device's orientation with the deviceorientation event:

addEventListener("deviceorientation", function(e) {
    // This function is called when the phone rotates
    // e.alpha is the angle around the y axis (yaw)
    // e.beta is the angle around the x axis (pitch)
    // e.gamma is the angle around the z axis (roll)
});

You don't need to use the accelerometer, there's already a way to get the device's orientation with the deviceorientation event:

addEventListener("deviceorientation", function(e) {
    // This function is called when the phone rotates
    // e.alpha is the angle around the y axis (yaw)
    // e.beta is the angle around the x axis (pitch)
    // e.gamma is the angle around the z axis (roll)
});

If you really want to use the accelerometer, then here's how to do it:

Take the acceleration vector and subtract it from the accelerationWithGravity vector, the result is the gravity vector. This always points down (except if you are in space), so you can normalize the vector, get the angles of it and negate them. This should result in the orientatiom of the device.

Source Link
Bálint
  • 15k
  • 2
  • 37
  • 56

You don't need to use the accelerometer, there's already a way to get the device's orientation with the deviceorientation event:

addEventListener("deviceorientation", function(e) {
    // This function is called when the phone rotates
    // e.alpha is the angle around the y axis (yaw)
    // e.beta is the angle around the x axis (pitch)
    // e.gamma is the angle around the z axis (roll)
});