Skip to content

Open source CapCut alternative.

TypeScript

feat(site): transition tiles preview the cut sharp and project-shaped

David Le3b787352775b
diff --git a/site/src/cut/components/TransitionsPanel.tsx b/site/src/cut/components/TransitionsPanel.tsx
index bb46026c..641f52e7 100644
--- a/site/src/cut/components/TransitionsPanel.tsx
+++ b/site/src/cut/components/TransitionsPanel.tsx
@@ -10,10 +10,11 @@ import {
SelectValue,
} from "@/components/ui/select";
import { Slider } from "@/components/ui/slider";
-import { SwatchScene, useSwatchClock } from "@/cut/components/EffectsPanel";
+import { SwatchScene, useSwatchClock, useSwatchRatio } from "@/cut/components/EffectsPanel";
import { parseSecondsInput, ScrubValue } from "@/cut/components/ScrubValue";
import { SectionTitle } from "@/cut/components/SectionTitle";
import { clearElementDrag, setElementDragData } from "@/cut/lib/assetDrag";
+import { peekEdgeFrame, requestEdgeFrame } from "@/cut/lib/media";
import { PICKED_RING, pickGridNav, useAssetPick } from "@/cut/lib/assetPick";
import { getClipSpans, resolveTransitions, useEditor } from "@/cut/lib/store";
import {
@@ -104,6 +105,36 @@ function frameAt(asset: MediaAsset, srcT: number): string | null {
return null;
}
+/** Tile-height capture for the swatch previews: covers a 2× retina portrait
+ * tile whichever way the source is oriented. */
+const TILE_FRAME_H = 640;
+
+/** One side of the cut: the sharp capture at the exact source time once it
+ * lands, the nearest filmstrip thumb (or the image itself) until then. */
+function useCutFrame(
+ slot: string,
+ side: { asset: MediaAsset; srcT: number } | null
+): string | null {
+ const url = side?.asset.type === "video" ? side.asset.url : null;
+ const time = side?.srcT ?? 0;
+ const id = url ? `${url}#${time.toFixed(2)}` : "";
+ const cached = url ? peekEdgeFrame(url, time, TILE_FRAME_H) : null;
+ const [frame, setFrame] = useState<{ id: string; src: string } | null>(null);
+ useEffect(() => {
+ if (!url || cached) return;
+ let live = true;
+ void requestEdgeFrame(slot, url, time, TILE_FRAME_H).then((src) => {
+ if (live && src) setFrame({ id, src });
+ });
+ return () => {
+ live = false;
+ };
+ }, [url, time, slot, id, cached]);
+ if (!side) return null;
+ const sharp = cached ?? (frame?.id === id ? frame.src : null);
+ return sharp ?? frameAt(side.asset, time);
+}
+
/** The two frames either side of the cut nearest the playhead — the real
* footage a transition dropped there would blend. Null sides fall back to the
* tiles' stand-in scenes. */
@@ -112,24 +143,29 @@ function useCutFrames(): { a: string | null; b: string | null } {
const assets = useEditor((s) => s.assets);
// Re-pick the cut as the playhead crosses half-second marks.
const tick = useEditor((s) => Math.round(s.currentTime * 2));
- return useMemo(() => {
+ const cut = useMemo(() => {
const spans = getClipSpans(clips, assets);
- let best: { a: string | null; b: string | null } | null = null;
+ let best: {
+ a: { asset: MediaAsset; srcT: number };
+ b: { asset: MediaAsset; srcT: number };
+ } | null = null;
let bestDist = Infinity;
for (let i = 0; i + 1 < spans.length; i++) {
const out = spans[i];
const inc = spans[i + 1];
- const cut = inc.start;
- const dist = Math.abs(tick / 2 - cut);
+ const dist = Math.abs(tick / 2 - inc.start);
if (dist >= bestDist) continue;
bestDist = dist;
best = {
- a: frameAt(out.asset, out.clip.in + out.len * (out.clip.speed ?? 1)),
- b: frameAt(inc.asset, inc.clip.in),
+ a: { asset: out.asset, srcT: out.clip.in + out.len * (out.clip.speed ?? 1) },
+ b: { asset: inc.asset, srcT: inc.clip.in },
};
}
- return best ?? { a: null, b: null };
+ return best;
}, [clips, assets, tick]);
+ const a = useCutFrame("xtile-a", cut?.a ?? null);
+ const b = useCutFrame("xtile-b", cut?.b ?? null);
+ return { a, b };
}
/** How a transition tile's loop spends its time: a beat on the first shot,
@@ -190,7 +226,7 @@ function TransitionTile({
a={a}
b={b}
t={t}
- className={cn("h-24 w-full rounded-lg border border-border", marked && PICKED_RING)}
+ className={cn("w-full rounded-lg border border-border", marked && PICKED_RING)}
/>
<span className="leading-none">{TRANSITION_STYLE_LABELS[style]}</span>
</button>
@@ -231,6 +267,7 @@ function TransitionSwatch({
t: number;
className?: string;
}) {
+ const ratio = useSwatchRatio();
const p0 = Math.min(1, Math.max(0, (t - X_HOLD) / X_RUN));
const p = p0 * p0 * (3 - 2 * p0);
const pc = (n: number) => `${(n * 100).toFixed(1)}%`;
@@ -375,7 +412,10 @@ function TransitionSwatch({
break;
}
return (
- <span className={cn("relative block overflow-hidden rounded-md bg-black", className)}>
+ <span
+ className={cn("relative block overflow-hidden rounded-md bg-black", className)}
+ style={{ aspectRatio: ratio }}
+ >
{layers}
</span>
);
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.ts
index f1725f15..a0b34d19 100644
--- a/site/src/cut/lib/media.ts
+++ b/site/src/cut/lib/media.ts
@@ -1142,32 +1142,43 @@ const EDGE_POOL_CAP = 4;
type EdgeRequest = {
url: string;
time: number;
+ height: number;
key: string;
resolvers: ((src: string | null) => void)[];
};
-/** A file held open for repeated frame reads, with the sink that decodes it. */
-type EdgeReader = { input: ReturnType<typeof openMedia>; sink: ReturnType<typeof frameSink> };
+/** A file held open for repeated frame reads, with a decoding sink per capture
+ * height — trim edges and tile previews read different sizes from the same
+ * warm reader. */
+type EdgeReader = {
+ input: ReturnType<typeof openMedia>;
+ sinkFor: (height: number) => ReturnType<typeof frameSink>;
+};
const edgeCache = new Map<string, string>();
const edgeQueue = new Map<string, EdgeRequest>();
const edgePool = new Map<string, Promise<EdgeReader>>();
let edgePumping = false;
-function edgeKey(url: string, time: number) {
- return `${url}#${time.toFixed(2)}`;
+function edgeKey(url: string, time: number, height: number) {
+ return `${url}#${time.toFixed(2)}@${height}`;
}
/** Synchronous cache read — the frame if a matching capture already landed. */
-export function peekEdgeFrame(url: string, time: number): string | null {
- return edgeCache.get(edgeKey(url, time)) ?? null;
+export function peekEdgeFrame(url: string, time: number, height = THUMB_H): string | null {
+ return edgeCache.get(edgeKey(url, time, height)) ?? null;
}
/** Capture the frame at `time`, latest-wins per `slot` (a clip edge). Resolves
* with the frame, or null when superseded by a newer request or on a failed
* read. */
-export function requestEdgeFrame(slot: string, url: string, time: number): Promise<string | null> {
- const key = edgeKey(url, time);
+export function requestEdgeFrame(
+ slot: string,
+ url: string,
+ time: number,
+ height = THUMB_H
+): Promise<string | null> {
+ const key = edgeKey(url, time, height);
const hit = edgeCache.get(key);
if (hit) return Promise.resolve(hit);
return new Promise((resolve) => {
@@ -1176,7 +1187,7 @@ export function requestEdgeFrame(slot: string, url: string, time: number): Promi
prev.resolvers.push(resolve);
} else {
prev?.resolvers.forEach((r) => r(null));
- edgeQueue.set(slot, { url, time, key, resolvers: [resolve] });
+ edgeQueue.set(slot, { url, time, height, key, resolvers: [resolve] });
}
void pumpEdgeFrames();
});
@@ -1197,9 +1208,18 @@ function edgeReader(url: string): Promise<EdgeReader> {
input.dispose();
throw new UnreadableMediaError("This file has no readable video.");
}
- // A pool of canvases the sink cycles through, so a drag that reads hundreds
- // of frames keeps its allocation flat.
- return { input, sink: frameSink(track, { height: THUMB_H }, 4) };
+ // A pool of canvases each sink cycles through, so a drag that reads
+ // hundreds of frames keeps its allocation flat.
+ const sinks = new Map<number, ReturnType<typeof frameSink>>();
+ const sinkFor = (height: number) => {
+ let sink = sinks.get(height);
+ if (!sink) {
+ sink = frameSink(track, { height }, 4);
+ sinks.set(height, sink);
+ }
+ return sink;
+ };
+ return { input, sinkFor };
})();
edgePool.set(url, opening);
while (edgePool.size > EDGE_POOL_CAP) {
@@ -1222,8 +1242,8 @@ async function pumpEdgeFrames() {
let src = edgeCache.get(req.key) ?? null;
if (!src) {
try {
- const { sink } = await edgeReader(req.url);
- const frame = await sink.getCanvas(Math.max(0, req.time));
+ const { sinkFor } = await edgeReader(req.url);
+ const frame = await sinkFor(req.height).getCanvas(Math.max(0, req.time));
if (!frame) throw new Error("No frame at that time.");
src = await canvasDataUrl(frame.canvas, "image/jpeg", 0.92);
edgeCache.set(req.key, src);