Skip to content

🎥 Make videos programmatically with React

Fix timeline flicker during zoom

Jonny Burger13634e7ebaca
diff --git a/packages/studio/src/components/Timeline/TimelineViewport.tsx b/packages/studio/src/components/Timeline/TimelineViewport.tsx
index 583114e836..964912c8ce 100644
--- a/packages/studio/src/components/Timeline/TimelineViewport.tsx
+++ b/packages/studio/src/components/Timeline/TimelineViewport.tsx
@@ -13,6 +13,12 @@ type TimelineRenderWindow = {
export const TimelineViewportContext =
createContext<TimelineRenderWindow | null>(null);
+let updateTimelineViewport: (() => void) | null = null;
+
+export const syncTimelineViewport = () => {
+ updateTimelineViewport?.();
+};
+
export const getTimelineRenderWindow = (
scrollable: HTMLDivElement,
): TimelineRenderWindow => {
@@ -63,6 +69,7 @@ export const TimelineViewportProvider: React.FC<{
useLayoutEffect(() => {
let element: HTMLDivElement | null = null;
let resizeObserver: ResizeObserver | null = null;
+ updateTimelineViewport = update;
const animationFrame = requestAnimationFrame(() => {
element = scrollable.current;
if (!element) {
@@ -76,6 +83,10 @@ export const TimelineViewportProvider: React.FC<{
});
return () => {
+ if (updateTimelineViewport === update) {
+ updateTimelineViewport = null;
+ }
+
cancelAnimationFrame(animationFrame);
resizeObserver?.disconnect();
element?.removeEventListener('scroll', update);
diff --git a/packages/studio/src/state/timeline-zoom.tsx b/packages/studio/src/state/timeline-zoom.tsx
index 0adeebc957..43efa7e891 100644
--- a/packages/studio/src/state/timeline-zoom.tsx
+++ b/packages/studio/src/state/timeline-zoom.tsx
@@ -6,6 +6,7 @@ import {
} from '../components/Timeline/imperative-state';
import {scrollableRef} from '../components/Timeline/timeline-refs';
import {prepareToPreserveTimelineCursor} from '../components/Timeline/timeline-scroll-logic';
+import {syncTimelineViewport} from '../components/Timeline/TimelineViewport';
import {getZoomFromLocalStorage} from '../components/ZoomPersistor';
import {
clampTimelineZoom,
@@ -74,6 +75,9 @@ export const TimelineZoomContext: React.FC<{
// The new scroll range exists now, so the browser will not clamp the offset
// against the previous width.
preserveTimelineCursor();
+ // A large zoom can move beyond the old horizontal render window. Commit
+ // the new window before the browser paints the new scroll position.
+ flushSync(syncTimelineViewport);
},
[],
);