Last active
October 7, 2019 18:58
-
-
Save Danetag/ccef6a07d160f4a7ad7ab3146fd23537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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