Detect Orientation Without Rotating (Enyo)
From WebOS101
In Enyo, there was no clear way to prevent the device from rotating, but still having orientation events fire. The following code will accomplish this.
Le Code
First, you have to prevent the app from rotating when the device rotates.
enyo.setAllowedOrientation("up"); //"up" can be changed to "left", "right", or "down".
Next, you have to set up a listener to listen to accelerometer event changes. You can add this code in any of your Enyo function.
document.addEventListener('acceleration', this.handleAcceleration.bind(this), false);
Lastly, we want to handle our acceleration events, and determine the orientation based off of the raw accelerometer data.
handleAcceleration: function(inEvent){
var x = Math.floor(inEvent.accelX * 100);
var y = Math.floor(inEvent.accelY * 100);
//Cascading IF's
if(x < -20){
if(y > 30){
//Down
}else if(y >30){
//Up
}else{
//Button on Right
}
}else if(x > 20){
if(y > 30){
//Down
}else if(y > 30){
//Up
}else{
//Button on Right
}
}else if(y > 20){
//Down
}else if(y < -20){
//Up
}
},
Have fun.
Hugs and kisses,
- Kesne