1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/images/imageLoader.js

242 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import * as lazyLoader from '../lazyLoader/lazyLoaderIntersectionObserver';
import * as userSettings from '../../scripts/settings/userSettings';
2021-11-04 20:07:07 +01:00
import { wrap } from 'comlink';
const getPixels = wrap(
new Worker(
// eslint-disable-next-line compat/compat
new URL('./blurhash.worker.ts', import.meta.url)
)
);
2021-01-26 16:25:38 -05:00
import './style.scss';
2020-04-12 05:47:41 +02:00
/* eslint-disable indent */
2020-04-12 14:29:42 +02:00
export function lazyImage(elem, source = elem.getAttribute('data-src')) {
2020-04-12 14:25:12 +02:00
if (!source) {
return;
}
2020-04-12 14:29:42 +02:00
fillImageElement(elem, source);
2020-04-12 14:25:12 +02:00
}
2021-11-04 20:07:07 +01:00
async function itemBlurhashing(target, hash) {
try {
2020-06-04 19:07:57 +02:00
// Although the default values recommended by Blurhash developers is 32x32, a size of 18x18 seems to be the sweet spot for us,
// improving the performance and reducing the memory usage, while retaining almost full blur quality.
2020-05-26 21:53:49 +02:00
// Lower values had more visible pixelation
2021-11-04 20:07:07 +01:00
const width = 32;
const height = 32;
const pixels = await getPixels({
hash,
width,
height
});
const canvas = document.createElement('canvas');
2020-05-26 21:53:49 +02:00
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
const imgData = ctx.createImageData(width, height);
2020-05-26 21:53:49 +02:00
imgData.data.set(pixels);
ctx.putImageData(imgData, 0, 0);
2020-06-29 23:57:05 +02:00
requestAnimationFrame(() => {
canvas.classList.add('blurhash-canvas');
if (userSettings.enableFastFadein()) {
canvas.classList.add('lazy-blurhash-fadein-fast');
} else {
canvas.classList.add('lazy-blurhash-fadein');
}
2020-05-23 18:35:34 +02:00
2020-06-29 23:57:05 +02:00
target.parentNode.insertBefore(canvas, target);
target.classList.add('blurhashed');
target.removeAttribute('data-blurhash');
});
2021-11-04 20:07:07 +01:00
} catch (err) {
console.log(err);
target.classList.add('non-blurhashable');
return;
2020-05-23 18:35:34 +02:00
}
}
2020-04-12 05:47:41 +02:00
export function fillImage(entry) {
if (!entry) {
throw new Error('entry cannot be null');
}
const target = entry.target;
2020-10-07 21:12:14 +09:00
let source = undefined;
2020-05-26 21:53:49 +02:00
if (target) {
source = target.getAttribute('data-src');
2020-04-12 14:25:12 +02:00
} else {
2020-04-12 14:34:51 +02:00
source = entry;
2020-04-12 14:25:12 +02:00
}
2020-05-01 17:06:50 +02:00
if (entry.intersectionRatio > 0) {
2020-05-26 21:53:49 +02:00
if (source) fillImageElement(target, source);
} else if (!source) {
2020-06-29 23:38:46 +02:00
requestAnimationFrame(() => {
emptyImageElement(target);
});
}
2018-10-23 01:05:09 +03:00
}
2020-04-12 03:49:42 +02:00
function fillImageElement(elem, url) {
2020-04-12 14:34:51 +02:00
if (url === undefined) {
2020-06-04 19:07:57 +02:00
throw new TypeError('url cannot be undefined');
2020-04-12 14:34:51 +02:00
}
const preloaderImg = new Image();
2020-04-12 03:49:42 +02:00
preloaderImg.src = url;
2020-06-29 23:38:46 +02:00
elem.classList.add('lazy-hidden');
2020-05-30 16:44:33 +02:00
2020-04-12 14:25:12 +02:00
preloaderImg.addEventListener('load', () => {
2020-06-29 23:38:46 +02:00
requestAnimationFrame(() => {
if (elem.tagName !== 'IMG') {
elem.style.backgroundImage = "url('" + url + "')";
} else {
elem.setAttribute('src', url);
}
elem.removeAttribute('data-src');
2020-05-30 16:44:33 +02:00
elem.classList.remove('lazy-hidden');
if (userSettings.enableFastFadein()) {
elem.classList.add('lazy-image-fadein-fast');
} else {
elem.classList.add('lazy-image-fadein');
}
2021-01-05 11:01:06 +01:00
const canvas = elem.previousSibling;
if (elem.classList.contains('blurhashed') && canvas && canvas.tagName === 'CANVAS') {
canvas.classList.remove('lazy-image-fadein-fast', 'lazy-image-fadein');
canvas.classList.add('lazy-hidden');
}
2020-06-29 23:38:46 +02:00
});
});
}
function emptyImageElement(elem) {
2021-11-06 00:41:10 +03:00
// block repeated call - requestAnimationFrame twice for one image
if (elem.getAttribute('data-src')) return;
2020-10-07 21:12:14 +09:00
let url;
2020-04-12 03:49:42 +02:00
if (elem.tagName !== 'IMG') {
url = elem.style.backgroundImage.slice(4, -1).replace(/"/g, '');
2020-04-12 03:49:42 +02:00
elem.style.backgroundImage = 'none';
} else {
url = elem.getAttribute('src');
elem.setAttribute('src', '');
2020-04-12 03:49:42 +02:00
}
elem.setAttribute('data-src', url);
2020-05-30 16:44:33 +02:00
2020-06-29 23:38:46 +02:00
elem.classList.remove('lazy-image-fadein-fast', 'lazy-image-fadein');
elem.classList.add('lazy-hidden');
2021-01-05 11:01:06 +01:00
const canvas = elem.previousSibling;
if (canvas && canvas.tagName === 'CANVAS') {
canvas.classList.remove('lazy-hidden');
if (userSettings.enableFastFadein()) {
canvas.classList.add('lazy-image-fadein-fast');
} else {
canvas.classList.add('lazy-image-fadein');
}
}
2018-10-23 01:05:09 +03:00
}
2020-04-12 05:47:41 +02:00
export function lazyChildren(elem) {
2020-06-30 00:10:13 +02:00
if (userSettings.enableBlurhash()) {
2020-11-08 12:37:53 +00:00
for (const lazyElem of elem.querySelectorAll('.lazy')) {
2020-06-30 00:10:13 +02:00
const blurhashstr = lazyElem.getAttribute('data-blurhash');
2020-06-29 23:38:46 +02:00
if (!lazyElem.classList.contains('blurhashed', 'non-blurhashable') && blurhashstr) {
2021-11-04 20:07:07 +01:00
Promise.resolve(itemBlurhashing(lazyElem, blurhashstr));
2020-06-29 23:38:46 +02:00
} else if (!blurhashstr && !lazyElem.classList.contains('blurhashed')) {
lazyElem.classList.add('non-blurhashable');
}
2020-11-08 12:37:53 +00:00
}
2020-06-29 23:38:46 +02:00
}
2020-11-06 00:04:23 +00:00
lazyLoader.lazyChildren(elem, fillImage);
2018-10-23 01:05:09 +03:00
}
2020-04-12 05:47:41 +02:00
export function getPrimaryImageAspectRatio(items) {
2020-10-07 21:12:14 +09:00
const values = [];
2020-10-07 21:12:14 +09:00
for (let i = 0, length = items.length; i < length; i++) {
const ratio = items[i].PrimaryImageAspectRatio || 0;
if (!ratio) {
continue;
}
values[values.length] = ratio;
2018-10-23 01:05:09 +03:00
}
if (!values.length) {
return null;
}
// Use the median
values.sort(function (a, b) {
return a - b;
});
2020-10-07 21:12:14 +09:00
const half = Math.floor(values.length / 2);
2020-10-07 21:12:14 +09:00
let result;
if (values.length % 2) {
result = values[half];
} else {
result = (values[half - 1] + values[half]) / 2.0;
}
// If really close to 2:3 (poster image), just return 2:3
2020-10-07 21:12:14 +09:00
const aspect2x3 = 2 / 3;
if (Math.abs(aspect2x3 - result) <= 0.15) {
return aspect2x3;
}
// If really close to 16:9 (episode image), just return 16:9
2020-10-07 21:12:14 +09:00
const aspect16x9 = 16 / 9;
if (Math.abs(aspect16x9 - result) <= 0.2) {
return aspect16x9;
}
// If really close to 1 (square image), just return 1
if (Math.abs(1 - result) <= 0.15) {
return 1;
}
// If really close to 4:3 (poster image), just return 2:3
2020-10-07 21:12:14 +09:00
const aspect4x3 = 4 / 3;
if (Math.abs(aspect4x3 - result) <= 0.15) {
return aspect4x3;
}
return result;
2018-10-23 01:05:09 +03:00
}
2020-04-12 05:47:41 +02:00
export function fillImages(elems) {
2020-10-07 21:12:14 +09:00
for (let i = 0, length = elems.length; i < length; i++) {
const elem = elems[0];
fillImage(elem);
2018-10-23 01:05:09 +03:00
}
}
2020-05-10 13:36:26 +02:00
export function setLazyImage(element, url) {
element.classList.add('lazy');
element.setAttribute('data-src', url);
lazyImage(element);
}
2020-04-12 05:47:41 +02:00
/* eslint-enable indent */
export default {
2020-08-01 05:37:07 +02:00
setLazyImage: setLazyImage,
2020-04-12 05:47:41 +02:00
fillImages: fillImages,
2020-04-12 14:25:12 +02:00
fillImage: fillImage,
lazyImage: lazyImage,
2020-04-12 05:47:41 +02:00
lazyChildren: lazyChildren,
getPrimaryImageAspectRatio: getPrimaryImageAspectRatio
};