Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

fix: debounce preview renders on effect/color grading changes

Augustus Otu8dec1c68d571
diff --git a/apps/web/src/components/editor/Preview.tsx b/apps/web/src/components/editor/Preview.tsx
index e2533759..6ea0a622 100644
--- a/apps/web/src/components/editor/Preview.tsx
+++ b/apps/web/src/components/editor/Preview.tsx
@@ -4197,27 +4197,34 @@ export const Preview: React.FC = () => {
]);
const lastModifiedAtRef = useRef<number>(project.modifiedAt);
+ const lastPlayheadForRenderRef = useRef<number>(playheadPosition);
+ const modifiedRenderTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const renderInFlightRef = useRef<boolean>(false);
useEffect(() => {
if (isPlaying) return;
if (isScrubbing) {
releaseScrubVideoElements();
lastModifiedAtRef.current = project.modifiedAt;
+ lastPlayheadForRenderRef.current = playheadPosition;
lastPreviewRenderTimeRef.current = playheadPosition;
return;
}
- // COMPLETELY skip rendering during resize/move interactions
- // The last rendered frame stays visible, preventing black flashing
if (isInteractingRef.current) {
lastModifiedAtRef.current = project.modifiedAt;
return;
}
- lastModifiedAtRef.current = project.modifiedAt;
const canvas = canvasRef.current;
if (!canvas) return;
+ const playheadChanged = playheadPosition !== lastPlayheadForRenderRef.current;
+ const modifiedChanged = project.modifiedAt !== lastModifiedAtRef.current;
+
+ lastModifiedAtRef.current = project.modifiedAt;
+ lastPlayheadForRenderRef.current = playheadPosition;
+
const previousRenderTime = lastPreviewRenderTimeRef.current;
const isLargeJump =
Math.abs(playheadPosition - previousRenderTime) > 1 ||
@@ -4227,14 +4234,37 @@ export const Preview: React.FC = () => {
}
lastPreviewRenderTimeRef.current = playheadPosition;
- const renderFrame = async () => {
- const rendered = await renderFrameDirectly(playheadPosition);
- if (!rendered) {
- renderFallbackFrame(playheadPosition);
+ const doRender = async () => {
+ if (renderInFlightRef.current) return;
+ renderInFlightRef.current = true;
+ try {
+ const rendered = await renderFrameDirectly(playheadPosition);
+ if (!rendered) {
+ renderFallbackFrame(playheadPosition);
+ }
+ } finally {
+ renderInFlightRef.current = false;
}
};
- renderFrame();
+ if (playheadChanged) {
+ doRender();
+ } else if (modifiedChanged) {
+ if (modifiedRenderTimerRef.current) {
+ clearTimeout(modifiedRenderTimerRef.current);
+ }
+ modifiedRenderTimerRef.current = setTimeout(() => {
+ modifiedRenderTimerRef.current = null;
+ doRender();
+ }, 150);
+ }
+
+ return () => {
+ if (modifiedRenderTimerRef.current) {
+ clearTimeout(modifiedRenderTimerRef.current);
+ modifiedRenderTimerRef.current = null;
+ }
+ };
}, [
playheadPosition,
isPlaying,