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

218 lines
6.4 KiB
JavaScript
Raw Normal View History

2020-05-01 15:01:56 +02:00
import * as lazyLoader from 'lazyLoader';
import * as userSettings from 'userSettings';
2020-05-23 18:35:34 +02:00
import * as blurhash from 'blurhash';
2020-04-12 05:47:41 +02:00
import 'css!./style';
/* 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
}
2020-05-26 21:53:49 +02:00
async function itemBlurhashing(target) {
let blurhashstr = target.getAttribute('data-blurhash');
if (blurhashstr && blurhash.isBlurhashValid(blurhashstr)) {
// Although the default values provided by blurhash devs is 32x32, 18x18 seems to be the sweetest spot possible,
// cramping up a lot the performance and reducing the memory usage, while retaining almost full blur quality.
// Lower values had more visible pixelation
let width = 18;
let height = 18;
let pixels;
try {
pixels = blurhash.decode(blurhashstr, width, height);
} catch (err) {
console.error('Blurhash decode error: ' + err.toString());
return;
2020-05-23 18:35:34 +02:00
}
2020-05-26 21:53:49 +02:00
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext('2d');
let imgData = ctx.createImageData(width, height);
imgData.data.set(pixels);
ctx.putImageData(imgData, 0, 0);
let child = target.appendChild(canvas);
child.classList.add('blurhash-canvas');
child.style.opacity = 1;
if (userSettings.enableFastFadein()) {
child.classList.add('lazy-blurhash-fadein-fast');
} else {
child.classList.add('lazy-blurhash-fadein');
}
target.classList.add('blurhashed');
2020-05-23 18:35:34 +02:00
}
}
function switchCanvas(elem) {
let child = elem.getElementsByClassName('blurhash-canvas')[0];
if (child) {
2020-05-26 21:53:49 +02:00
child.style.opacity = elem.getAttribute('data-src') ? 1 : 0;
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');
}
2020-05-26 21:53:49 +02:00
let target = entry.target;
2020-04-12 14:34:51 +02:00
var 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-30 16:44:33 +02:00
if (!target.classList.contains('blurhashed') && userSettings.enableBlurhash()) {
2020-05-26 21:53:49 +02:00
itemBlurhashing(target);
2020-05-23 18:35:34 +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-05-26 21:53:49 +02:00
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) {
throw new Error('url cannot be undefined');
}
2020-04-12 03:49:42 +02:00
let preloaderImg = new Image();
preloaderImg.src = url;
2020-05-30 16:50:38 +02:00
// This is necessary, so switching between blurhash enabled and disabled works
2020-05-30 16:44:33 +02:00
if (!userSettings.enableBlurhash()) elem.classList.add('lazy-hidden');
2020-04-12 14:25:12 +02:00
preloaderImg.addEventListener('load', () => {
if (elem.tagName !== 'IMG') {
2020-04-12 03:49:42 +02:00
elem.style.backgroundImage = "url('" + url + "')";
} else {
elem.setAttribute('src', url);
2020-04-12 03:49:42 +02:00
}
2020-05-04 12:44:12 +02:00
elem.removeAttribute('data-src');
2020-05-30 16:44:33 +02:00
if (userSettings.enableBlurhash()) {
switchCanvas(elem);
} else {
elem.classList.remove('lazy-hidden');
if (userSettings.enableFastFadein()) {
elem.classList.add('lazy-image-fadein-fast');
} else {
elem.classList.add('lazy-image-fadein');
}
}
});
}
function emptyImageElement(elem) {
2020-04-12 03:49:42 +02:00
var url;
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
if (userSettings.enableBlurhash()) {
switchCanvas(elem);
} else {
if (userSettings.enableFastFadein()) {
elem.classList.remove('lazy-image-fadein-fast');
} else {
elem.classList.remove('lazy-image-fadein');
}
elem.classList.add('lazy-hidden');
}
2018-10-23 01:05:09 +03:00
}
2020-04-12 05:47:41 +02:00
export function lazyChildren(elem) {
lazyLoader.lazyChildren(elem, fillImage);
2018-10-23 01:05:09 +03:00
}
2020-04-12 05:47:41 +02:00
export function getPrimaryImageAspectRatio(items) {
var values = [];
for (var i = 0, length = items.length; i < length; i++) {
2018-10-23 01:05:09 +03:00
var 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;
});
var half = Math.floor(values.length / 2);
var 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
var aspect2x3 = 2 / 3;
if (Math.abs(aspect2x3 - result) <= 0.15) {
return aspect2x3;
}
// If really close to 16:9 (episode image), just return 16:9
var 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
var 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) {
2018-10-23 01:05:09 +03:00
for (var i = 0, length = elems.length; i < length; i++) {
var elem = elems[0];
fillImage(elem);
2018-10-23 01:05:09 +03:00
}
}
2020-04-12 05:47:41 +02:00
/* eslint-enable indent */
export default {
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
};