Reputation: 5629
I am using the deviceorientation
event in JavaScript to retrieve Gyroscope data. So far, I have tested this on iOS devices which worked perfectly. Today I tried the same code on a Microsoft Surface Pro 2 tablet running Chrome 39 and noticed I get pretty strange values.
After exploring this a bit, it seems that the alpha and gamma values are switched on the Surface Pro, as it can be seen when testing on this page.
When lying flat on a table and rotating the device, my iPad Air changes its alpha value while the Surface Pro changes the gamma value. When picking up the device (so it stands at a 90 degree angle to the table), I get a gamma value of 90 on the iPad and an alpha value of 90 on the Surface Pro.
I suppose, according to the description given at HTML5Rocks about how those events should be working, the iPad is doing the correct thing but the Surface Pro is not.
So I guess my question is: Is this a known issue? Is there any way to detect or work around this?
WORKAROUND:
I am currently using the following workaround for this:
var alphaGammaFlipped = false;
window.addEventListener("deviceorientation", function(e) {
if (e.alpha < 0 || e.gamma > 180) alphaGammaFlipped = true;
var alpha = e.alpha;
var beta = e.beta;
var gamma = e.gamma;
if (alphaGammaFlipped) {
var temp = alpha;
alpha = gamma;
gamma = temp;
}
// Do something with alpha, beta, gamma...
});
This code uses the assumption that alpha should be between 0 and 360 and gamma should be between -180 and 180. If one of them is out of bounds, this means that they are probably flipped. Obviously, this is just a workaround als also the values will be wrong as long as both stay between 0 and 180. If anyone has something better, I'd be glad.
Upvotes: 0
Views: 427
Reputation: 48
In my case, with the same config (Surface Pro 2 + javascript deviceorientation
), the three angles are reversed.
For fix this I do that :
false_alpha = gamma
false_beta = alpha
false_gamma = beta
(I noticed that there was no inversion with Internet Explorer 11 ...)
Upvotes: 0