Blocking device rotation on mobile web pages Blocking device rotation on mobile web pages javascript javascript

Blocking device rotation on mobile web pages


New API's are developing (and are currently available)!

screen.orientation.lock();   // webkit only

and

screen.lockOrientation("orientation");

Where "orientation" can be any of the following:

portrait-primary -It represents the orientation of the screen when it is in its primary portrait mode. A screen is considered in its primary portrait mode if the device is held in its normal position and that position is in portrait, or if the normal position of the device is in landscape and the device held turned by 90° clockwise. The normal position is device dependant.

portrait-secondary -It represents the orientation of the screen when it is in its secondary portrait mode. A screen is considered in its secondary portrait mode if the device is held 180° from its normal position and that position is in portrait, or if the normal position of the device is in landscape and the device held is turned by 90° anticlockwise. The normal position is device dependant.

landscape-primary -It represents the orientation of the screen when it is in its primary landscape mode. A screen is considered in its primary landscape mode if the device is held in its normal position and that position is in landscape, or if the normal position of the device is in portrait and the device held is turned by 90° clockwise. The normal position is device dependant.

landscape-secondary -It represents the orientation of the screen when it is in its secondary landscape mode. A screen is considered in its secondary landscape mode if the device held is 180° from its normal position and that position is in landscape, or if the normal position of the device is in portrait and the device held is turned by 90° anticlockwise. The normal position is device dependant.

portrait -It represents both portrait-primary and portrait-secondary.

landscape -It represents both landscape-primary and landscape-secondary.

default -It represents either portrait-primary and landscape-primary depends on natural orientation of devices. For example, if the panel resolution is 1280800, default will make it landscape, if the resolution is 8001280, default will make it to portrait.

Mozilla recommends adding a lockOrientationUniversal to screen to make it more cross-browser compatible.

screen.lockOrientationUniversal = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;

Go here for more info (deprecated API): https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation


Modern API docs in MDN is here: https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock


In JavaScript-enabled browsers it should be easy to determine if the screen is in landscape or portrait mode and compensate using CSS. It may be helpful to give users the option to disable this or at least warn them that device rotation will not work properly.

Edit

The easiest way to detect the orientation of the browser is to check the width of the browser versus the height of the browser. This also has the advantage that you'll know if the game is being played on a device that is naturally oriented in landscape mode (as some mobile devices like the PSP are). This makes more sense than trying to disable device rotation.

Edit 2

Daz has shown how you can detect device orientation, but detecting orientation is only half of the solution. If want to reverse the automatic orientation change, you'll need to rotate everything either 90° or 270°/-90°, e.g.

$(window).bind('orientationchange resize', function(event){  if (event.orientation) {    if (event.orientation == 'landscape') {      if (window.rotation == 90) {        rotate(this, -90);      } else {        rotate(this, 90);      }    }  }});function rotate(el, degs) {  iedegs = degs/90;  if (iedegs < 0) iedegs += 4;  transform = 'rotate('+degs+'deg)';  iefilter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+iedegs+')';  styles = {    transform: transform,    '-webkit-transform': transform,    '-moz-transform': transform,    '-o-transform': transform,    filter: iefilter,    '-ms-filter': iefilter  };  $(el).css(styles);}

Note: if you want to rotate in IE by an arbitrary angle (for other purposes), you'll need to use matrix transform, e.g.

rads = degs * Math.PI / 180;m11 = m22 = Math.cos(rads);m21 = Math.sin(rads);m12 = -m21;iefilter = "progid:DXImageTransform.Microsoft.Matrix("  + "M11 = " + m11 + ", "  + "M12 = " + m12 + ", "  + "M21 = " + m21 + ", "  + "M22 = " + m22 + ", sizingMethod = 'auto expand')";styles['filter'] = styles['-ms-filter'] = iefilter;

—or use CSS Sandpaper. Also, this applies the rotation style to the window object, which I've never actually tested and don't know if works or not. You may need to apply the style to a document element instead.

Anyway, I would still recommend simply displaying a message that asks the user to play the game in portrait mode.


seems weird that no one proposed the CSS media query solution:

@media screen and (orientation: portrait) {  ...}

and the option to use a specific style sheet:

<link rel="stylesheet" type="text/css" href="css/style_p.css" media="screen and (orientation: portrait)">

MDN:https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries#orientation