layout: post author:
name: Vishal Mishra
url: https://twitter.com/tweetsbymishra
title: "Screen Orientation Plugin" categories: news
Originally posted at this PhoneGap blog
The new version(2.0.0) of the cordova screen orientation plugin was released recently. The purpose of the plugin is to set/lock the screen orientation in a common way for all the major platforms. This new version conforms with the specifications mentioned in the W3C Screen Orientation API, currently in open draft.
The plugin incorporates these major updates:
The screen orientation can be locked to a particular orientation, say, for example, landscape, using :
screen.orientation.lock('landscape').then(function success() {
console.log("Successfully locked the orientation");
}, function error(errMsg) {
console.log("Error locking the orientation :: " + errMsg);
});
The code above sets the screen orientation device to any landscape mode ( landscape-primary or landscape-secondary) depending upon the rotation. The screen.orientation.lock(OrientationLockType); method returns a promise.On successfully setting the orientation, it resolves a promise. If the screen is not locked successfully, the promise rejects with 'NotSupportedError' . The screen orientation can be unlocked by using:
screen.orientation.unlock();
The code above makes the screen adapt to the default orientation of the device. The unlock method does not return a promise.
The current screen orientation can be accessed as :
console.log('Orientation is' + screen.orientation.type);
An example usage of the 'onchange' event handler:
screen.orientation.onchange = function(){
console.log(screen.orientation.type);
};
The 'change' event can also be added to the screen.orientation object :
screen.orientation.addEventListener('change', function(){
console.log(screen.orientation.type);
});
The demo application is included on the github repository. Once the plugin has been added to it, the demo application allows the user to test the orientation types with the screen.orientation.lock() method. The demo application is explained in detail here.