Skip to content

Open source CapCut alternative.

TypeScript

perf(cut): keep a lane's wash layers mounted so a film flash doesn't hold a frame

David Le7c7d49dd44fc
diff --git a/site/src/cut/components/StageEffects.tsx b/site/src/cut/components/StageEffects.tsx
index 96de0961..e22f1266 100644
--- a/site/src/cut/components/StageEffects.tsx
+++ b/site/src/cut/components/StageEffects.tsx
@@ -17,8 +17,10 @@ import { isEffectOverlay, laneOf } from "@/cut/lib/types";
import {
hasEffects,
liveEffectsAt,
+ placeWashes,
stageEffectFilter,
stageEffectTransform,
+ washSlotsOf,
type LiveEffect,
} from "@/cut/lib/effectStack";
import "./grain.css";
@@ -146,18 +148,20 @@ export function useEffectLanes(): number[] {
/** The frame-wide paints of the effects on one lane, over everything under
* them and under everything above. */
export function StageEffectPaint({ lane }: { lane: number }) {
+ const overlays = useEditor((s) => s.overlays);
+ const slots = useMemo(() => washSlotsOf(overlays, lane), [overlays, lane]);
const live = useLiveEffects();
const states = useMemo(
() => live.filter((e) => e.lane === lane).map((e) => e.state),
[live, lane]
);
- if (states.length === 0) return null;
+ if (states.length === 0 && slots.length === 0) return null;
const grainUrl = grainTileUrl();
const grain = Math.max(0, ...states.map((s) => s.grain ?? 0));
const vignette = Math.max(0, ...states.map((s) => s.vignette ?? 0));
const flash = Math.max(0, ...states.map((s) => (s.flashTone ? 0 : (s.flash ?? 0))));
const dark = Math.max(0, ...states.map((s) => (s.flashTone === "black" ? (s.flash ?? 0) : 0)));
- const washes = states.flatMap((s) => s.washes ?? []);
+ const { alphas, extra } = placeWashes(slots, states.flatMap((s) => s.washes ?? []));
const leaks = states.flatMap((s) => (s.leak ? [s.leak] : []));
const burns = states.flatMap((s) => (s.burn ? [s.burn] : []));
const bars = states.flatMap((s) => s.bars ?? []);
@@ -166,7 +170,8 @@ export function StageEffectPaint({ lane }: { lane: number }) {
!vignette &&
!flash &&
!dark &&
- washes.length === 0 &&
+ slots.length === 0 &&
+ extra.length === 0 &&
leaks.length === 0 &&
burns.length === 0 &&
bars.length === 0
@@ -210,7 +215,21 @@ export function StageEffectPaint({ lane }: { lane: number }) {
})}
</div>
))}
- {washes.map((w, i) => (
+ {slots.map((w, i) => (
+ // Mounted while the lane holds the effect, painted once: a flash
+ // moves only the opacity, on the compositor.
+ <div
+ key={`slot-${i}`}
+ className="absolute inset-0"
+ style={{
+ background: w.background,
+ opacity: alphas[i],
+ mixBlendMode: w.mode as React.CSSProperties["mixBlendMode"],
+ willChange: "opacity",
+ }}
+ />
+ ))}
+ {extra.map((w, i) => (
<div
key={i}
className="absolute inset-0"
diff --git a/site/src/cut/lib/effectStack.test.ts b/site/src/cut/lib/effectStack.test.ts
new file mode 100644
index 00000000..7f0ccc03
--- /dev/null
+++ b/site/src/cut/lib/effectStack.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, test } from "bun:test";
+import { liveEffectsAt, placeWashes, washSlotsOf } from "./effectStack";
+import type { Overlay } from "./types";
+
+const film = (id: string, start: number, lane = 0) =>
+ ({ id, kind: "effect", effect: "lightleak", leak: "film", amount: 1, start, end: start + 4 / 30, x: 0.5, y: 0.5, lane }) as Overlay;
+
+// A film flash over every cut lays five screen washes. Mounting them as the
+// flash starts and dropping them as it ends rebuilds the stage's compositing
+// each time, and the preview holds a frame; the lane keeps them mounted
+// instead, so a flash only moves their opacity.
+describe("wash slots", () => {
+ const overlays = [film("a", 1), film("b", 2), film("c", 3, 1)];
+
+ test("a lane's slots cover every wash its effects lay, one set for repeats", () => {
+ const slots = washSlotsOf(overlays, 0);
+ expect(slots.length).toBe(5);
+ expect(slots.every((s) => s.mode === "screen")).toBe(true);
+ expect(new Set(slots.map((s) => s.key)).size).toBe(5);
+ expect(washSlotsOf(overlays, 2)).toEqual([]);
+ });
+
+ test("a live flash lands in the lane's slots, and the slots stay put between flashes", () => {
+ const slots = washSlotsOf(overlays, 0);
+ const washesAt = (t: number) => liveEffectsAt(overlays, t).filter((e) => e.lane === 0).flatMap((e) => e.state.washes ?? []);
+ const mid = placeWashes(slots, washesAt(1 + 2 / 30));
+ expect(mid.extra).toEqual([]);
+ expect(mid.alphas.every((a) => a > 0)).toBe(true);
+ const between = placeWashes(slots, washesAt(1.5));
+ expect(between).toEqual({ alphas: [0, 0, 0, 0, 0], extra: [] });
+ });
+
+ test("a wash with no slot of its own still shows", () => {
+ const wash = { color: "#ffffff", alpha: 0.4, mode: "screen" as GlobalCompositeOperation };
+ expect(placeWashes([], [wash])).toEqual({ alphas: [], extra: [wash] });
+ });
+});
diff --git a/site/src/cut/lib/effectStack.ts b/site/src/cut/lib/effectStack.ts
index 17261cf6..331af727 100644
--- a/site/src/cut/lib/effectStack.ts
+++ b/site/src/cut/lib/effectStack.ts
@@ -11,7 +11,13 @@
* The math is here, apart from React, because both exports walk the same stack.
*/
-import { effectPreviewState, effectRecipe, isAudioEffect, type EffectPreviewState } from "@donkeycut/effects-kit";
+import {
+ effectPreviewState,
+ effectRecipe,
+ isAudioEffect,
+ washBackground,
+ type EffectPreviewState,
+} from "@donkeycut/effects-kit";
import { isEffectOverlay, laneOf, type Overlay } from "./types";
/** One effect live at a moment, with the lane that places it in the stack. */
@@ -40,6 +46,67 @@ export function liveEffectsAt(overlays: Overlay[], t: number): LiveEffect[] {
export const hasEffects = (overlays: Overlay[]) =>
overlays.some((o) => isEffectOverlay(o) && !isAudioEffect(o.effect));
+export type Wash = NonNullable<EffectPreviewState["washes"]>[number];
+
+/** One wash layer a lane keeps mounted: its blend and its paint, which never
+ * change while it waits — only its opacity does. */
+export interface WashSlot {
+ key: string;
+ background: string;
+ mode: GlobalCompositeOperation;
+}
+
+const washKey = (w: Wash) => `${w.mode}|${washBackground(w.color, w.area)}`;
+
+/**
+ * The wash layers a lane keeps mounted while it holds an effect that lays any.
+ *
+ * A blend layer mounted as a flash starts, and dropped as it ends, rebuilds
+ * the stage's compositing both times — a held frame in the preview for a
+ * four-frame flash. Mounted for good, at the paint each effect lays (sampled
+ * halfway through it), a flash only moves opacities. Effects that lay the same
+ * wash share its slot: a film flash over every cut is five slots, not five
+ * per cut.
+ */
+export function washSlotsOf(overlays: Overlay[], lane: number): WashSlot[] {
+ const need = new Map<string, WashSlot & { count: number }>();
+ for (const o of overlays) {
+ if (!isEffectOverlay(o) || isAudioEffect(o.effect) || o.hidden || laneOf(o) !== lane) continue;
+ const len = o.end - o.start;
+ const washes = effectPreviewState(effectRecipe(o), o.amount, len / 2, o.focus, o.ramp, len, o).washes ?? [];
+ const counts = new Map<string, number>();
+ for (const w of washes) {
+ const key = washKey(w);
+ const count = (counts.get(key) ?? 0) + 1;
+ counts.set(key, count);
+ const slot = need.get(key);
+ if (!slot) need.set(key, { key, background: washBackground(w.color, w.area), mode: w.mode, count });
+ else slot.count = Math.max(slot.count, count);
+ }
+ }
+ return [...need.values()].flatMap(({ count, ...slot }) => Array.from({ length: count }, () => slot));
+}
+
+/** The live washes laid into a lane's slots: each slot's opacity (0 when no
+ * wash is on it), and the washes no slot matches, which mount for as long as
+ * they play. */
+export function placeWashes(slots: WashSlot[], washes: Wash[]): { alphas: number[]; extra: Wash[] } {
+ const alphas = slots.map(() => 0);
+ const taken = slots.map(() => false);
+ const extra: Wash[] = [];
+ for (const w of washes) {
+ const key = washKey(w);
+ const i = slots.findIndex((s, j) => !taken[j] && s.key === key);
+ if (i < 0) {
+ extra.push(w);
+ continue;
+ }
+ taken[i] = true;
+ alphas[i] = w.alpha;
+ }
+ return { alphas, extra };
+}
+
/** How the content under an effect moves: a zoom's push in, a shake, and the
* horizontal jump that reads as tearing. Empty for the effects that only
* filter. */