Open source CapCut alternative.
TypeScript
fix(site): align cut fade and transition preview with export
David Le23a701725bfb
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsxindex 1ec6c927..3f345572 100644--- a/site/src/cut/components/Editor.tsx+++ b/site/src/cut/components/Editor.tsx@@ -77,6 +77,8 @@ export function Editor({ projectId }: { projectId: string }) {overlays: unknown;subtitles: unknown;aspect: string;+ fadeIn: number;+ fadeOut: number;} | null = null;const unsub = useEditor.subscribe((s) => {if (!s.loaded || s.projectId !== projectId) return;@@ -87,7 +89,9 @@ export function Editor({ projectId }: { projectId: string }) {s.overlayClips !== last.overlayClips ||s.overlays !== last.overlays ||s.subtitles !== last.subtitles ||- s.aspect !== last.aspect);+ s.aspect !== last.aspect ||+ s.fadeIn !== last.fadeIn ||+ s.fadeOut !== last.fadeOut);last = {clips: s.clips,audioClips: s.audioClips,@@ -95,6 +99,8 @@ export function Editor({ projectId }: { projectId: string }) {overlays: s.overlays,subtitles: s.subtitles,aspect: s.aspect,+ fadeIn: s.fadeIn,+ fadeOut: s.fadeOut,};if (!changed) return; // first tick just primes the baselineif (timer) clearTimeout(timer);diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.tsindex 284d4bf8..e5573e8b 100644--- a/site/src/cut/hooks/usePlayback.ts+++ b/site/src/cut/hooks/usePlayback.ts@@ -2,7 +2,7 @@import { useEffect, type RefObject } from "react";import { clipSpeed, getClipSpans, projectDuration, useEditor } from "@/cut/lib/store";-import { isFullRect, rectOf, TRANSITION_ZOOM } from "@/cut/lib/types";+import { isFullRect, projectFadeSeconds, rectOf, TRANSITION_ZOOM } from "@/cut/lib/types";import type { ClipSpan, FrameRect, MediaAsset, OverlayClip, VideoClip } from "@/cut/lib/types";// The canvas backing store is the full frame resolution (1080×1920 or@@ -272,15 +272,10 @@ class Engine {// base composites over them (a regioned base leaves them showing).this.drawLayer(masterEl, masterSpan.clip, false, 1, masterZoom);if (incEl) this.drawLayer(incEl, next!.clip, false, alpha, incZoom);- if (black > 0) {- // Veil the base like the export's fade-to-black; tracks drawn after- // (above the base) stay lit.- const ctx = this.canvas.getContext("2d");- if (ctx) {- ctx.fillStyle = `rgba(0,0,0,${Math.min(1, black).toFixed(3)})`;- ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);- }- }+ // Veil only the base's own footprint, like the export's per-clip fade+ // filter: a regioned base darkens inside its rect while a below track shows+ // through the margins; tracks drawn after (above the base) stay lit.+ if (black > 0) this.fillBlackVeil(black, rectOf(masterSpan.clip));return masterEl;}@@ -309,22 +304,33 @@ class Engine {* 1→0 over the fade-out at the end of the cut. 1 when neither applies. */private projectFadeGain(t: number, total: number) {const s = useEditor.getState();+ const fadeIn = projectFadeSeconds(s.fadeIn, total);+ const fadeOut = projectFadeSeconds(s.fadeOut, total);let g = 1;- if (s.fadeIn > 0 && t < s.fadeIn) g = Math.min(g, Math.max(0, t / s.fadeIn));- if (s.fadeOut > 0 && t > total - s.fadeOut)- g = Math.min(g, Math.max(0, (total - t) / s.fadeOut));+ if (fadeIn > 0 && t < fadeIn) g = Math.min(g, Math.max(0, t / fadeIn));+ if (fadeOut > 0 && t > total - fadeOut) g = Math.min(g, Math.max(0, (total - t) / fadeOut));return Math.min(1, g);}+ /** Paint an `amount` (0..1) black veil — over the whole frame, or clipped to+ * `rect` for a per-clip fade. Shared by the edge-transition fade and the+ * whole-video project fade so the two can never drift apart. */+ private fillBlackVeil(amount: number, rect?: FrameRect) {+ if (amount <= 0) return;+ const ctx = this.canvas.getContext("2d");+ if (!ctx) return;+ const W = this.canvas.width;+ const H = this.canvas.height;+ ctx.fillStyle = `rgba(0,0,0,${Math.min(1, amount).toFixed(3)})`;+ if (rect) ctx.fillRect(rect.x * W, rect.y * H, rect.w * W, rect.h * H);+ else ctx.fillRect(0, 0, W, H);+ }+/** The picture side of the project fade: a black veil over the whole frame* (everything drawn this tick — base, tracks, at any time), matching the* export's fade on the final composite. */private drawProjectFade(gain: number) {- if (gain >= 1) return;- const ctx = this.canvas.getContext("2d");- if (!ctx) return;- ctx.fillStyle = `rgba(0,0,0,${(1 - Math.max(0, gain)).toFixed(3)})`;- ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);+ this.fillBlackVeil(1 - gain);}/** Draw the overlay tracks on one side of the base — `below` (track < 0) ordiff --git a/site/src/cut/lib/aiContext.ts b/site/src/cut/lib/aiContext.tsindex 8a5ffa76..62e3a0ab 100644--- a/site/src/cut/lib/aiContext.ts+++ b/site/src/cut/lib/aiContext.ts@@ -1,9 +1,26 @@"use client";import { getClipSpans, totalDuration, useEditor } from "./store";+import { isCrossStyle, type ClipSpan } from "./types";const r = (n: number) => Math.round(n * 100) / 100;+/** The transition this clip applies into the next one, clamped the same way the+ * preview and export are: a cross dissolve to its overlap, an edge fade/zoom to+ * the clip it ramps. Null when there's no transition or no next clip. */+function transitionToNext(sp: ClipSpan, index: number, spans: ClipSpan[]) {+ const seconds = sp.clip.transition ?? 0;+ if (seconds <= 0 || index >= spans.length - 1) return null;+ const style = sp.clip.transitionStyle ?? "crossfade";+ const next = spans[index + 1];+ const applied = isCrossStyle(style)+ ? sp.transitionOut+ : style === "fadein" || style === "zoomout"+ ? Math.min(seconds, next.len) // edge style ramps the next clip's head+ : Math.min(seconds, sp.len); // edge style ramps this clip's tail+ return { style, seconds: r(applied) };+}+/*** Compact JSON snapshot of everything the assistant should know: the cut,* the selection, what's on screen, and every user-facing setting. Sent with@@ -72,14 +89,10 @@ export function buildAiContext(opts?: { fullCues?: boolean }) {muted: sp.clip.muted,framing: sp.clip.fit ?? "fit",speed: r(sp.clip.speed ?? 1),- ...((sp.clip.transition ?? 0) > 0 && index < spans.length - 1- ? {- transitionToNext: {- style: sp.clip.transitionStyle ?? "crossfade",- seconds: r(sp.clip.transition ?? 0),- },- }- : {}),+ ...(() => {+ const t = transitionToNext(sp, index, spans);+ return t ? { transitionToNext: t } : {};+ })(),...(sp.clip.fit === "fill"? { panX: r(sp.clip.panX ?? 0), panY: r(sp.clip.panY ?? 0) }: {}),diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex f9a3d428..1acfd2d4 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -149,6 +149,14 @@ export const SPEED_MAX = 4;/** Longest transition offered; also clamps against the clips it joins. */export const TRANSITION_MAX = 2;+/** Effective whole-video fade length: the stored seconds, capped at half the+ * project so a fade-in and fade-out never overlap. The one clamp preview and+ * export both apply, so a short project fades identically in the editor and the+ * rendered file. */+export function projectFadeSeconds(fade: number | undefined, duration: number): number {+ return Math.max(0, Math.min(fade ?? 0, duration / 2));+}+/** How a clip hands off to the next one. Cross styles overlap the two clips;* edge styles ramp one side of a hard cut — the outgoing tail (fadeout,* zoomin) or the incoming head (fadein, zoomout). */diff --git a/site/src/cut/server/jobs.ts b/site/src/cut/server/jobs.tsindex e851093b..d678fe4b 100644--- a/site/src/cut/server/jobs.ts+++ b/site/src/cut/server/jobs.ts@@ -6,6 +6,7 @@ import { assertLocalRuntime } from "./local-only";import { createJobRegistry } from "./jobRegistry";import { exportsDir, mediaPath, projectDir, readProject } from "./projects";import { atempoChain, hasStream, num } from "./util";+import { projectFadeSeconds, TRANSITION_ZOOM } from "../lib/types";export interface ExportSpec {projectId: string;@@ -232,9 +233,6 @@ function clipRate(c: ExportSpec["clips"][number]) {return c.speed && c.speed > 0 ? c.speed : 1;}-/** Peak scale of zoom transitions — matches TRANSITION_ZOOM in lib/types.ts. */-const TRANSITION_ZOOM = 1.18;-/** A clip's frame region in even pixels, or null when it fills the whole frame* (the common case, which keeps the plain full-frame filter path). */function regionPx(@@ -618,8 +616,8 @@ async function runExport(job: Job, spec: ExportSpec) {// Whole-video fades on the final composite and mix, so titles, captions,// overlays, and soundtrack all fade together.- const fadeIn = Math.max(0, Math.min(spec.fadeIn ?? 0, spec.duration / 2));- const fadeOut = Math.max(0, Math.min(spec.fadeOut ?? 0, spec.duration / 2));+ const fadeIn = projectFadeSeconds(spec.fadeIn, spec.duration);+ const fadeOut = projectFadeSeconds(spec.fadeOut, spec.duration);if (fadeIn > 0.01 || fadeOut > 0.01) {const win = (f: string) =>[