phaser/src/dom/GetScreenOrientation.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var CONST = require('../scale/const');
2019-01-11 18:00:02 +00:00
2019-02-12 12:04:35 +00:00
/**
* Attempts to determine the screen orientation using the Orientation API.
*
* @function Phaser.DOM.GetScreenOrientation
* @since 3.16.0
*
* @param {number} width - The width of the viewport.
* @param {number} height - The height of the viewport.
*
* @return {string} The orientation.
*/
var GetScreenOrientation = function (width, height)
{
var screen = window.screen;
var orientation = (screen) ? screen.orientation || screen.mozOrientation || screen.msOrientation : false;
if (orientation && typeof orientation.type === 'string')
{
// Screen Orientation API specification
return orientation.type;
}
else if (typeof orientation === 'string')
{
// moz / ms-orientation are strings
return orientation;
}
if (screen)
{
2019-01-11 18:00:02 +00:00
return (screen.height > screen.width) ? CONST.PORTRAIT : CONST.LANDSCAPE;
}
else if (typeof window.orientation === 'number')
{
// This may change by device based on "natural" orientation.
2019-01-11 18:00:02 +00:00
return (window.orientation === 0 || window.orientation === 180) ? CONST.PORTRAIT : CONST.LANDSCAPE;
}
else if (window.matchMedia)
{
if (window.matchMedia('(orientation: portrait)').matches)
{
2019-01-11 18:00:02 +00:00
return CONST.PORTRAIT;
}
else if (window.matchMedia('(orientation: landscape)').matches)
{
2019-01-11 18:00:02 +00:00
return CONST.LANDSCAPE;
}
}
2019-01-11 18:00:02 +00:00
return (height > width) ? CONST.PORTRAIT : CONST.LANDSCAPE;
};
module.exports = GetScreenOrientation;