mifi / reactive-video

Create videos using React!
GNU General Public License v3.0
159 stars 13 forks source link

Sporadic blank frames (canvas/img) #4

Closed mifi closed 2 years ago

mifi commented 2 years ago

Sometimes screenshot will take a screenshot before canvas/image has finished rendering.

Observations/testing:

Workarounds

Alternatives

Doesn't work

const rect = imgRef.current.getBoundingClientRect();
if (!rect.width || !rect.height) throw new Error('wh');
await new Promise((resolve, reject) => {
  const observer = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting) {
      // el is visible
      resolve();
    } else {
      // el is not visible
      reject(new Error('invis'));
    }
  });
  observer.observe(imgRef.current);
})
await new Promise((resolve, reject) => {
  let timeout2;
  const timeout = setTimeout(() => {
    clearTimeout(timeout2);
    reject(new Error('Image never completed'));
  }, 10000);

  function checkComplete() {
    if (imgRef.current.complete && imgRef.current.naturalWidth > ) {
      clearTimeout(timeout);
      resolve();
      return;
    }

    timeout2 = setTimeout(checkComplete, 100);
  }

  checkComplete();
});

HeadlessExperimental frame control

Not tried

Links

Possibly related

mifi commented 2 years ago

Update: The img bug was due to broken ffmpeg node.js stream code. After fixing that bug, I have not seen this bug again when using images instead of canvas. Because canvas is no longer used by default I'm closing this.