Skip to content

Instantly share code, notes, and snippets.

@pixelthing
Last active February 14, 2017 10:22
Show Gist options
  • Save pixelthing/a44d5148ae5965cd9a55d8334b1c89d1 to your computer and use it in GitHub Desktop.
Save pixelthing/a44d5148ae5965cd9a55d8334b1c89d1 to your computer and use it in GitHub Desktop.
browser feature detection
// Screen orientation:
if (typeof screen.orientation !== 'undefined') {
data.orientation = screen.orientation.angle;
} else if (typeof window.orientation !== 'undefined') {
  data.orientation = window.orientation;
}
// screen width/height
data.screenW = Math.max(screen.width || 0);
data.screenH = Math.max(screen.height || 0);
// edge case for older Android Browsers that report screen width/height in actual pixels, not CSS px
if (data.ua.indexOf('Android') > -1 && data.ua.indexOf('Chrome') === -1 && data.screenW >= 480 && data.screenH >= 480) {
data.screenW = data.screenW/2;
data.screenH = data.screenW/2;
}
// dppx
if (typeof window.devicePixelRatio !== 'undefined') {
data.dppx = window.devicePixelRatio.toFixed(2);
} else {
for(var i=5; i>=1; i = (i-0.05).toFixed(2)) {
if (window.matchMedia("(-webkit-min-device-pixel-ratio: " + i + ")").matches || window.matchMedia("(min-resolution: " + i + "dppx)").matches) {
data.dppx = i;
break;
}
}
}
// force touch
data.forceTouch = false;
if ('webkitmouseforcedown' in document) {
data.forceTouch = true;
}
// wide gamut color
data.wideGamut = false;
if (window.matchMedia("(color-gamut: p3)").matches) {
data.wideGamut = true;
}
// pointer events
data.pointerEvents = false;
if ('PointerEvent' in window || (window.navigator && 'msPointerEnabled' in window.navigator)) {
data.pointerEvents = true;
}
// touch events
data.touchEvents = false;
if ('ontouchstart' in document || (window.navigator && window.navigator.maxTouchPoints)) {
data.touchEvents = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment