From 2f7509ca69eb6edda1319b428d7b619ac7871f86 Mon Sep 17 00:00:00 2001 From: Matt Welsh Date: Thu, 12 Dec 2024 20:05:38 -0800 Subject: [PATCH] Adding loading effect. --- astro/src/components/LoadingImage.tsx | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 astro/src/components/LoadingImage.tsx diff --git a/astro/src/components/LoadingImage.tsx b/astro/src/components/LoadingImage.tsx new file mode 100644 index 0000000..e53e5d7 --- /dev/null +++ b/astro/src/components/LoadingImage.tsx @@ -0,0 +1,77 @@ +import { useEffect, useRef, useState } from "react"; + +export function LoadingImage({ + src, + slices = 8, + delay = 60, +}: { + src: string; + slices?: number; + delay?: number; +}) { + const [imageLoaded, setImageLoaded] = useState(false); + const [loadProgress, setLoadProgress] = useState(0); + const canvasRef = useRef(null); + const imageRef = useRef(null); + + const drawPartialImage = (progress: number) => { + const canvas = canvasRef.current; + const image = imageRef.current; + if (!canvas || !image) return; + + const ctx = canvas.getContext("2d"); + if (!ctx) return; + + // Clear canvas + ctx.clearRect(0, 0, canvas.width, canvas.height); + + // Calculate how much of the image to show based on progress + const sliceHeight = image.height / slices; + const height = Math.floor(sliceHeight * (progress / 100)); + + for (let i = 0; i < slices; i++) { + const y = i * sliceHeight; + ctx.drawImage( + image, + 0, y, image.width, height, // Source rectangle + 0, y, canvas.width, height // Destination rectangle + ); + } + }; + + useEffect(() => { + const img = new Image(); + img.onload = () => { + if (canvasRef.current) { + canvasRef.current.width = img.width; + canvasRef.current.height = img.height; + imageRef.current = img; + setImageLoaded(true); + + // Simulate slow loading + let progress = 0; + const interval = setInterval(() => { + progress += 2; + setLoadProgress(progress); + drawPartialImage(progress); + + if (progress >= 100) { + clearInterval(interval); + } + }, delay); // Adjust speed by changing interval time + } + }; + img.src = src; + }, [src]); + + return ( + + ); +}