Skip to content

Instantly share code, notes, and snippets.

@indiejoseph
Created May 12, 2022 04:05
Show Gist options
  • Save indiejoseph/32d79abe2eae1fc7a4676d1cb19ae249 to your computer and use it in GitHub Desktop.
Save indiejoseph/32d79abe2eae1fc7a4676d1cb19ae249 to your computer and use it in GitHub Desktop.
get video color
useEffect(() => {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d', {
alpha: false,
});
const colorSampling = () => {
const { videoWidth, videoHeight } = videoRef.current;
context.drawImage(videoRef.current, 0, 0, videoWidth, videoHeight);
setTimeout(() => {
const firstPixel = context.getImageData(0, 0, 1, 1).data;
const rgbColor = `rgb(${firstPixel[0]}, ${firstPixel[1]}, ${firstPixel[2]})`;
console.log(rgbColor);
}, 100);
};
if (videoRef.current.readyState === 4) {
colorSampling();
} else {
videoRef.current.addEventListener('loadedmetadata', colorSampling);
}
return () => {
canvas.remove();
};
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment