Skip to content

Instantly share code, notes, and snippets.

@pugson
Created June 2, 2024 09:32
Show Gist options
  • Save pugson/a96d10de751140f7b618595c2ff65625 to your computer and use it in GitHub Desktop.
Save pugson/a96d10de751140f7b618595c2ff65625 to your computer and use it in GitHub Desktop.
client component that uses FastAverageColor
"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