Skip to content

Instantly share code, notes, and snippets.

@jpellizzari
Created August 18, 2023 16:26
Show Gist options
  • Save jpellizzari/f7954e5dae06de16f7d1a750dc060f6a to your computer and use it in GitHub Desktop.
Save jpellizzari/f7954e5dae06de16f7d1a750dc060f6a to your computer and use it in GitHub Desktop.
//https://stackoverflow.com/questions/19689715/what-is-the-best-way-to-detect-retina-support-on-a-device-using-javascript
import { ScreenSize } from "./types";
export function isHighDensity() {
return (
(window.matchMedia &&
(window.matchMedia(
"only screen and (min-resolution: 124dpi), only screen and (min-resolution: 1.3dppx), only screen and (min-resolution: 48.8dpcm)"
).matches ||
window.matchMedia(
"only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (min-device-pixel-ratio: 1.3)"
).matches)) ||
(window.devicePixelRatio && window.devicePixelRatio > 1.3)
);
}
export function isRetina() {
return (
((window.matchMedia &&
(window.matchMedia(
"only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)"
).matches ||
window.matchMedia(
"only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)"
).matches)) ||
(window.devicePixelRatio && window.devicePixelRatio >= 2)) &&
/(iPad|iPhone|iPod)/g.test(navigator.userAgent)
);
}
export function isMobile() {
return adjustedScreenWidth() < ScreenSize.Tablet;
}
export function adjustedScreenWidth() {
return isRetina() ? screen.width / 2 : screen.width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment