Created
May 12, 2022 04:05
-
-
Save indiejoseph/32d79abe2eae1fc7a4676d1cb19ae249 to your computer and use it in GitHub Desktop.
get video color
This file contains hidden or 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
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