Hello, I’ve been trying to render this video on my site with a transparent background. After some research I managed to stumble upon this resource which let me get my desired effect using a canvas.
Using the B&W mask on the right side of the video, I can set the pixels I’d like to be transparent. Issue is my current method at times stutters or has low framerate. How would I go about optimizing this or taking a different approach entirely? I feel like having a second canvas just to draw then read the pixels seems inefficient.
Also to note, for whatever reason my rendered image seems to be squished compared to the original.
Here is my current code
const canvas = this.$refs.canvas.getContext("2d", { willReadFrequently: true })
const render = this.$refs.render.getContext("2d", { willReadFrequently: true })
const video = this.$refs.video
video.play()
function maskVideo() {
canvas.drawImage(video, 0, 0, 3840, 1280)
const frame = canvas.getImageData(0, 0, 1920, 1280)
const frameData = frame.data
for (var i = 0; i < frameData.length; i += 4) {
const [r, g, b] = [frameData[i], frameData[i + 1], frameData[i + 2]]
frameData[i + 3] = (r + g + b)
}
render.putImageData(frame, 0, 0)
window.requestAnimationFrame(maskVideo)
}
window.requestAnimationFrame(maskVideo)