Created
December 2, 2019 20:31
-
-
Save pjbrown11/6bf4be4ac19135308b79aa7cb246ca57 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 lazyLoadFullImg = (node, data) => { | |
const loaded = new Map(); | |
// use bigger image file if warranted | |
const fileSize = window.devicePixelRatio > 1 ? '-2x' : '-1x' | |
const correctImg = `${data.path + fileSize}.${data.type}` | |
if (loaded.has(correctImg)) { | |
node.setAttribute("src", correctImg); | |
} else { | |
const img = new Image(); | |
img.src = correctImg; | |
// TODO: get appropriate fallback image | |
// if the full size image fails to load use fallback | |
// img.onerror = () => { | |
// loaded.set('img/bio-background-design.svg', img); | |
// node.setAttribute("src", 'img/bio-background-design.svg'); | |
// }; | |
img.onload = () => { | |
loaded.set(correctImg, img); | |
node.setAttribute("src", correctImg); | |
}; | |
} | |
return { | |
destroy() { } // noop | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment