Skip to content

Open source CapCut alternative.

TypeScript

fix(site): step the paused playhead onto each frame exactly

David Lecf14b25a9ce6
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 0b501a2d..bfbd003a 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -74,7 +74,7 @@ import {
import { docFieldsEdited, projectDuration, serializeDoc, storedAssets, timelineHMax, useEditor, clipLen, type VideoTrackPlacement } from "@/cut/lib/store";
import { fileLandingAt } from "@/cut/lib/timelineDrop";
import { landOnRow } from "@/cut/lib/laneTracks";
-import { playheadAt, previewAt, skimAt } from "@/cut/lib/playhead";
+import { playheadAt, previewAt, skimAt, stepFrame } from "@/cut/lib/playhead";
import type { MediaAsset } from "@/cut/lib/types";
import { AiPanel } from "./AiPanel";
import type { ChatStatus } from "./ChatStatusBadge";
@@ -1182,8 +1182,8 @@ export function Editor({
// While playing, arrows skip in 1s jumps and playback rolls on (seek
// never pauses); paused, they step a frame — 1s with Shift — for
// precise editing.
- const step = s.playing ? 1 : e.shiftKey ? 1 : 1 / 30;
- s.seek(playheadAt() + (e.key === "ArrowLeft" ? -step : step));
+ const dir = e.key === "ArrowLeft" ? -1 : 1;
+ s.seek(s.playing || e.shiftKey ? playheadAt() + dir : stepFrame(playheadAt(), dir));
} else if (e.key === "Escape") {
s.select(null);
}
diff --git a/site/src/cut/lib/playhead.test.ts b/site/src/cut/lib/playhead.test.ts
new file mode 100644
index 00000000..db6b8726
--- /dev/null
+++ b/site/src/cut/lib/playhead.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, test } from "bun:test";
+import { overlayLiveAt } from "./overlayLive";
+import { stepFrame } from "./playhead";
+
+// Paused, the arrow keys step the playhead a frame at a time. Summing 1/30
+// lands a hair under the frame: 283 steps make 9.4333…32, and a title cut in
+// at 283 / 30 would not show yet while the block under it already had.
+describe("stepping the paused playhead a frame", () => {
+ test("lands each step exactly on its frame", () => {
+ let t = 0;
+ for (let k = 1; k <= 300; k++) {
+ t = stepFrame(t, 1);
+ expect(t).toBe(k / 30);
+ }
+ for (let k = 299; k >= 0; k--) {
+ t = stepFrame(t, -1);
+ expect(t).toBe(k / 30);
+ }
+ });
+
+ test("a title that starts on the frame stepped to is on screen", () => {
+ let t = 0;
+ for (let k = 0; k < 283; k++) t = stepFrame(t, 1);
+ expect(overlayLiveAt({ start: 283 / 30, end: 9.9 }, t)).toBe(true);
+ expect(overlayLiveAt({ start: 268 / 30, end: 283 / 30 }, t)).toBe(false);
+ });
+
+ test("a playhead between frames steps to the next frame its way", () => {
+ expect(stepFrame(5.21, 1)).toBe(157 / 30);
+ expect(stepFrame(5.21, -1)).toBe(156 / 30);
+ });
+});
diff --git a/site/src/cut/lib/playhead.ts b/site/src/cut/lib/playhead.ts
index 32d99870..3cc576d1 100644
--- a/site/src/cut/lib/playhead.ts
+++ b/site/src/cut/lib/playhead.ts
@@ -49,6 +49,24 @@ export const subscribePlayhead = subscribe;
/** The playhead, in timeline seconds. */
export const playheadAt = () => at;
+/** The frame rate the paused playhead steps at. */
+const STEP_FPS = 30;
+
+/** How far off a frame, in frames, still counts as on it. */
+const ON_FRAME = 1e-6;
+
+/**
+ * The frame one step from `t`, forward for `dir` 1 and back for -1, as
+ * exactly k / 30. Adding 1/30 drifts a hair under the frame (283 steps make
+ * 9.4333…32), and a title cut in on that frame would not show yet while the
+ * block under it had. A playhead between frames steps to the next one its way.
+ */
+export function stepFrame(t: number, dir: 1 | -1): number {
+ const f = t * STEP_FPS;
+ const k = dir > 0 ? Math.floor(f + ON_FRAME) + 1 : Math.ceil(f - ON_FRAME) - 1;
+ return k / STEP_FPS;
+}
+
/** The skimmer, or null when the pointer is off the timeline. */
export const skimAt = () => skim;