function updateCanvas()

in video-background-removal/main.js [61:93]


function updateCanvas() {
  const { width, height } = canvas;

  if (!isProcessing) {
    isProcessing = true;
    (async function () {
      // Read the current frame from the video
      context.drawImage(video, 0, 0, width, height);
      const currentFrame = context.getImageData(0, 0, width, height);
      const image = new RawImage(currentFrame.data, width, height, 4);

      // Predict alpha matte
      const [output] = await pipe(image);

      // Draw the alpha matte on the canvas
      outputContext.putImageData(
        new ImageData(output.data, output.width, output.height),
        0,
        0,
      );

      if (previousTime !== undefined) {
        const fps = 1000 / (performance.now() - previousTime);
        status.textContent = `FPS: ${fps.toFixed(2)}`;
      }
      previousTime = performance.now();

      isProcessing = false;
    })();
  }

  window.requestAnimationFrame(updateCanvas);
}