Skip to content

Instantly share code, notes, and snippets.

@GianlucaGuarini
Created October 9, 2017 12:48
Show Gist options
  • Save GianlucaGuarini/6453ab520a32239cc46d52e6fc21319e to your computer and use it in GitHub Desktop.
Save GianlucaGuarini/6453ab520a32239cc46d52e6fc21319e to your computer and use it in GitHub Desktop.
Because devicePixelRatio is not relyable enough
const resolutions = [
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',
'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=',
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJggg==',
];
const img = document.createElement('img');
img.src = resolutions[0];
img.srcset = resolutions.map((itm, i) => `${itm} ${i + 1}x`).join(',');
export default function imageDpiResolution() {
return resolutions.indexOf(img.currentSrc || img.src) + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment