Skip to content

Open source CapCut alternative.

TypeScript

fix(site): wrap turned titles to the room they run along [rebuild]

David Lef04e44381620
diff --git a/evals/cut-perf.latest-report.json b/evals/cut-perf.latest-report.json
index 35559705..0642d067 100644
--- a/evals/cut-perf.latest-report.json
+++ b/evals/cut-perf.latest-report.json
@@ -51,10 +51,10 @@
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.7,
- "p95": 32.5,
+ "p50": 16,
+ "p95": 32.3,
"mean": 18.48,
- "max": 33.5,
+ "max": 33.9,
"n": 41
},
"longTasks": {
@@ -64,7 +64,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 57,
+ "liveSamples": 35,
"machine": "desktop"
},
{
@@ -74,17 +74,17 @@
"notes": [],
"late": {
"p50": 0,
- "p95": 0,
- "mean": 0,
- "max": 0,
+ "p95": 1,
+ "mean": 0.12,
+ "max": 1,
"n": 41
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.3,
- "p95": 17.4,
- "mean": 15.43,
- "max": 17.6,
+ "p50": 15.8,
+ "p95": 32.5,
+ "mean": 17.7,
+ "max": 33.3,
"n": 41
},
"longTasks": {
@@ -94,7 +94,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 65,
+ "liveSamples": 45,
"machine": "desktop"
},
{
@@ -112,9 +112,9 @@
"frameMs": 16.7,
"scrub": {
"p50": 15.5,
- "p95": 17.5,
- "mean": 15.89,
- "max": 32.1,
+ "p95": 17.4,
+ "mean": 15.9,
+ "max": 30.8,
"n": 41
},
"longTasks": {
@@ -124,7 +124,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 77,
+ "liveSamples": 76,
"machine": "desktop"
},
{
@@ -141,10 +141,10 @@
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.4,
- "p95": 17.2,
- "mean": 15.1,
- "max": 33,
+ "p50": 15.5,
+ "p95": 17.5,
+ "mean": 15.66,
+ "max": 32.3,
"n": 81
},
"longTasks": {
@@ -165,16 +165,16 @@
"late": {
"p50": 0,
"p95": 1,
- "mean": 0.2,
- "max": 2,
+ "mean": 0.17,
+ "max": 1,
"n": 41
},
"frameMs": 16.7,
"scrub": {
"p50": 15.7,
"p95": 32.8,
- "mean": 18.84,
- "max": 48.6,
+ "mean": 18.39,
+ "max": 33.8,
"n": 41
},
"longTasks": {
@@ -184,7 +184,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 63,
+ "liveSamples": 45,
"machine": "desktop"
},
{
@@ -195,16 +195,16 @@
"late": {
"p50": 0,
"p95": 1,
- "mean": 0.15,
- "max": 1,
+ "mean": 0.2,
+ "max": 2,
"n": 41
},
"frameMs": 16.7,
"scrub": {
- "p50": 15.5,
+ "p50": 15.9,
"p95": 32,
- "mean": 17.9,
- "max": 33.3,
+ "mean": 18.77,
+ "max": 50.1,
"n": 41
},
"longTasks": {
@@ -214,7 +214,7 @@
"max": 0,
"n": 0
},
- "liveSamples": 65,
+ "liveSamples": 46,
"machine": "desktop"
},
{
@@ -225,16 +225,16 @@
"late": {
"p50": 1,
"p95": 1,
- "mean": 0.88,
+ "mean": 0.93,
"max": 1,
"n": 41
},
"frameMs": 16.7,
"scrub": {
- "p50": 32.1,
- "p95": 33.1,
- "mean": 30.01,
- "max": 33.2,
+ "p50": 32.3,
+ "p95": 33.5,
+ "mean": 31.07,
+ "max": 33.6,
"n": 41
},
"longTasks": {
@@ -289,13 +289,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 811,
+ "presented": 810,
"lag": {
"p50": 0.02,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 811
+ "n": 810
},
"stallShare": 0,
"decay": {
@@ -326,13 +326,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 3237,
+ "presented": 3241,
"lag": {
"p50": 0.02,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 3237
+ "n": 3241
},
"stallShare": 0,
"decay": {
@@ -363,13 +363,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 222,
+ "presented": 221,
"lag": {
- "p50": 0.02,
+ "p50": 0.01,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 222
+ "n": 221
},
"stallShare": 0,
"decay": {
@@ -436,30 +436,30 @@
"bucket": "playback",
"pass": true,
"notes": [],
- "drops": 5,
- "presented": 3404,
+ "drops": 0,
+ "presented": 3444,
"lag": {
"p50": 0.01,
"p95": 0.02,
"mean": 0.01,
- "max": 0.22,
- "n": 3404
+ "max": 0.02,
+ "n": 3444
},
"stallShare": 0,
"decay": {
"first": 0,
- "last": 0.004
+ "last": 0
},
"clock": {
"lead": 0.04,
"reported": 0.04
},
"longTasks": {
- "p50": 61,
- "p95": 354,
- "mean": 127.4,
- "max": 354,
- "n": 5
+ "p50": 0,
+ "p95": 0,
+ "mean": 0,
+ "max": 0,
+ "n": 0
},
"liveSamples": 10,
"liveSources": 1,
@@ -474,13 +474,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 3441,
+ "presented": 3444,
"lag": {
"p50": 0.02,
"p95": 0.03,
- "mean": 0.02,
+ "mean": 0.01,
"max": 0.03,
- "n": 3441
+ "n": 3444
},
"stallShare": 0,
"decay": {
@@ -492,11 +492,11 @@
"reported": 0.04
},
"longTasks": {
- "p50": 79,
- "p95": 79,
- "mean": 79,
- "max": 79,
- "n": 1
+ "p50": 0,
+ "p95": 0,
+ "mean": 0,
+ "max": 0,
+ "n": 0
},
"liveSamples": 10,
"liveSources": 1,
@@ -513,7 +513,7 @@
"drops": 0,
"presented": 3444,
"lag": {
- "p50": 0.01,
+ "p50": 0.02,
"p95": 0.03,
"mean": 0.01,
"max": 0.03,
@@ -529,10 +529,10 @@
"reported": 0.04
},
"longTasks": {
- "p50": 54,
- "p95": 54,
- "mean": 54,
- "max": 54,
+ "p50": 55,
+ "p95": 55,
+ "mean": 55,
+ "max": 55,
"n": 1
},
"liveSamples": 10,
@@ -622,13 +622,13 @@
"pass": true,
"notes": [],
"drops": 0,
- "presented": 1621,
+ "presented": 1620,
"lag": {
"p50": 0.02,
"p95": 0.03,
"mean": 0.02,
"max": 0.03,
- "n": 1621
+ "n": 1620
},
"stallShare": 0,
"decay": {
@@ -691,18 +691,18 @@
"bucket": "playback",
"pass": true,
"notes": [],
- "drops": 3,
- "presented": 1095,
+ "drops": 2,
+ "presented": 1094,
"lag": {
"p50": 0.02,
"p95": 0.03,
- "mean": 0.01,
+ "mean": 0.02,
"max": 0.03,
"n": 1092
},
"stallShare": 0,
"decay": {
- "first": 0.008,
+ "first": 0.005,
"last": 0
},
"clock": {
@@ -710,17 +710,17 @@
"reported": 0.04
},
"longTasks": {
- "p50": 151,
- "p95": 151,
- "mean": 109,
- "max": 151,
+ "p50": 120,
+ "p95": 120,
+ "mean": 94,
+ "max": 120,
"n": 2
},
"liveSamples": 10,
"liveSources": 1,
"keptSources": 1,
"warmMb": 0,
- "memMb": 209,
+ "memMb": 218,
"machine": "desktop"
}
]
diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.ts
index fa8642e3..87642403 100644
--- a/site/packages/effects-kit/src/render.ts
+++ b/site/packages/effects-kit/src/render.ts
@@ -385,9 +385,24 @@ export function textRoom(x: number, frameWidth: number): number {
return Math.min(TEXT_SAFE_WIDTH, anchored) * frameWidth;
}
-/** Explicit boxes wrap at their width; auto-width text stays within the frame. */
-export function textWrapRoom(o: Pick<TextOverlay, "x" | "wrapWidth">, frameWidth: number): number {
- return o.wrapWidth && Number.isFinite(o.wrapWidth) ? Math.max(0.01, o.wrapWidth) * frameWidth : textRoom(o.x, frameWidth);
+/** Explicit boxes wrap at their width; auto-width text stays within the frame.
+ * A line turned by its rotation runs across the frame at that angle, so its
+ * room is the run along it from the anchor: a column word at -90 has the
+ * frame's height, measured from its y. Without the frame's height, or upright,
+ * the room is across. */
+export function textWrapRoom(
+ o: Pick<TextOverlay, "x" | "wrapWidth"> & { y?: number; rotation?: number },
+ frameWidth: number,
+ frameHeight?: number
+): number {
+ if (o.wrapWidth && Number.isFinite(o.wrapWidth)) return Math.max(0.01, o.wrapWidth) * frameWidth;
+ const across = textRoom(o.x, frameWidth);
+ const a = ((o.rotation ?? 0) * Math.PI) / 180;
+ if (!frameHeight || !a || !Number.isFinite(a)) return across;
+ const cos = Math.abs(Math.cos(a));
+ const sin = Math.abs(Math.sin(a));
+ const down = textRoom(o.y ?? 0.5, frameHeight);
+ return Math.min(cos > 1e-6 ? across / cos : Infinity, sin > 1e-6 ? down / sin : Infinity);
}
/**
@@ -525,7 +540,7 @@ async function settledFace(o: TextOverlay, frame: PaintFrame, env: RenderEnv): P
const own = o.cycleOwn;
if (!own) return o;
const cssOf = (type: TextType) => textCssFont({ ...o, ...type }, type.size * frame.scale, env);
- const room = textWrapRoom(o, frame.width);
+ const room = textWrapRoom(o, frame.width, frame.height);
const key = `${room}|${frame.scale}|${cssOf(own)}|${own.letterSpacing ?? 0}|${cssOf(o)}|${o.letterSpacing ?? 0}|${o.text}`;
let hit = settledFaces.get(key);
if (!hit) {
@@ -568,7 +583,7 @@ async function paintText(
// Words stay in the picture: a line wider than the room its anchor leaves
// is broken here, measured on the very context that is about to draw it.
- const lines = wrapTextToRoom(overlay.text, textWrapRoom(overlay, width), (line) =>
+ const lines = wrapTextToRoom(overlay.text, textWrapRoom(overlay, width, frame.height), (line) =>
ctx.measureText(line).width
).split("\n");
const lineH = fpx * (overlay.lineHeight ?? LINE_HEIGHT);
@@ -1335,7 +1350,7 @@ async function lastGlyphBox(
ctx.font = cssFont;
if ("letterSpacing" in ctx) ctx.letterSpacing = `${(o.letterSpacing ?? 0) * fpx}px`;
const measure = (line: string) => ctx.measureText(line).width;
- const lines = wrapTextToRoom(o.text, textWrapRoom(o, frame.width), measure).split("\n");
+ const lines = wrapTextToRoom(o.text, textWrapRoom(o, frame.width, frame.height), measure).split("\n");
const widths = lines.map(measure);
const maxW = o.wrapWidth ? textWrapRoom(o, frame.width) : Math.max(...widths, 1);
const lineH = fpx * (o.lineHeight ?? LINE_HEIGHT);
@@ -1525,7 +1540,7 @@ export async function measureElementBounds(
const ctx = scratch.getContext("2d")!;
ctx.font = cssFont;
if ("letterSpacing" in ctx) ctx.letterSpacing = `${(o.letterSpacing ?? 0) * fpx}px`;
- const lines = wrapTextToRoom(o.text, textWrapRoom(o, frame.width), (line) =>
+ const lines = wrapTextToRoom(o.text, textWrapRoom(o, frame.width, frame.height), (line) =>
ctx.measureText(line).width
).split("\n");
const maxW = o.wrapWidth ? textWrapRoom(o, frame.width) : Math.max(...lines.map((l) => ctx.measureText(l).width), 1);
diff --git a/site/packages/effects-kit/src/textFit.test.ts b/site/packages/effects-kit/src/textFit.test.ts
index cda7ad58..b0f4297c 100644
--- a/site/packages/effects-kit/src/textFit.test.ts
+++ b/site/packages/effects-kit/src/textFit.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
-import { TEXT_SAFE_WIDTH, textRoom, wrapTextToRoom } from "./render";
+import { TEXT_SAFE_WIDTH, textRoom, textWrapRoom, wrapTextToRoom } from "./render";
// A stand-in face: every glyph ten units wide, spaces included. Enough to pin
// down where the breaks land without a canvas.
@@ -27,6 +27,29 @@ describe("textRoom", () => {
});
});
+describe("textWrapRoom", () => {
+ test("a line turned to stand upright runs the frame's height, not its width", () => {
+ // A column word at the left edge reading bottom to top: its line runs
+ // down the frame, so the room is the height it can take from its anchor.
+ const column = { x: 0.095, y: 0.5, rotation: -90 };
+ expect(textWrapRoom(column, 1920, 1080)).toBeCloseTo(TEXT_SAFE_WIDTH * 1080);
+ expect(textWrapRoom({ ...column, rotation: 90 }, 1920, 1080)).toBeCloseTo(TEXT_SAFE_WIDTH * 1080);
+ // Upright, the same anchor has only the run to the near edge.
+ expect(textWrapRoom({ ...column, rotation: 0 }, 1920, 1080)).toBeCloseTo(0.19 * 1920);
+ });
+
+ test("a tilted line takes the shorter of its runs to the sides and to the top or bottom", () => {
+ const tilted = { x: 0.5, y: 0.5, rotation: 45 };
+ const across = (TEXT_SAFE_WIDTH * 1920) / Math.SQRT1_2;
+ const down = (TEXT_SAFE_WIDTH * 1080) / Math.SQRT1_2;
+ expect(textWrapRoom(tilted, 1920, 1080)).toBeCloseTo(Math.min(across, down));
+ });
+
+ test("a box with its own width wraps at it whatever its angle", () => {
+ expect(textWrapRoom({ x: 0.1, y: 0.5, rotation: -90, wrapWidth: 0.3 }, 1920, 1080)).toBeCloseTo(576);
+ });
+});
+
describe("wrapTextToRoom", () => {
test("a line that fits stays on one line", () => {
expect(wrapTextToRoom("before going to", 400, measure)).toBe("before going to");
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index e8b47c70..27ecb816 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -367,7 +367,7 @@ export function OverlayLayer({
// A font cycle sets the title in another face at this moment.
// A selected title holds its own face while paused, since that is
// the face its grips and the inspector edit.
- overlay={inRange && (playing || !selected) ? cycleFaceAt(o, t - o.start, frame.w) : o}
+ overlay={inRange && (playing || !selected) ? cycleFaceAt(o, t - o.start, frame.w, frame.h) : o}
// The skimmer paints the bare frame: the item still renders, its
// selection chrome (outline, resize handle) does not.
selected={selected && !scrubbing}
@@ -420,7 +420,7 @@ export function OverlayLayer({
return (
<OverlayItem
key={o.id}
- overlay={cycleFaceAt(o, t - o.start, frame.w)}
+ overlay={cycleFaceAt(o, t - o.start, frame.w, frame.h)}
selected={false}
armed={false}
grouped={false}
@@ -723,7 +723,7 @@ function OverlayItem({
// line is wider than the room the frame leaves it. The export's painter
// breaks the same lines in the same places; editing shows the raw text, so
// the box holds what the author typed.
- const drawnText = !isTextOverlay(o) ? "" : editing ? o.text : fitTextToFrame(o, frame.w);
+ const drawnText = !isTextOverlay(o) ? "" : editing ? o.text : fitTextToFrame(o, frame.w, frame.h);
// Publish this element's box so a sibling drag can align to it.
useEffect(() => {
@@ -1082,7 +1082,7 @@ function OverlayItem({
const w = worldPoint(cam, seen.x + mx, seen.y + my, aspect);
return { x: w.x - self.x, y: w.y - self.y };
};
- const textSizes = new Map(members.filter(isTextOverlay).map((m) => [m.id, textBoxSize(m, frame.w)]));
+ const textSizes = new Map(members.filter(isTextOverlay).map((m) => [m.id, textBoxSize(m, frame.w, frame.h)]));
const stickerSize = (v: number) => Math.min(1.5, Math.max(0.02, v));
// A sticker's resting height as a frame fraction: the one it stores, or
// the grabbed box laid out under the source's own aspect. A side grip
@@ -1132,7 +1132,7 @@ function OverlayItem({
};
if (isTextOverlay(self)) {
const before = textSizes.get(self.id)!;
- const after = textBoxSize({ ...self, size: p.size ?? self.size, wrapWidth: p.wrapWidth ?? self.wrapWidth }, frame.w);
+ const after = textBoxSize({ ...self, size: p.size ?? self.size, wrapWidth: p.wrapWidth ?? self.wrapWidth }, frame.w, frame.h);
return { kx: after.width / before.width, ky: after.height / before.height };
}
if (self.kind === "shape")
diff --git a/site/src/cut/lib/previewSelection.ts b/site/src/cut/lib/previewSelection.ts
index 36f72ea5..ed9f9bcf 100644
--- a/site/src/cut/lib/previewSelection.ts
+++ b/site/src/cut/lib/previewSelection.ts
@@ -65,7 +65,7 @@ export function previewSelectionSnapshot(s: EditorState, t: number): Position[]
kind: "overlay", id: o.id, x: seen.x, y: seen.y, key, keys: o.kf, cam,
rotation: (key?.rotation ?? o.rotation ?? 0) + (cam?.pose.rotation ?? 0),
box: isShapeOverlay(o) || o.kind === "sticker" ? { w: o.w, h: o.h } : undefined,
- text: isTextOverlay(o) ? { size: o.size, wrapWidth: o.wrapWidth, width: (textBoxSize(o, frameOf(s.aspect).w).width - (o.plate ? 2 * PLATE_PAD_X * o.size : 0)) / frameOf(s.aspect).w } : undefined,
+ text: isTextOverlay(o) ? { size: o.size, wrapWidth: o.wrapWidth, width: (textBoxSize(o, frameOf(s.aspect).w, frameOf(s.aspect).h).width - (o.plate ? 2 * PLATE_PAD_X * o.size : 0)) / frameOf(s.aspect).w } : undefined,
});
}
for (const c of s.clips) {
diff --git a/site/src/cut/lib/textFit.ts b/site/src/cut/lib/textFit.ts
index 5776996c..d891160c 100644
--- a/site/src/cut/lib/textFit.ts
+++ b/site/src/cut/lib/textFit.ts
@@ -99,10 +99,10 @@ export function textFontOf(
/** The element's text with every over-long line broken to fit the room its
* anchor leaves in the frame. Returns the text unchanged when it already fits,
* so nothing re-renders for an element that was never in trouble. */
-export function fitTextToFrame(o: TextOverlay, frameW: number): string {
+export function fitTextToFrame(o: TextOverlay, frameW: number, frameH?: number): string {
const size = o.size;
const font = textFontOf(o, size);
- const room = textWrapRoom(o, frameW);
+ const room = textWrapRoom(o, frameW, frameH);
const width = (line: string) => measureLine(line, font, size, o.letterSpacing ?? 0);
if (o.text.split("\n").every((line) => width(line) <= room)) return o.text;
return wrapTextToRoom(o.text, room, width);
@@ -111,7 +111,7 @@ export function fitTextToFrame(o: TextOverlay, frameW: number): string {
/** Each element's settled faces, one per face, frame width and font state,
* so a playing preview resolves a cycling title without re-wrapping it on
* every tick. Keyed by the element, so an edit starts afresh. */
-let settled = new WeakMap<Overlay, { face: CycleFace; frameW: number; ready: boolean; out: Overlay }[]>();
+let settled = new WeakMap<Overlay, { face: CycleFace; frameW: number; frameH?: number; ready: boolean; out: Overlay }[]>();
/** Settled faces one element keeps: its faces at a few frame widths. */
const SETTLED_PER_ELEMENT = 32;
@@ -119,7 +119,7 @@ const SETTLED_PER_ELEMENT = 32;
* the title's own lines the way the kit's painters set it (`settleCycleFace`),
* so the preview never reflows a title the export keeps on one line. One face
* at one frame width hands back the same element every tick. */
-export function cycleFaceAt<T extends Overlay>(o: T, tLocal: number, frameW: number): T {
+export function cycleFaceAt<T extends Overlay>(o: T, tLocal: number, frameW: number, frameH?: number): T {
const c = overlayFontCycle(o);
const face = c ? faceShowingAt(c, tLocal) : null;
if (!face) return o;
@@ -128,22 +128,22 @@ export function cycleFaceAt<T extends Overlay>(o: T, tLocal: number, frameW: num
const ready = facesReady();
const known = settled.get(o) ?? [];
for (const k of known) {
- if (k.face === face && k.frameW === frameW && k.ready === ready) return k.out as T;
+ if (k.face === face && k.frameW === frameW && k.frameH === frameH && k.ready === ready) return k.out as T;
}
const el = withFontCycle(o, tLocal);
- const out = settleCycleFace(el, textWrapRoom(el as TextOverlay, frameW), (type, line) =>
+ const out = settleCycleFace(el, textWrapRoom(el as TextOverlay, frameW, frameH), (type, line) =>
measureLine(line, textFontOf(type, type.size), type.size, type.letterSpacing ?? 0)
);
if (known.length >= SETTLED_PER_ELEMENT) known.shift();
- known.push({ face, frameW, ready, out });
+ known.push({ face, frameW, frameH, ready, out });
settled.set(o, known);
return out;
}
/** Resting box in design pixels, measured once for resize gestures. */
-export function textBoxSize(o: TextOverlay, frameW: number): { width: number; height: number } {
- const lines = fitTextToFrame(o, frameW).split("\n");
+export function textBoxSize(o: TextOverlay, frameW: number, frameH?: number): { width: number; height: number } {
+ const lines = fitTextToFrame(o, frameW, frameH).split("\n");
const width = o.wrapWidth ? textWrapRoom(o, frameW) : Math.max(1, ...lines.map((line) => measureLine(line, textFontOf(o, o.size), o.size, o.letterSpacing ?? 0)));
return { width: width + (o.plate ? 2 * PLATE_PAD_X * o.size : 0), height: (lines.length * (o.lineHeight ?? LINE_HEIGHT) + (o.plate ? 2 * PLATE_PAD_Y : 0)) * o.size };
}