refactor: use blurhash to render background
This commit is contained in:
12 files changed
+205
-164
No files matched your search
@@ -1,50 +1,38 @@
|
||||
import { AnimatePresence, motion } from "motion/react";
|
||||
import React, { useLayoutEffect, useState } from "react";
|
||||
import React from "react";
|
||||
import { useShallow } from "zustand/shallow";
|
||||
import { useBackdropStore } from "@/utils/store/backdrop";
|
||||
import BlurhashCanvas from "../blurhash-canvas";
|
||||
|
||||
export default function Backdrop() {
|
||||
const [backdropLoading, setBackdropLoading] = useState(true);
|
||||
|
||||
const [backdropId, backdropUrl] = useBackdropStore((state) => [
|
||||
state.backdropId,
|
||||
state.backdropUrl,
|
||||
]);
|
||||
|
||||
// Reset loading status for backdrop
|
||||
useLayoutEffect(() => {
|
||||
setBackdropLoading(true);
|
||||
}, [backdropId]);
|
||||
|
||||
if (!backdropUrl) {
|
||||
// const [backdropLoading, setBackdropLoading] = useState(true);
|
||||
|
||||
const { backdropHash } = useBackdropStore(
|
||||
useShallow((state) => ({
|
||||
backdropHash: state.backdropHash,
|
||||
})),
|
||||
);
|
||||
|
||||
if (!backdropHash) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className="app-backdrop-container">
|
||||
<AnimatePresence>
|
||||
<motion.img
|
||||
key={backdropId}
|
||||
src={backdropUrl}
|
||||
alt=""
|
||||
className="app-backdrop"
|
||||
initial={{
|
||||
opacity: 0,
|
||||
}}
|
||||
animate={{
|
||||
opacity: backdropLoading ? 0 : 1,
|
||||
}}
|
||||
exit={{
|
||||
opacity: 0,
|
||||
}}
|
||||
transition={{
|
||||
opacity: {
|
||||
duration: 1.2,
|
||||
},
|
||||
}}
|
||||
onLoad={() => setBackdropLoading(false)}
|
||||
loading="lazy"
|
||||
/>
|
||||
</AnimatePresence>
|
||||
<BlurhashCanvas
|
||||
key={backdropHash}
|
||||
blurhashString={backdropHash}
|
||||
width={300}
|
||||
height={150}
|
||||
canvasProps={{
|
||||
style: {
|
||||
display: "block",
|
||||
width: "100vw",
|
||||
height: "100vh",
|
||||
margin: "0 auto",
|
||||
filter: "brightness(0.8) contrast(1.6) saturate(1.6)",
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import * as blurhash from "blurhash-wasm";
|
||||
import React, { useEffect } from "react";
|
||||
|
||||
type BlurhashCanvasProps = {
|
||||
canvasProps?: React.CanvasHTMLAttributes<HTMLCanvasElement>;
|
||||
blurhashString: string;
|
||||
width?: number;
|
||||
height?: number;
|
||||
// punch?: number;
|
||||
onLoad?: () => void;
|
||||
onError?: (error: Error) => void;
|
||||
};
|
||||
|
||||
const BlurhashCanvas = ({
|
||||
canvasProps,
|
||||
blurhashString,
|
||||
width,
|
||||
height,
|
||||
// punch,
|
||||
onLoad,
|
||||
onError,
|
||||
}: BlurhashCanvasProps) => {
|
||||
const canvasRef = React.useRef<HTMLCanvasElement>(null);
|
||||
useEffect(() => {
|
||||
const canvas = canvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) return;
|
||||
const drawBlurhash = async () => {
|
||||
try {
|
||||
const imageDataBuffer = blurhash.decode(
|
||||
blurhashString,
|
||||
width || 32,
|
||||
height || 32,
|
||||
// punch || 1,
|
||||
);
|
||||
// canvas.width = width || 32;
|
||||
// canvas.height = height || 32;
|
||||
if (!imageDataBuffer) {
|
||||
throw new Error("Failed to decode blurhash");
|
||||
}
|
||||
const imageData = new ImageData(
|
||||
new Uint8ClampedArray(imageDataBuffer),
|
||||
width || 32,
|
||||
height || 32,
|
||||
);
|
||||
// imageData.data.set(imageDataBuffer);
|
||||
ctx.putImageData(imageData, 0, 0);
|
||||
if (onLoad) onLoad();
|
||||
} catch (error) {
|
||||
if (onError) onError(error as Error);
|
||||
else console.error("Error decoding blurhash:", error);
|
||||
}
|
||||
};
|
||||
drawBlurhash();
|
||||
}, [blurhash, width, height, onLoad, onError]);
|
||||
return <canvas ref={canvasRef} {...canvasProps} />;
|
||||
};
|
||||
|
||||
export default BlurhashCanvas;
|
||||
Reference in new issue
Block a user