Created
June 2, 2024 09:32
-
-
Save pugson/a96d10de751140f7b618595c2ff65625 to your computer and use it in GitHub Desktop.
client component that uses FastAverageColor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import { FastAverageColor } from "fast-average-color"; | |
import { useEffect, useState } from "react"; | |
const fac = new FastAverageColor(); | |
export function ImageWithAvgColor({ src }: { src: string }) { | |
const [avgColor, setAvgColor] = useState<string>("transparent"); | |
useEffect(() => { | |
fac | |
.getColorAsync(src) | |
.then((color) => { | |
setAvgColor(color.rgba); | |
}) | |
.catch((e) => { | |
console.log(e); | |
}); | |
}, []); | |
return ( | |
<div className="p-12 bg-white/10 relative"> | |
<div | |
className="absolute inset-0 z-10" | |
style={{ | |
backgroundColor: avgColor, | |
}} | |
></div> | |
<img src={src} alt="" className="inline-block w-full h-full relative z-20" /> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment