/
GetScreenOrientation.js
59 lines (54 loc) · 1.8 KB
/
GetScreenOrientation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var CONST = require('../scale/const');
/**
* 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 (typeof window.orientation === 'number')
{
// Do this check first, as iOS supports this, but also has an incomplete window.screen implementation
// This may change by device based on "natural" orientation.
return (window.orientation === 0 || window.orientation === 180) ? CONST.ORIENTATION.PORTRAIT : CONST.ORIENTATION.LANDSCAPE;
}
else if (window.matchMedia)
{
if (window.matchMedia('(orientation: portrait)').matches)
{
return CONST.ORIENTATION.PORTRAIT;
}
else if (window.matchMedia('(orientation: landscape)').matches)
{
return CONST.ORIENTATION.LANDSCAPE;
}
}
else
{
return (height > width) ? CONST.ORIENTATION.PORTRAIT : CONST.ORIENTATION.LANDSCAPE;
}
};
module.exports = GetScreenOrientation;