feat: optimize image hash
Some checks failed
Deploy to SFTP Server / build (push) Failing after 18m17s

This commit is contained in:
2024-04-06 22:38:56 +02:00
parent f86661bbba
commit 68431e6b9c
8 changed files with 850 additions and 648 deletions

View File

@ -10,12 +10,14 @@ interface Props {
maxWidth?: number;
}
import { rgbaToThumbHash } from "thumbhash";
import sharp from "sharp";
const { src: image, hash = true, alt, maxWidth } = Astro.props;
let thumbhash = "";
const s = await import("sharp");
const sharp = s.default;
if (hash) {
const scaleFactor = 100 / Math.max(image.width, image.height);
@ -57,7 +59,7 @@ const sizes = [
src={image}
alt={alt}
data-thumbhash={thumbhash}
pictureAttributes={{ class: hash ? "block h-full" : "" }}
pictureAttributes={{ class: hash ? "block h-full relative" : "" }}
class={Astro.props.class}
widths={sizes.map((size) => size.width)}
sizes={sizes

0
src/components/Max.png Executable file → Normal file
View File

View File

@ -11,6 +11,8 @@
}, 600);
}
console.log("Thumbhash script loaded");
document.querySelectorAll("[data-thumbhash]").forEach((entry) => {
const parent = entry?.parentNode as HTMLPictureElement;
const img = entry as HTMLImageElement;
@ -35,6 +37,10 @@
parent.style.background = `url(${dataURL})`;
parent.style.backgroundSize = "cover";
if (img.complete) {
return;
}
img.style.opacity = "0";
img.style.filter = "blur(5px)";
img.style.transition = "opacity 0.6s ease, filter 0.8s ease";