forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.js
More file actions
158 lines (139 loc) · 3.37 KB
/
platform.js
File metadata and controls
158 lines (139 loc) · 3.37 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let desktop = false;
let mobile = false;
let windows = false;
let xbox = false;
let android = false;
let ios = false;
let touch = false;
let gamepads = false;
let workers = false;
let passiveEvents = false;
if (typeof navigator !== 'undefined') {
const ua = navigator.userAgent;
if (/(windows|mac os|linux|cros)/i.test(ua))
desktop = true;
if (/xbox/i.test(ua))
xbox = true;
if (/(windows phone|iemobile|wpdesktop)/i.test(ua)) {
desktop = false;
mobile = true;
windows = true;
} else if (/android/i.test(ua)) {
desktop = false;
mobile = true;
android = true;
} else if (/ip([ao]d|hone)/i.test(ua)) {
desktop = false;
mobile = true;
ios = true;
}
if (typeof window !== 'undefined') {
touch = 'ontouchstart' in window || ('maxTouchPoints' in navigator && navigator.maxTouchPoints > 0);
}
gamepads = 'getGamepads' in navigator;
workers = (typeof(Worker) !== 'undefined');
try {
const opts = Object.defineProperty({}, 'passive', {
get: function () {
passiveEvents = true;
return false;
}
});
window.addEventListener("testpassive", null, opts);
window.removeEventListener("testpassive", null, opts);
} catch (e) {}
}
/**
* @namespace
* @name platform
* @description Global namespace that stores flags regarding platform environment and features support.
* @example
* if (pc.platform.touch) {
* // touch is supported
* }
*/
const platform = {
/**
* @static
* @readonly
* @type {boolean}
* @name platform.desktop
* @description Is it a desktop or laptop device.
*/
desktop: desktop,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.mobile
* @description Is it a mobile or tablet device.
*/
mobile: mobile,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.ios
* @description If it is iOS.
*/
ios: ios,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.android
* @description If it is Android.
*/
android: android,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.windows
* @description If it is Windows.
*/
windows: windows,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.xbox
* @description If it is Xbox.
*/
xbox: xbox,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.gamepads
* @description If platform supports gamepads.
*/
gamepads: gamepads,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.touch
* @description If platform supports touch input.
*/
touch: touch,
/**
* @static
* @readonly
* @type {boolean}
* @name platform.workers
* @description If the platform supports Web Workers.
*/
workers: workers,
/**
* @private
* @static
* @readonly
* @type {boolean}
* @name platform.passiveEvents
* @description If the platform supports an options object as the third parameter
* to `EventTarget.addEventListener()` and the passive property is supported.
*/
passiveEvents: passiveEvents
};
export { platform };