Skip to content

Commit

Permalink
add requestAnimationFrame to video element
Browse files Browse the repository at this point in the history
  • Loading branch information
below43 committed Nov 4, 2024
1 parent 99d5271 commit 5b46a85
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/app/services/camera.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,29 @@ export class CameraService
const videoHeight = (videoElement.videoHeight / videoElement.videoWidth) * width;
canvas.setAttribute("width", width.toString());
canvas.setAttribute("height", videoHeight.toString());

// Use requestAnimationFrame to keep the video element updated
const updateVideo = () => {
if (!videoElement.paused && !videoElement.ended) {
videoElement.play();
requestAnimationFrame(updateVideo);
}
};
requestAnimationFrame(updateVideo);
},
false,
);

videoElement.addEventListener(
"error",
(err) => {
console.error("Error with video element:", err);
// Try reassigning the srcObject
videoElement.srcObject = null;
videoElement.srcObject = stream;
},
false
);

this.clearPhoto(canvas, photo);
}
Expand Down

0 comments on commit 5b46a85

Please sign in to comment.