Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
feat(editor): crop at pixel resolution with a larger preview
超超超超超级喜欢你的达妮娅1894422fac34
diff --git a/src/components/ai-edition/Modals.tsx b/src/components/ai-edition/Modals.tsxindex aeab7b1fa..520582ee2 100644--- a/src/components/ai-edition/Modals.tsx+++ b/src/components/ai-edition/Modals.tsx@@ -10,6 +10,13 @@ import type { CropRegion } from "@/components/video-editor/types";import { useScopedT } from "@/contexts/I18nContext";import type { AxcutClip } from "@/lib/ai-edition/schema";import { formatSeconds } from "@/lib/ai-edition/timeline/format";+import {+ cropDraftFromRegion,+ cropDraftToPct,+ displayPct,+ previewBoxStyle,+ stepPct,+} from "./cropDraft";import styles from "./NewEditorShell.module.css";import type { VideoSource } from "./VirtualPreview";@@ -596,10 +603,12 @@ function CropField({label,value,onChange,+ step,}: {label: string;value: number;onChange: (n: number) => void;+ step: number;}) {return (<div style={{ display: "flex", flexDirection: "column", gap: 4, minWidth: 0 }}>@@ -615,9 +624,10 @@ function CropField({</label><inputtype="number"- value={value}+ value={displayPct(value)}min={0}max={100}+ step={step}onChange={(e) => onChange(Number(e.target.value))}style={{width: "100%",@@ -687,6 +697,7 @@ export function EditClipModal({// fraction-of-frame width/height. 16/9 is just a placeholder until the// crop <video>'s real metadata loads (see the effect below).const [videoAspectRatio, setVideoAspectRatio] = useState(16 / 9);+ const [frameSizePx, setFrameSizePx] = useState({ width: 0, height: 0 });const cropFrameRef = useRef<HTMLDivElement | null>(null);const cropVideoRef = useRef<HTMLVideoElement | null>(null);@@ -698,10 +709,11 @@ export function EditClipModal({setDraftEnd(clip.sourceEndSec ?? clip.sourceStartSec);setActiveEdge(null);const region = clip.cropRegion ?? IDENTITY_CROP;- setCropXPct(Math.round(region.x * 100));- setCropYPct(Math.round(region.y * 100));- setCropWPct(Math.round(region.width * 100));- setCropHPct(Math.round(region.height * 100));+ const pct = cropDraftToPct(cropDraftFromRegion(region));+ setCropXPct(pct.x);+ setCropYPct(pct.y);+ setCropWPct(pct.w);+ setCropHPct(pct.h);setCropTouched(false);}, [open, clip]);@@ -729,6 +741,7 @@ export function EditClipModal({if (Number.isFinite(clip.sourceStartSec)) v.currentTime = clip.sourceStartSec;if (v.videoWidth > 0 && v.videoHeight > 0) {setVideoAspectRatio(v.videoWidth / v.videoHeight);+ setFrameSizePx({ width: v.videoWidth, height: v.videoHeight });}};if (v.readyState >= 1) seek();@@ -785,10 +798,10 @@ export function EditClipModal({// field/handle logic below, keeps every later edit at that ratio).if (!candidate?.ratio) return;const fit = centeredFitPct(candidate.ratio / videoAspectRatio);- setCropXPct(Math.round(fit.x));- setCropYPct(Math.round(fit.y));- setCropWPct(Math.round(fit.w));- setCropHPct(Math.round(fit.h));+ setCropXPct(fit.x);+ setCropYPct(fit.y);+ setCropWPct(fit.w);+ setCropHPct(fit.h);};// Fraction-space width/height ratio the crop is locked to while a preset is@@ -802,11 +815,11 @@ export function EditClipModal({// independently. All keep the rectangle inside the frame.const applyCropX = (v: number) => {setCropTouched(true);- setCropXPct(Math.round(clampPct(v, 0, 100 - cropWPct)));+ setCropXPct(clampPct(v, 0, 100 - cropWPct));};const applyCropY = (v: number) => {setCropTouched(true);- setCropYPct(Math.round(clampPct(v, 0, 100 - cropHPct)));+ setCropYPct(clampPct(v, 0, 100 - cropHPct));};const applyCropW = (v: number) => {setCropTouched(true);@@ -817,10 +830,10 @@ export function EditClipModal({h = 100 - cropYPct;w = h * lockedFractionRatio;}- setCropWPct(Math.round(w));- setCropHPct(Math.round(h));+ setCropWPct(w);+ setCropHPct(h);} else {- setCropWPct(Math.round(clampPct(v, MIN_PCT, 100 - cropXPct)));+ setCropWPct(clampPct(v, MIN_PCT, 100 - cropXPct));}};const applyCropH = (v: number) => {@@ -832,10 +845,10 @@ export function EditClipModal({w = 100 - cropXPct;h = w / lockedFractionRatio;}- setCropWPct(Math.round(w));- setCropHPct(Math.round(h));+ setCropWPct(w);+ setCropHPct(h);} else {- setCropHPct(Math.round(clampPct(v, MIN_PCT, 100 - cropYPct)));+ setCropHPct(clampPct(v, MIN_PCT, 100 - cropYPct));}};@@ -853,8 +866,8 @@ export function EditClipModal({const move = (ev: PointerEvent) => {const dxPct = ((ev.clientX - startX) / r.width) * 100;const dyPct = ((ev.clientY - startY) / r.height) * 100;- setCropXPct(Math.round(clampPct(start.x + dxPct, 0, 100 - start.w)));- setCropYPct(Math.round(clampPct(start.y + dyPct, 0, 100 - start.h)));+ setCropXPct(clampPct(start.x + dxPct, 0, 100 - start.w));+ setCropYPct(clampPct(start.y + dyPct, 0, 100 - start.h));};const up = () => {window.removeEventListener("pointermove", move);@@ -924,10 +937,10 @@ export function EditClipModal({x = fixedLeft ? anchorX : anchorX - w;y = fixedTop ? anchorY : anchorY - h;}- setCropXPct(Math.round(x));- setCropYPct(Math.round(y));- setCropWPct(Math.round(w));- setCropHPct(Math.round(h));+ setCropXPct(x);+ setCropYPct(y);+ setCropWPct(w);+ setCropHPct(h);};const up = () => {window.removeEventListener("pointermove", move);@@ -951,10 +964,11 @@ export function EditClipModal({setDraftStart(clip.sourceStartSec);setDraftEnd(clip.sourceEndSec ?? clip.sourceStartSec);const region = clip.cropRegion ?? IDENTITY_CROP;- setCropXPct(Math.round(region.x * 100));- setCropYPct(Math.round(region.y * 100));- setCropWPct(Math.round(region.width * 100));- setCropHPct(Math.round(region.height * 100));+ const pct = cropDraftToPct(cropDraftFromRegion(region));+ setCropXPct(pct.x);+ setCropYPct(pct.y);+ setCropWPct(pct.w);+ setCropHPct(pct.h);setCropRatio(detectRatio(region, videoAspectRatio));setCropTouched(false);};@@ -979,21 +993,7 @@ export function EditClipModal({subtitle={assetMeta?.label ?? undefined}wide>- <div- ref={cropFrameRef}- style={{- position: "relative",- width: "100%",- height: 230,- maxWidth: 409,- margin: "0 auto 14px",- flexShrink: 0,- background: "#0a0b0e",- borderRadius: "var(--r-md)",- border: "1px solid var(--border)",- overflow: "hidden",- }}- >+ <div ref={cropFrameRef} style={previewBoxStyle(videoAspectRatio)}>{cropPreviewSource ? (<videoref={cropVideoRef}@@ -1200,10 +1200,30 @@ export function EditClipModal({alignItems: "end",}}>- <CropField label={t("cropDialog.fieldX")} value={cropXPct} onChange={applyCropX} />- <CropField label={t("cropDialog.fieldY")} value={cropYPct} onChange={applyCropY} />- <CropField label={t("cropDialog.fieldW")} value={cropWPct} onChange={applyCropW} />- <CropField label={t("cropDialog.fieldH")} value={cropHPct} onChange={applyCropH} />+ <CropField+ label={t("cropDialog.fieldX")}+ value={cropXPct}+ step={stepPct(frameSizePx.width)}+ onChange={applyCropX}+ />+ <CropField+ label={t("cropDialog.fieldY")}+ value={cropYPct}+ step={stepPct(frameSizePx.height)}+ onChange={applyCropY}+ />+ <CropField+ label={t("cropDialog.fieldW")}+ value={cropWPct}+ step={stepPct(frameSizePx.width)}+ onChange={applyCropW}+ />+ <CropField+ label={t("cropDialog.fieldH")}+ value={cropHPct}+ step={stepPct(frameSizePx.height)}+ onChange={applyCropH}+ /><div style={{ display: "flex", flexDirection: "column", gap: 4, minWidth: 110 }}><labelstyle={{@@ -1244,7 +1264,7 @@ export function EditClipModal({whiteSpace: "nowrap",}}>- {cropWPct}% × {cropHPct}%+ {displayPct(cropWPct)}% × {displayPct(cropHPct)}%</span></div></div>diff --git a/src/components/ai-edition/cropDraft.test.ts b/src/components/ai-edition/cropDraft.test.tsnew file mode 100644index 000000000..90542f346--- /dev/null+++ b/src/components/ai-edition/cropDraft.test.ts@@ -0,0 +1,54 @@+import { describe, expect, it } from "vitest";+import {+ cropDraftFromRegion,+ displayPct,+ PREVIEW_MAX_HEIGHT_PX,+ previewBoxStyle,+ stepPct,+} from "./cropDraft";++describe("crop draft helpers", () => {+ it("steps one source pixel, not 1%, when the frame width is known", () => {+ expect(stepPct(1920)).toBe(100 / 1920);+ expect(stepPct(1920)).not.toBe(1);+ });++ it("falls back to a fine percent step when the frame size is unknown", () => {+ expect(stepPct(0)).toBe(0.1);+ });++ it("gives the preview box the video's own aspect ratio", () => {+ // The overlay and every drag are measured against the box while the+ // video letterboxes inside it — any box/video aspect mismatch shifts+ // the crop off the pixels it claims to select.+ for (const aspect of [16 / 9, 784 / 1082, 21 / 9]) {+ const style = previewBoxStyle(aspect);+ expect(style.aspectRatio).toBe(`${aspect}`);+ // Width is derived from the height cap so a clamped height can+ // never silently break the ratio (CSS keeps width and drops the+ // ratio when max-height wins).+ expect(style.width).toBe(`min(100%, calc(${PREVIEW_MAX_HEIGHT_PX}px * ${aspect}))`);+ }+ });++ it("falls back to 16:9 while the video metadata has not loaded", () => {+ for (const bogus of [0, -3, Number.NaN, Number.POSITIVE_INFINITY]) {+ expect(previewBoxStyle(bogus).aspectRatio).toBe(`${16 / 9}`);+ }+ });++ it("stores the draft as unrounded fractions", () => {+ const draft = cropDraftFromRegion({ x: 1 / 3, y: 0.1, width: 0.5, height: 0.8 });+ expect(draft.x).toBe(1 / 3);+ expect(draft.x).not.toBe(33);+ });++ it("formats field display to two decimals without touching the stored value", () => {+ expect(displayPct((1 / 3) * 100)).toBe(33.33);+ expect(displayPct(100)).toBe(100);+ expect(displayPct(0)).toBe(0);+ // One source pixel on a 1920-wide frame is ~0.052% — two display+ // decimals stay within a fifth of a pixel of the stored fraction.+ expect(Math.abs(displayPct(33.333333) - 33.333333)).toBeLessThan(stepPct(1920) / 5);+ });+});diff --git a/src/components/ai-edition/cropDraft.ts b/src/components/ai-edition/cropDraft.tsnew file mode 100644index 000000000..ff09b314f--- /dev/null+++ b/src/components/ai-edition/cropDraft.ts@@ -0,0 +1,80 @@+// Pure crop-draft helpers for Edit Clip. The modal stores the rectangle as+// frame fractions (0–1), not rounded integer percents, so a 1px nudge is+// representable once the source size is known.++export interface CropDraft {+ x: number;+ y: number;+ width: number;+ height: number;+}++export function cropDraftFromRegion(region: CropDraft): CropDraft {+ return { x: region.x, y: region.y, width: region.width, height: region.height };+}++export function cropDraftToPct(draft: CropDraft): { x: number; y: number; w: number; h: number } {+ return {+ x: draft.x * 100,+ y: draft.y * 100,+ w: draft.width * 100,+ h: draft.height * 100,+ };+}++/** Percent step for a numeric field: one source pixel when the frame size is known. */+export function stepPct(frameSizePx: number): number {+ return frameSizePx > 0 ? 100 / frameSizePx : 0.1;+}++/**+ * What the numeric fields DISPLAY. The draft itself stays unrounded (that is+ * the point of pixel-precision crops), but a drag leaves values like+ * 33.33333333333333 behind, and eight digits of float noise in a percent+ * field reads as a bug. Two decimals is 0.01% — under a fifth of a pixel on+ * a 1920-wide source — so the display can never be visibly off from the+ * stored value. Typing writes the exact typed number; this only formats.+ */+export function displayPct(value: number): number {+ return Math.round(value * 100) / 100;+}++export const PREVIEW_MAX_HEIGHT_PX = 360;++/**+ * Preview box for the crop overlay. The crop rectangle and every drag are+ * measured against THIS element, while the <video> inside it letterboxes via+ * object-fit: contain — so the box must have the video's own aspect ratio or+ * the overlay drifts off the pixels it claims to crop (the old fixed+ * 409x230 box was 16:9 for exactly this reason). Width is therefore derived+ * from the height cap and the aspect: `min(100%, height-cap * aspect)`.+ * Declaring width this way — rather than `width: 100%` plus a max-height —+ * matters because when a max-height clamps an aspect-ratio box, CSS keeps+ * the width and BREAKS the ratio, which would letterbox portrait sources+ * all over again.+ */+export function previewBoxStyle(videoAspectRatio: number): {+ position: "relative";+ width: string;+ aspectRatio: string;+ margin: string;+ flexShrink: number;+ background: string;+ borderRadius: string;+ border: string;+ overflow: "hidden";+} {+ const aspect =+ Number.isFinite(videoAspectRatio) && videoAspectRatio > 0 ? videoAspectRatio : 16 / 9;+ return {+ position: "relative",+ width: `min(100%, calc(${PREVIEW_MAX_HEIGHT_PX}px * ${aspect}))`,+ aspectRatio: `${aspect}`,+ margin: "0 auto 14px",+ flexShrink: 0,+ background: "#0a0b0e",+ borderRadius: "var(--r-md)",+ border: "1px solid var(--border)",+ overflow: "hidden",+ };+}