Skip to content

Instantly share code, notes, and snippets.

@Danetag
Last active October 7, 2019 18:58
Show Gist options
  • Save Danetag/ccef6a07d160f4a7ad7ab3146fd23537 to your computer and use it in GitHub Desktop.
Save Danetag/ccef6a07d160f4a7ad7ab3146fd23537 to your computer and use it in GitHub Desktop.
export const getImageWidthByUserContext = (width, MAX_WIDTH = 2880) => {
if (typeof(window) === 'undefined') return width;
// Pixel ratio
let qualityRatio = window.devicePixelRatio ? window.devicePixelRatio : 1;
// If bad connection, we downgrade
let type = null;
if (navigator && navigator.connection && navigator.connection.type) type = navigator.connection.type;
if (navigator && navigator.connection && navigator.connection.effectiveType) type = navigator.connection.effectiveType;
if (type !== null && (type !== 'wifi' && type !== '4g')) qualityRatio = 1;
width *= qualityRatio;
if (width > MAX_WIDTH) width = MAX_WIDTH;
return width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment