playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
53 lines (52 loc) • 2.1 kB
JavaScript
const detectPassiveEvents = () => {
let result = false;
try {
const opts = Object.defineProperty({}, "passive", {
get: function() {
result = true;
return false;
}
});
window.addEventListener("testpassive", null, opts);
window.removeEventListener("testpassive", null, opts);
} catch (e) {
}
return result;
};
const ua = typeof navigator !== "undefined" ? navigator.userAgent : "";
const environment = typeof window !== "undefined" ? "browser" : typeof global !== "undefined" ? "node" : "worker";
const platformName = /android/i.test(ua) ? "android" : /ip(?:[ao]d|hone)/i.test(ua) ? "ios" : /windows/i.test(ua) ? "windows" : /mac os/i.test(ua) ? "osx" : /linux/i.test(ua) ? "linux" : /cros/i.test(ua) ? "cros" : null;
const browserName = environment !== "browser" ? null : /Chrome\/|Chromium\/|Edg.*\//.test(ua) ? "chrome" : (
// chrome, chromium, edge
/Safari\//.test(ua) ? "safari" : (
// safari, ios chrome/firefox
/Firefox\//.test(ua) ? "firefox" : "other"
)
);
const xbox = /xbox/i.test(ua);
const visionos = /Macintosh/i.test(ua) && typeof navigator !== "undefined" && navigator.maxTouchPoints > 0 && !/iPhone|iPad|iPod/i.test(ua);
const touch = environment === "browser" && ("ontouchstart" in window || "maxTouchPoints" in navigator && navigator.maxTouchPoints > 0);
const gamepads = environment === "browser" && (!!navigator.getGamepads || !!navigator.webkitGetGamepads);
const workers = typeof Worker !== "undefined";
const passiveEvents = detectPassiveEvents();
const platform = {
name: platformName,
environment,
global: (typeof globalThis !== "undefined" && globalThis) ?? (environment === "browser" && window) ?? (environment === "node" && global) ?? (environment === "worker" && self),
browser: environment === "browser",
worker: environment === "worker",
desktop: ["windows", "osx", "linux", "cros"].includes(platformName),
mobile: ["android", "ios"].includes(platformName),
ios: platformName === "ios",
android: platformName === "android",
visionos,
xbox,
gamepads,
touch,
workers,
passiveEvents,
browserName
};
export {
platform
};