Skip to content

Open source CapCut alternative.

TypeScript

fix(site): color preset swatches stop flickering

David Le6065b6b8cbcb
diff --git a/site/src/cut/components/ColorPanel.tsx b/site/src/cut/components/ColorPanel.tsx
index 4e5886f8..9e2304fc 100644
--- a/site/src/cut/components/ColorPanel.tsx
+++ b/site/src/cut/components/ColorPanel.tsx
@@ -174,7 +174,16 @@ function PresetView({
contentClassName="grid grid-cols-2 gap-2 px-3.5 pt-1 pb-2"
>
{presets.map((p) => (
- <PresetTile key={p.id} preset={p} frame={frame} selected={active?.id === p.id} onPick={pick} />
+ // Keyed by the clip too: a tile holds its last thumb across a
+ // moving playhead, and that hold has to break when the footage
+ // under it changes, or the swatches show the clip just left.
+ <PresetTile
+ key={`${clip.id}:${p.id}`}
+ preset={p}
+ frame={frame}
+ selected={active?.id === p.id}
+ onPick={pick}
+ />
))}
</ScrollArea>
<div className="shrink-0 border-t border-border bg-card px-3.5 py-1">
@@ -277,7 +286,14 @@ function PresetTile({
if (presetThumbCache.size > 128)
for (const k of presetThumbCache.keys())
if (!k.endsWith(`|${frame}`)) presetThumbCache.delete(k);
- bump((n) => n + 1);
+ // Decoded before the swap, so the tile flips straight from the old
+ // thumb to a ready bitmap.
+ const pre = new Image();
+ pre.src = url;
+ const land = () => {
+ if (!gone) bump((n) => n + 1);
+ };
+ pre.decode().then(land, land);
};
img.src = frame;
return () => {
@@ -285,7 +301,12 @@ function PresetTile({
};
}, [key, frame, preset]);
- const src = needsLut ? (key ? presetThumbCache.get(key) ?? null : null) : frame;
+ const fresh = needsLut ? (key ? presetThumbCache.get(key) ?? null : null) : frame;
+ // The last thumb this tile showed holds while the fresh frame's is still
+ // rendering, so a moving playhead never drops the tile to its stand-in.
+ const [held, setHeld] = useState<string | null>(null);
+ if (fresh && fresh !== held) setHeld(fresh);
+ const src = fresh ?? held;
return (
<button
type="button"
@@ -298,7 +319,7 @@ function PresetTile({
>
<span
className={cn(
- "relative block aspect-video w-full overflow-hidden rounded-md bg-muted",
+ "relative block aspect-square w-full overflow-hidden rounded-md bg-muted",
selected && "ring-2 ring-[#0a84ff] ring-offset-1 ring-offset-card"
)}
>
diff --git a/site/src/cut/components/usePlayheadFrame.ts b/site/src/cut/components/usePlayheadFrame.ts
index 3fc6da0e..0a4fe22a 100644
--- a/site/src/cut/components/usePlayheadFrame.ts
+++ b/site/src/cut/components/usePlayheadFrame.ts
@@ -81,22 +81,64 @@ export function snapshotClipSource(clipId: string): string | null {
}
}
-/** The clip's ungraded frame under the playhead, re-sampled as it moves — the
- * base every color preset swatch paints its own recipe onto. */
-export function useClipSourceFrame(clipId: string): string | null {
- const [frame, setFrame] = useState<string | null>(null);
- const tick = usePreviewTimeEvery(5);
+/** Sampling beats per second while the playhead is being moved by hand. */
+const SCRUB_BEATS = 5;
+/** Beats folded into one sample while playback runs: the swatches want
+ * variety, and once a second is variety enough. */
+const PLAY_BEAT = 5;
+
+/**
+ * The swatch feed's shared schedule and hand-off. Playing publishes about
+ * once a second; paused publishes on every move, so the tiles land on the
+ * exact frame under the playhead — including the frame playback stops on,
+ * which gets its own sample when `playing` flips. Every URL is decoded before
+ * it publishes: an <img> handed an undecoded one paints blank for a beat,
+ * which is what made the swatches flicker.
+ */
+function useSampledFrame(
+ snapshot: () => string | null,
+ enabled: boolean,
+ key: unknown,
+ initial: string | null = null,
+ onShot?: (url: string) => void
+): string | null {
+ const [frame, setFrame] = useState<string | null>(initial);
+ const playing = useEditor((s) => s.playing);
+ const tick = usePreviewTimeEvery(SCRUB_BEATS);
const epoch = useEditor((s) => s.loadEpoch);
+ const beat = playing ? Math.floor(tick / PLAY_BEAT) : tick;
useEffect(() => {
+ if (!enabled) return;
+ let gone = false;
+ // A beat after the move, so the decoder has painted the frame.
const t = setTimeout(() => {
- const shot = snapshotClipSource(clipId);
- if (shot) setFrame(shot);
+ const shot = snapshot();
+ if (!shot) return;
+ const img = new Image();
+ img.src = shot;
+ const land = () => {
+ if (gone) return;
+ onShot?.(shot);
+ setFrame(shot);
+ };
+ img.decode().then(land, land);
}, 160);
- return () => clearTimeout(t);
- }, [tick, epoch, clipId]);
+ return () => {
+ gone = true;
+ clearTimeout(t);
+ };
+ // snapshot and onShot read live state; the beat is the schedule.
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [beat, playing, epoch, enabled, key]);
return frame;
}
+/** The clip's ungraded frame under the playhead, re-sampled as it moves — the
+ * base every color preset swatch paints its own recipe onto. */
+export function useClipSourceFrame(clipId: string): string | null {
+ return useSampledFrame(() => snapshotClipSource(clipId), true, clipId);
+}
+
/** The last sampled frame, kept across tab switches: a panel unmounts when
* another tab opens, and a fresh mount seeds from here so the tiles paint the
* real picture at once with no pass through the stand-in. */
@@ -107,24 +149,17 @@ let lastFrame: { project: string; url: string } | null = null;
* decoder has painted); null until the preview has a picture. */
export function usePlayheadFrame(): string | null {
const projectId = useEditor((s) => s.projectId);
- const [frame, setFrame] = useState<string | null>(() =>
- lastFrame && lastFrame.project === projectId ? lastFrame.url : null
- );
- const tick = usePreviewTimeEvery(5);
- const epoch = useEditor((s) => s.loadEpoch);
// The tile shows the live frame, which a cut of titles and shapes over the
// background has as much as one made of footage.
const hasPicture = useEditor((s) => projectDuration(s) > 0);
- useEffect(() => {
- if (!hasPicture || !projectId) return;
- const t = setTimeout(() => {
- const shot = snapshotPreview();
- if (shot) {
- lastFrame = { project: projectId, url: shot };
- setFrame(shot);
- }
- }, 160);
- return () => clearTimeout(t);
- }, [tick, epoch, hasPicture, projectId]);
+ const frame = useSampledFrame(
+ snapshotPreview,
+ hasPicture && !!projectId,
+ projectId,
+ lastFrame && lastFrame.project === projectId ? lastFrame.url : null,
+ (url) => {
+ if (projectId) lastFrame = { project: projectId, url };
+ }
+ );
return hasPicture ? frame : null;
}