Files
OfficialSite/assets/scripts/utils/image.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-10-18 16:45:31 -04:00
const LAZY_LOADED_IMAGES = []
export function loadImage(url, options = {}) {
return new Promise((resolve, reject) => {
const $img = new Image();
2021-10-19 14:45:24 -04:00
if (options.crossOrigin) {
$img.crossOrigin = options.crossOrigin;
}
2021-10-18 16:45:31 -04:00
const loadCallback = () => {
resolve({
element: $img,
...getImageMetadata($img),
});
}
if($img.decode) {
$img.src = url
$img.decode().then(loadCallback).catch(e => {
reject(e)
})
} else {
$img.onload = loadCallback
$img.onerror = (e) => {
reject(e);
};
$img.src = url
}
});
}
export function getImageMetadata($img) {
return {
url: $img.src,
width: $img.naturalWidth,
height: $img.naturalHeight,
ratio: $img.naturalWidth / $img.naturalHeight,
};
}
/**
2021-10-19 14:41:43 -04:00
* Lazy load the given image.
2021-10-18 16:45:31 -04:00
*
2021-10-19 14:41:43 -04:00
* @param {HTMLImageElement} $el - The image element.
* @param {?string} url - The URI to lazy load into $el.
* If falsey, the value of the `data-src` attribute on $el will be used as the URI.
* @param {?function} callback - A function to call when the image is loaded.
2021-10-18 16:45:31 -04:00
*/
2021-10-19 14:41:43 -04:00
export async function lazyLoadImage($el, url, callback) {
2021-10-18 16:45:31 -04:00
let src = url ? url : $el.dataset.src
let loadedImage = LAZY_LOADED_IMAGES.find(image => image.url === src)
if (!loadedImage) {
loadedImage = await loadImage(src)
2021-10-19 14:45:24 -04:00
if (!loadedImage.url) {
return;
}
2021-10-18 16:45:31 -04:00
LAZY_LOADED_IMAGES.push(loadedImage)
}
if($el.src === src) {
return
}
2021-10-19 14:45:24 -04:00
if ($el.tagName === 'IMG') {
2021-10-18 16:45:31 -04:00
$el.src = loadedImage.url;
} else {
$el.style.backgroundImage = `url(${loadedImage.url})`;
}
requestAnimationFrame(() => {
let lazyParent = $el.closest('.c-lazy');
2021-10-19 14:45:24 -04:00
2021-10-18 16:45:31 -04:00
if(lazyParent) {
lazyParent.classList.add('-lazy-loaded')
lazyParent.style.backgroundImage = ''
}
2021-10-19 14:45:24 -04:00
2021-10-18 16:45:31 -04:00
$el.classList.add('-lazy-loaded')
callback?.()
})
}