Skip to content

Open source CapCut alternative.

TypeScript

fix(site): paint typing once, keep presses in time [rebuild]

David Le4781ecc59f76
diff --git a/site/packages/effects-kit/src/anim.ts b/site/packages/effects-kit/src/anim.ts
index 377bd67c..38804c81 100644
--- a/site/packages/effects-kit/src/anim.ts
+++ b/site/packages/effects-kit/src/anim.ts
@@ -190,7 +190,7 @@ export const loopMotion = (slot: OverlayLoop | undefined): MotionPreset | undefi
export const moveMotion = (slot: OverlayMove | undefined): MotionPreset | undefined =>
slot ? holdPreset(slot.style) : undefined;
-export const hitMotion = (slot: OverlayHit | undefined): MotionPreset | undefined =>
+const hitMotion = (slot: OverlayHit | undefined): MotionPreset | undefined =>
slot ? hitPreset(slot.style) : undefined;
// ── the registries, read from the catalog ─────────────────────────────────
@@ -537,9 +537,8 @@ export function evalOverlayAnim(
* the preset's pose folded onto what the other slots left, as one piece. */
function applyHit(state: OverlayAnimState, slot: OverlayHit, tLocal: number): void {
const preset = hitMotion(slot);
- const secs = slot.seconds > 0 ? slot.seconds : OVERLAY_HIT_DEFAULT_SECONDS;
- const q = (tLocal - slot.at) / secs;
- if (!preset || q <= 0 || q >= 1) return;
+ const q = (tLocal - slot.at) / slot.seconds;
+ if (!preset || !(q > 0 && q < 1)) return;
const pose = evalWhole(preset, q, false);
state.dx += pose.dx;
state.dy += pose.dy;
@@ -558,9 +557,8 @@ export function hitWindow(
): { start: number; end: number } | null {
const slot = anim?.hit;
if (!slot || !hitMotion(slot)) return null;
- const secs = slot.seconds > 0 ? slot.seconds : OVERLAY_HIT_DEFAULT_SECONDS;
const start = Math.max(0, Math.min(dur, slot.at));
- const end = Math.max(start, Math.min(dur, slot.at + secs));
+ const end = Math.max(start, Math.min(dur, slot.at + slot.seconds));
return end - start > 1e-3 ? { start, end } : null;
}
@@ -572,6 +570,12 @@ export function hitScale(anim: OverlayAnim | undefined): number {
return s;
}
+/** The typing bar an entrance keeps when it changes to `style`: the bar stays
+ * while the new entrance still types. */
+export function keepCaret(edge: OverlayEdge | undefined, style: string): OverlayCaret | undefined {
+ return edgePreset(style)?.animate.typed ? edge?.caret : undefined;
+}
+
/** The element's typing bar, when its entrance types and carries one. */
export function typeCaret(anim: OverlayAnim | undefined): OverlayCaret | undefined {
const edge = anim?.in;
@@ -608,14 +612,19 @@ export function caretToggles(anim: OverlayAnim | undefined, dur: number): number
const caret = typeCaret(anim);
if (!caret) return [];
const typedBy = Math.min(anim!.in!.seconds, dur);
+
+ // A bar that holds after typing turns off once, when it hides.
+ if (!caret.blink) return caret.hide && typedBy > 0 && typedBy < dur ? [typedBy] : [];
+
+ // A blink flips every CARET_BLINK_SECONDS once typing stops, lit first. A
+ // counted blink ends dark, then lights again for good unless it hides:
+ // two blinks flip at 1, 2, 3 (hide) or 1, 2, 3, 4 (stay) half-cycles.
+ const flips = caret.blinks !== undefined && caret.blinks > 0 ? caret.blinks * 2 - (caret.hide ? 1 : 0) : Infinity;
const out: number[] = [];
- const half = CARET_BLINK_SECONDS / 2;
- // Every flip lands on a half-blink boundary after typing stops; each one
- // is kept when the bar reads differently on its two sides.
- for (let k = 0; typedBy + k * CARET_BLINK_SECONDS < dur; k++) {
+ for (let k = 1; k <= flips; k++) {
const t = typedBy + k * CARET_BLINK_SECONDS;
- if (t <= 0) continue;
- if (caretOn(anim, t - half, dur) !== caretOn(anim, t + half, dur)) out.push(t);
+ if (t >= dur) break;
+ out.push(t);
}
return out;
}
diff --git a/site/packages/effects-kit/src/caretHit.test.ts b/site/packages/effects-kit/src/caretHit.test.ts
index a14a3661..e5e2ea32 100644
--- a/site/packages/effects-kit/src/caretHit.test.ts
+++ b/site/packages/effects-kit/src/caretHit.test.ts
@@ -12,7 +12,9 @@ import {
} from "./anim";
import { evalOverlayFrame } from "./keys";
import { MOTION, presetsFor } from "./motion/catalog";
-import { darkenCanvas, paintElement, planAnimatedLayers, renderOverlayFrames, type RenderEnv } from "./render";
+import { WORD_EFFECT_IDS } from "./words/catalog";
+import { darkenCanvas, ElementFx, elementLook } from "./elementFx";
+import { paintElement, planAnimatedLayers, renderOverlayFrames, type RenderEnv } from "./render";
import type { ShapeOverlay, TextOverlay } from "./types";
const B = CARET_BLINK_SECONDS;
@@ -153,6 +155,22 @@ describe("the press", () => {
expect(d[2 * 4 + 3]).toBe(0);
});
+ test("a darkening hit dims the element and leaves the frame under it", () => {
+ const c = new Canvas(4, 4);
+ const ctx = c.getContext("2d") as unknown as CanvasRenderingContext2D;
+ ctx.fillStyle = "rgb(0, 0, 200)";
+ ctx.fillRect(0, 0, 4, 4);
+ const fx = new ElementFx((w, h) => new Canvas(w, h) as unknown as HTMLCanvasElement);
+ const into = fx.begin(ctx, 4, 4, elementLook({ brightness: 0.5 }, 1));
+ expect(into).not.toBe(ctx);
+ into.fillStyle = "rgb(200, 200, 200)";
+ into.fillRect(0, 0, 2, 4);
+ fx.end(ctx);
+ const d = ctx.getImageData(0, 0, 4, 4).data;
+ expect(d[0]).toBeCloseTo(100, -1);
+ expect(d[2 * 4 + 2]).toBe(200);
+ });
+
test("the frame sampler plays the press frame by frame and holds the rest still", async () => {
const o: ShapeOverlay = { id: "s", kind: "shape", shape: "rect", start: 0, end: 4, x: 0.5, y: 0.5, w: 0.2, h: 0.1, fill: "#ffffff", anim: press(true) };
const set = await renderOverlayFrames(o, 540, 960, 30, env);
@@ -163,6 +181,27 @@ describe("the press", () => {
expect(set.entries[0].image).toBe(set.entries[set.entries.length - 1].image);
});
+ test("a press over a loop costs one cycle and the press, however long the element runs", async () => {
+ const o: ShapeOverlay = { id: "s", kind: "shape", shape: "rect", start: 0, end: 60, x: 0.5, y: 0.5, w: 0.2, h: 0.1, fill: "#ffffff", anim: { ...press(false), loop: { style: "pulse", speed: 1 } } };
+ const set = await renderOverlayFrames(o, 270, 480, 30, env);
+ expect(set.entries.reduce((a, e) => a + e.duration, 0)).toBeCloseTo(60, 6);
+ expect(set.images.length).toBeLessThan(200);
+ });
+
+ test("a press over a word effect costs the word pictures and the press", async () => {
+ const o: TextOverlay = { id: "t", text: "one two three four", start: 0, end: 60, x: 0.5, y: 0.5, size: 60, font: "sf", weight: 700, color: "#fff", plate: false, shadow: false, anim: { ...press(false), words: { style: WORD_EFFECT_IDS[0] } } };
+ const set = await renderOverlayFrames(o, 270, 480, 30, env);
+ expect(set.entries.reduce((a, e) => a + e.duration, 0)).toBeCloseTo(60, 6);
+ expect(set.images.length).toBeLessThan(200);
+ });
+
+ test("a blink over a loop keeps the bar's turns", async () => {
+ const o: TextOverlay = { id: "t", text: "LINK", start: 0, end: 20, x: 0.5, y: 0.5, size: 60, font: "sf", weight: 700, color: "#fff", plate: false, shadow: false, anim: { ...typed({ blink: true }), loop: { style: "pulse", speed: 1 } } };
+ const set = await renderOverlayFrames(o, 270, 480, 30, env);
+ expect(set.entries.reduce((a, e) => a + e.duration, 0)).toBeCloseTo(20, 6);
+ expect(set.images.length).toBeLessThan(200);
+ });
+
test("the frame sampler draws a blink as two pictures", async () => {
const o: TextOverlay = { id: "t", text: "LINK", start: 0, end: 4, x: 0.5, y: 0.5, size: 60, font: "sf", weight: 700, color: "#fff", plate: false, shadow: false, anim: typed({ blink: true }) };
const set = await renderOverlayFrames(o, 540, 960, 30, env);
diff --git a/site/packages/effects-kit/src/elementFx.ts b/site/packages/effects-kit/src/elementFx.ts
index 6fcab7ba..2365d503 100644
--- a/site/packages/effects-kit/src/elementFx.ts
+++ b/site/packages/effects-kit/src/elementFx.ts
@@ -1,5 +1,6 @@
/**
- * Drawing an element soft: its blur and its motion streak.
+ * Drawing an element soft: its blur and its motion streak, and the darkening
+ * a hit gives it.
*
* Both act on the element's posed picture as it lands on screen, so every
* renderer does the same three steps: pose the element into a scratch surface
@@ -29,28 +30,48 @@ export interface ElementLook {
streakY: number;
/** How many copies the streak is drawn from; 0 = no streak. */
taps: number;
+ /** What a hit leaves of the element's color, 0..1; 1 = untouched. */
+ brightness: number;
}
/** A blur below this many output px is invisible and skipped. */
const BLUR_FLOOR = 0.1;
/** The look an evaluated frame asks for at `scale` output px per design px,
- * or null when the element draws sharp. */
+ * or null when the element draws sharp and at its own color. */
export function elementLook(
- ev: { blur?: number; streak?: { x: number; y: number } },
+ ev: { blur?: number; streak?: { x: number; y: number }; brightness?: number },
scale: number
): ElementLook | null {
const blur = (ev.blur ?? 0) * scale;
const taps = ev.streak ? streakTaps(Math.hypot(ev.streak.x, ev.streak.y)) : 0;
- if (blur < BLUR_FLOOR && taps === 0) return null;
+ const brightness = ev.brightness ?? 1;
+ if (blur < BLUR_FLOOR && taps === 0 && !(brightness < 1)) return null;
return {
blur: blur < BLUR_FLOOR ? 0 : blur,
streakX: taps ? ev.streak!.x * scale : 0,
streakY: taps ? ev.streak!.y * scale : 0,
taps,
+ brightness,
};
}
+/**
+ * Multiply every color already on a surface by `brightness` (0..1), leaving
+ * alpha alone: black laid source-atop at 1 - brightness. The surface has to
+ * hold the element by itself, or whatever sits under it darkens too.
+ */
+export function darkenCanvas(ctx: Ctx, width: number, height: number, brightness: number): void {
+ if (!(brightness < 1)) return;
+ ctx.save();
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ ctx.globalCompositeOperation = "source-atop";
+ ctx.globalAlpha = Math.min(1, 1 - Math.max(0, brightness));
+ ctx.fillStyle = "#000000";
+ ctx.fillRect(0, 0, width, height);
+ ctx.restore();
+}
+
/** A context back to plain drawing: identity transform, full alpha,
* source-over, no filter. */
function reset(c: Ctx): void {
@@ -120,7 +141,7 @@ export class ElementFx {
return c;
}
- /** Lay the painted element onto `ctx` with its streak and blur. */
+ /** Lay the painted element onto `ctx` with its streak, darkening and blur. */
end(ctx: Ctx): void {
const look = this.look;
this.look = null;
@@ -134,6 +155,8 @@ export class ElementFx {
drawStreak(s, src, 0, 0, look);
src = this.smear;
}
+ // The scratch holds the element alone, so a hit darkens its pixels only.
+ darkenCanvas(src.getContext("2d") as Ctx, src.width, src.height, look.brightness);
ctx.save();
reset(ctx);
if (look.blur > 0 && "filter" in ctx) ctx.filter = `blur(${look.blur.toFixed(2)}px)`;
diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.ts
index 557df5af..05bf718d 100644
--- a/site/packages/effects-kit/src/render.ts
+++ b/site/packages/effects-kit/src/render.ts
@@ -157,27 +157,6 @@ export interface PaintPhase {
pose?: Pick<OverlayFrameState, "x" | "y" | "dx" | "dy" | "scale" | "rotation">;
}
-/**
- * Multiply every color already on a surface by `brightness` (0..1), leaving
- * alpha alone: black laid source-atop at 1 - brightness. The surface has to
- * hold the element by itself, or whatever sits under it darkens too.
- */
-export function darkenCanvas(
- ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
- width: number,
- height: number,
- brightness: number
-): void {
- if (!(brightness < 1)) return;
- ctx.save();
- ctx.setTransform(1, 0, 0, 1, 0, 0);
- ctx.globalCompositeOperation = "source-atop";
- ctx.globalAlpha = Math.min(1, 1 - Math.max(0, brightness));
- ctx.fillStyle = "#000000";
- ctx.fillRect(0, 0, width, height);
- ctx.restore();
-}
-
/**
* A shape's pixel geometry in the output frame — the one place the fractions
* become pixels, shared by the canvas painter and testable on its own.
@@ -560,16 +539,19 @@ async function paintText(
// as the preview's bar.
const caretColor = phase?.caret ? (typeCaret(overlay.anim)?.color ?? overlay.color) : null;
const alphaOf = ctx.globalAlpha;
+ // How far the type's top sits over a baseline, in the current font.
+ const ascentAt = (baseline: CanvasTextBaseline) => {
+ ctx.textBaseline = baseline;
+ return ctx.measureText("Hg").actualBoundingBoxAscent;
+ };
const drawCaret = (right: number) => {
if (!caretColor) return;
const y = cy - totalH / 2 + lineH * (lines.length - 0.5);
ctx.save();
ctx.font = cssFont;
- ctx.textBaseline = "alphabetic";
- const base = ctx.measureText("Hg").actualBoundingBoxAscent;
+ const base = ascentAt("alphabetic");
const xh = ctx.measureText("x").actualBoundingBoxAscent;
- ctx.textBaseline = "middle";
- const mid = ctx.measureText("Hg").actualBoundingBoxAscent;
+ const mid = ascentAt("middle");
const off =
Number.isFinite(base) && Number.isFinite(mid) && Number.isFinite(xh) ? base - mid - xh / 2 : 0;
ctx.shadowColor = "transparent";
@@ -645,10 +627,6 @@ async function paintText(
// gives the gap exactly, in whatever engine is drawing: the ascent is
// reported from the alignment point, so the difference between the two is the
// distance between them.
- const ascentAt = (baseline: CanvasTextBaseline) => {
- ctx.textBaseline = baseline;
- return ctx.measureText("Hg").actualBoundingBoxAscent;
- };
const alphabetic = ascentAt("alphabetic");
const middle = ascentAt("middle");
const drop =
@@ -1420,6 +1398,9 @@ export interface OverlayFrameSet {
entries: { image: number; duration: number }[];
}
+/** How a sampled picture shows the typing bar. */
+type BarLook = "lit" | "dark";
+
/**
* Sample an animated element into frames at `fps`: the In and Out windows
* frame by frame, the middle as one still (or one loop cycle, repeated by
@@ -1514,10 +1495,13 @@ export async function renderOverlayFrames(
// Blur and motion blur paint the posed element into a scratch first.
const fx = new ElementFx((w, h) => newCanvas(env, w, h));
- const drawAt = async (tLocal: number): Promise<Blob> => {
+ // `bar` paints the typing bar lit or dark whatever `tLocal` says, so a
+ // cycle drawn once serves every blink it repeats under.
+ const drawAt = async (tLocal: number, bar?: BarLook): Promise<Blob> => {
target.setTransform(1, 0, 0, 1, 0, 0);
target.clearRect(0, 0, rw, rh);
const ev = evalOverlayFrame(overlay, tLocal, width / height);
+ if (bar) ev.caret = bar === "lit";
if (ev.opacity <= 0.001) return pngBlob(canvas, env); // fully transparent frame
const ctx = fx.begin(target, rw, rh, elementLook(ev, scale)) as CanvasRenderingContext2D;
ctx.globalAlpha = ev.opacity;
@@ -1545,9 +1529,6 @@ export async function renderOverlayFrames(
maskTransform
);
}
- // The region holds this element alone, so a hit's darkening lands on its
- // own pixels.
- if (ev.brightness !== undefined) darkenCanvas(ctx, rw, rh, ev.brightness);
fx.end(target);
return pngBlob(canvas, env);
};
@@ -1561,22 +1542,12 @@ export async function renderOverlayFrames(
const step = 1 / fps;
const period = loopPeriod(anim) ?? lottieDur;
- // A hit and a blinking bar change the middle at their own moments.
const hit = hitWindow(anim, dur);
const toggles = (overlay.kind ?? "text") === "text" ? caretToggles(anim, dur) : [];
- const timed = !!hit || toggles.length > 0;
// A keyframed pose, a camera, a move, or a keyframed mask changes on its
// own schedule across the whole element, so there is no still middle and no cycle to
- // repeat: sample the element frame by frame. So does a hit or a blink over
- // a middle that already cycles.
- if (
- keyed ||
- filmed ||
- anim?.move ||
- isMaskAnimated(overlay.mask) ||
- (accent && anim?.loop) ||
- (timed && (!!accent || !!period))
- ) {
+ // repeat: sample the element frame by frame.
+ if (keyed || filmed || anim?.move || isMaskAnimated(overlay.mask) || (accent && anim?.loop)) {
const n = Math.max(1, Math.round(dur * fps));
for (let i = 0; i < n; i++) {
await push(i * step, i === n - 1 ? dur - (n - 1) * step : step);
@@ -1594,63 +1565,86 @@ export async function renderOverlayFrames(
await push(i * step, i === n - 1 ? inS - (n - 1) * step : step);
}
}
- const middle = dur - inS - outS;
- if (middle > 1e-3) {
- if (timed && !accent && !period) {
- // The middle holds still between the hit and the bar's turns: one
- // picture per look (bar lit, bar dark), reused across every still
- // piece, and the hit frame by frame.
- const a0 = inS;
- const b0 = dur - outS;
- const cuts = [a0, b0, ...toggles];
- if (hit) cuts.push(hit.start, hit.end);
- const marks = [...new Set(cuts.filter((t) => t >= a0 && t <= b0))].sort((x, y) => x - y);
- const stills = new Map<boolean, number>();
- for (let i = 0; i + 1 < marks.length; i++) {
- const a = marks[i];
- const b = marks[i + 1];
- if (b - a < 1e-4) continue;
- if (hit && a >= hit.start - 1e-6 && b <= hit.end + 1e-6) {
- const n = Math.max(1, Math.round((b - a) * fps));
- for (let j = 0; j < n; j++) await push(a + j * step, j === n - 1 ? b - a - (n - 1) * step : step);
- continue;
- }
- const lit = caretOn(anim, (a + b) / 2, dur) === true;
- let image = stills.get(lit);
- if (image === undefined) {
- images.push(await drawAt((a + b) / 2));
- image = images.length - 1;
- stills.set(lit, image);
- }
- entries.push({ image, duration: b - a });
- }
- } else if (accent) {
- // The picture changes as the effect walks the line, and nowhere else:
- // one frame per span the engine says holds still covers the middle
- // however long it runs. The last one absorbs the rounding, so the
- // pieces sum to the middle exactly.
+ const a0 = inS;
+ const b0 = dur - outS;
+
+ // Pictures the middle reuses, by what they show: a word span or a cycle
+ // frame with the bar lit or dark.
+ const kept = new Map<string, number>();
+ const keep = async (key: string, t: number, bar?: BarLook): Promise<number> => {
+ let image = kept.get(key);
+ if (image === undefined) {
+ images.push(await drawAt(t, bar));
+ image = images.length - 1;
+ kept.set(key, image);
+ }
+ return image;
+ };
+
+ // The moment a cycle frame is drawn at, moved by whole periods off the
+ // hit, so the cycle repeated around the press never carries it.
+ const calm = (t: number): number => {
+ if (!hit || !period) return t;
+ for (let u = t; u < b0; u += period) if (u < hit.start || u >= hit.end) return u;
+ return t;
+ };
+
+ // One stretch of the middle between the hit and the bar's turns, drawn as
+ // the middle draws when nothing interrupts it: one frame per word span,
+ // one cycle repeated by reference, or one still.
+ const fill = async (a: number, b: number, bar?: BarLook) => {
+ if (accent) {
const spans = wordSampleWindows((overlay as TextOverlay).text, accent, dur, fps)
- .map((w) => ({ a: Math.max(inS, w.start), b: Math.min(dur - outS, w.end) }))
+ .map((w, i) => ({ i, a: Math.max(a, w.start), b: Math.min(b, w.end) }))
.filter((w) => w.b - w.a > 1e-3);
- if (spans.length > 0) spans[spans.length - 1].b = dur - outS;
- for (const w of spans) await push((w.a + w.b) / 2, w.b - w.a);
- if (spans.length === 0) await push(inS + middle / 2, middle);
- } else if (period) {
- // One cycle of pictures, repeated by reference to cover the middle.
+ if (spans.length === 0) {
+ entries.push({ image: await keep(`still:${bar}`, (a + b) / 2, bar), duration: b - a });
+ return;
+ }
+ // The last one absorbs the rounding, so the pieces sum to the stretch.
+ spans[0].a = a;
+ spans[spans.length - 1].b = b;
+ for (const w of spans) {
+ entries.push({ image: await keep(`word${w.i}:${bar}`, (w.a + w.b) / 2, bar), duration: w.b - w.a });
+ }
+ return;
+ }
+ if (period) {
+ // One cycle of pictures, repeated by reference, on the cycle's own
+ // clock from the middle's start.
const n = Math.max(2, Math.min(Math.round(period * fps), 180));
const cycleStep = period / n;
- const cycleStart = images.length;
- for (let j = 0; j < n; j++) images.push(await drawAt(inS + j * cycleStep));
- let remaining = middle;
- let j = 0;
- while (remaining > 1e-4) {
- const d = Math.min(cycleStep, remaining);
- entries.push({ image: cycleStart + (j % n), duration: d });
- remaining -= d;
- j++;
+ let t = a;
+ while (b - t > 1e-4) {
+ const j = Math.floor((t - a0) / cycleStep + 1e-6);
+ const next = Math.min(b, a0 + (j + 1) * cycleStep);
+ const image = await keep(`cycle${j % n}:${bar}`, calm(a0 + (j % n) * cycleStep), bar);
+ entries.push({ image, duration: next - t });
+ t = next;
}
- } else {
- await push(inS + middle / 2, middle);
+ return;
+ }
+ entries.push({ image: await keep(`still:${bar}`, (a + b) / 2, bar), duration: b - a });
+ };
+
+ // A hit and a blinking bar change the middle at their own moments: it is
+ // cut there, the hit plays frame by frame, and every stretch between fills
+ // as an uninterrupted middle would.
+ if (b0 - a0 > 1e-3) {
+ const cuts = [a0, b0, ...toggles];
+ if (hit) cuts.push(hit.start, hit.end);
+ const marks = [...new Set(cuts.filter((t) => t >= a0 && t <= b0))].sort((x, y) => x - y);
+ for (let i = 0; i + 1 < marks.length; i++) {
+ const a = marks[i];
+ const b = marks[i + 1];
+ if (b - a < 1e-4) continue;
+ if (hit && a >= hit.start - 1e-6 && b <= hit.end + 1e-6) {
+ const n = Math.max(1, Math.round((b - a) * fps));
+ for (let j = 0; j < n; j++) await push(a + j * step, j === n - 1 ? b - a - (n - 1) * step : step);
+ continue;
+ }
+ const on = caretOn(anim, (a + b) / 2, dur);
+ await fill(a, b, on === undefined ? undefined : on ? "lit" : "dark");
}
}
if (outS > 1e-3) {
diff --git a/site/src/cut/components/Inspector.tsx b/site/src/cut/components/Inspector.tsx
index b8847db8..9410ced6 100644
--- a/site/src/cut/components/Inspector.tsx
+++ b/site/src/cut/components/Inspector.tsx
@@ -45,7 +45,7 @@ import {
OVERLAY_ANIM_MIN_SECONDS,
CARET_BLINKS_MAX,
edgeMotion,
- edgePreset,
+ keepCaret,
OVERLAY_HIT_DEFAULT_SECONDS,
OVERLAY_HIT_MAX_SECONDS,
OVERLAY_HIT_MIN_SECONDS,
@@ -2746,14 +2746,6 @@ function WordSettings({ overlay: o, peers }: { overlay: Overlay; peers?: readonl
);
}
-/** A caret with one setting taken out, so an absent field reads as its
- * default. */
-function caretWithout(caret: OverlayCaret, key: "blinks"): OverlayCaret {
- const next = { ...caret };
- delete next[key];
- return next;
-}
-
/** The typing bar of a typewriter entrance: on or off, whether it blinks once
* typing stops, how many times, whether it goes after, and its color. */
function CaretSettings({ overlay: o, peers }: { overlay: Overlay; peers?: readonly Overlay[] }) {
@@ -2782,7 +2774,10 @@ function CaretSettings({ overlay: o, peers }: { overlay: Overlay; peers?: readon
};
const setBlinks = (v: number) => {
if (!caret) return;
- write(v > 0 ? { ...caret, blinks: Math.round(v) } : caretWithout(caret, "blinks"));
+ // No blinks count reads as blinking for good.
+ const { blinks: _drop, ...rest } = caret;
+ void _drop;
+ write(v > 0 ? { ...caret, blinks: Math.round(v) } : rest);
};
return (
<>
@@ -3129,6 +3124,7 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?
writeOverlayAnim(o, anim, { [slot]: undefined }, peers);
return;
}
+ const caret = slot === "in" ? keepCaret(anim.in, style) : undefined;
const patch: Partial<OverlayAnim> =
slot === "loop"
? { loop: { style: style as OverlayLoopStyle, speed: anim.loop?.speed ?? 1 } }
@@ -3136,10 +3132,7 @@ export function AnimationPanel({ overlay: o, peers }: { overlay: Overlay; peers?
[slot]: {
style: style as OverlayAnimStyle,
seconds: seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS,
- // A caret stays with an entrance that still types.
- ...(slot === "in" && anim.in?.caret && edgePreset(style)?.animate.typed
- ? { caret: anim.in.caret }
- : {}),
+ ...(caret ? { caret } : {}),
},
};
useEditor.getState().pushHistory();
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index e6112bef..a5935ed4 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -78,7 +78,7 @@ import {
CARET_BLINKS_MAX,
displayWords,
edgeMotion,
- edgePreset,
+ keepCaret,
OVERLAY_HIT_DEFAULT_SECONDS,
OVERLAY_HIT_MAX_SECONDS,
OVERLAY_HIT_MIN_SECONDS,
@@ -620,8 +620,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {
throw new ToolError(`Unknown ${slot} style: ${raw}`);
if ((TEXT_ONLY_ANIM_STYLE_IDS as string[]).includes(raw) && (o.kind ?? "text") !== "text")
throw new ToolError(`${raw} animates titles only.`);
- // A caret stays with an entrance that still types.
- const caret = edgePreset(raw)?.animate.typed ? anim[slot]?.caret : undefined;
+ const caret = keepCaret(anim[slot], raw);
anim[slot] = {
style: raw as OverlayAnimStyle,
seconds: secs ?? anim[slot]?.seconds ?? OVERLAY_ANIM_DEFAULT_SECONDS,
@@ -6199,7 +6198,7 @@ function applyCaretInput(
if (input.caret_hide) caret.hide = true;
else delete caret.hide;
}
- if (typeof input.caret_color === "string") caret.color = input.caret_color;
+ if (typeof input.caret_color === "string") caret.color = hexColor(input.caret_color, "caret_color");
anim.in = { ...edge, caret };
}
diff --git a/site/src/cut/lib/behindPass.ts b/site/src/cut/lib/behindPass.ts
index 06b394e8..e44d0ad3 100644
--- a/site/src/cut/lib/behindPass.ts
+++ b/site/src/cut/lib/behindPass.ts
@@ -11,7 +11,6 @@
import {
applyWordDraw,
- darkenCanvas,
diveView,
divesAt,
evalOverlayFrame,
@@ -79,27 +78,29 @@ function rasterSliceAt(o: Overlay, tLocal: number): number {
* Which picture an element needs at one moment: its word slice, how many
* characters a typewriter has typed (-1 = the whole text), and whether the
* typing bar is lit — the same reading the front path's painters take from
- * the evaluator. One record, rewritten in place every time it is read.
+ * the evaluator. A whole-text picture keys at zero or above (two per word
+ * slice, bar dark and lit); a typing picture keys below zero, one per typed
+ * count.
*/
-const look = { word: -1, chars: -1, caret: false, key: 0 };
-
-/** Fill `look` for `o` at `tLocal` and return its picture key. A whole-text
- * picture keys at zero or above (two per word slice, bar dark and lit); a
- * typing picture keys below zero, one per typed count. */
-function lookAt(o: Overlay, tLocal: number, ev: OverlayFrameState): number {
- look.word = rasterSliceAt(o, tLocal);
- look.caret = ev.caret === true;
+interface Look {
+ key: number;
+ word: number;
+ chars: number;
+ caret: boolean;
+}
+
+/** The picture `o` needs at `tLocal`. */
+function lookAt(o: Overlay, tLocal: number, ev: OverlayFrameState): Look {
+ const word = rasterSliceAt(o, tLocal);
+ const caret = ev.caret === true;
const n = isTextOverlay(o) ? o.text.length : 0;
- look.chars =
+ const chars =
isTextOverlay(o) && ev.textProgress !== undefined
? Math.max(0, Math.min(n, Math.ceil(ev.textProgress * n)))
: -1;
- const bar = look.caret ? 1 : 0;
- look.key =
- look.chars < 0
- ? (look.word + 1) * 2 + bar
- : -1 - (((look.word + 1) * (n + 1) + look.chars) * 2 + bar);
- return look.key;
+ const bar = caret ? 1 : 0;
+ const key = chars < 0 ? (word + 1) * 2 + bar : -1 - (((word + 1) * (n + 1) + chars) * 2 + bar);
+ return { key, word, chars, caret };
}
/** The phase a picture with its typing bar lit is painted at. */
@@ -168,17 +169,17 @@ const entryBytes = (e: { byWord: Map<number, ImageBitmap> }): number => {
};
/** One element's pictures: whole-text ones by word slice and bar, and the
- * typing picture it is on. `want` is the typing picture it is waiting for. */
+ * typing picture it is on. `pending` holds the keys being drawn, `want` the
+ * typing picture it is waiting for, and `typedAt` when the pump last served
+ * it, so elements typing at once take turns. */
interface RasterEntry {
of: Overlay;
byWord: Map<number, ImageBitmap>;
pending: Set<number>;
usedAt: number;
last: ImageBitmap | null;
- want: number | null;
- wantWord: number;
- wantChars: number;
- wantCaret: boolean;
+ want: Look | null;
+ typedAt: number;
}
export class SubjectMaskCompositor {
@@ -211,6 +212,8 @@ export class SubjectMaskCompositor {
* latest count it reached. */
private typeSurface: HTMLCanvasElement | null = null;
private typing = false;
+ /** Typing pictures painted, the clock the pump takes turns on. */
+ private typed = 0;
private typeArgs: { w: number; h: number; assets: MediaAsset[] } = { w: 1, h: 1, assets: [] };
/** Frames this pass has drawn, which is the clock the pictures are given
* back on: an element drawn on the frame being composited is the one the
@@ -219,8 +222,8 @@ export class SubjectMaskCompositor {
private readonly releaseMemory = holdMemory("overlayRasters", () => {
let n = 0;
for (const e of this.rasters.values()) n += entryBytes(e);
- const dark = this.darkSurface ? this.darkSurface.width * this.darkSurface.height * 4 : 0;
- return n + this.fx.bytes() + dark;
+ const typing = this.typeSurface ? this.typeSurface.width * this.typeSurface.height * 4 : 0;
+ return n + this.fx.bytes() + typing;
});
private person: HTMLCanvasElement | null = null;
/** Scratch for blurred and motion-blurred behind elements. */
@@ -261,9 +264,7 @@ export class SubjectMaskCompositor {
usedAt: this.pass,
last: null,
want: null,
- wantWord: -1,
- wantChars: -1,
- wantCaret: false,
+ typedAt: -1,
};
this.rasters.set(o.id, entry);
}
@@ -300,32 +301,29 @@ export class SubjectMaskCompositor {
}
/**
- * The picture `look` names (see `lookAt`), or while it is being drawn the
- * last one this element landed, so a typing title moves on a frame late
- * and never blinks out.
+ * The picture `look` names, or while it is being drawn the last one this
+ * element landed, so a typing title moves on a frame late and never blinks
+ * out.
*/
- private rasterFor(o: Overlay, w: number, h: number, assets: MediaAsset[]): ImageBitmap | null {
+ private rasterFor(o: Overlay, look: Look, w: number, h: number, assets: MediaAsset[]): ImageBitmap | null {
const entry = this.entryFor(o);
- const key = look.key;
- const hit = entry.byWord.get(key);
+ const hit = entry.byWord.get(look.key);
if (hit) return hit;
- if (key < 0) {
- if (entry.want !== key) {
- entry.want = key;
- entry.wantWord = look.word;
- entry.wantChars = look.chars;
- entry.wantCaret = look.caret;
- }
- this.typeArgs.w = w;
- this.typeArgs.h = h;
- this.typeArgs.assets = assets;
- this.pumpTyping();
- } else if (!entry.pending.has(key)) {
- entry.pending.add(key);
+ if (look.key >= 0) {
+ // Done typing: a typed count still queued is a picture nothing shows.
+ entry.want = null;
+ if (entry.pending.has(look.key)) return entry.last;
+ entry.pending.add(look.key);
// Neutral picture: position aside, the per-frame pose owns rotation and
// opacity, so baking them here would apply each of them twice.
- void this.drawRaster(o, w, h, assets, key, look.word, look.caret).catch(() => {});
+ void this.drawRaster(o, w, h, assets, look.key, look.word, look.caret).catch(() => {});
+ return entry.last;
}
+ if (!entry.pending.has(look.key)) entry.want = look;
+ this.typeArgs.w = w;
+ this.typeArgs.h = h;
+ this.typeArgs.assets = assets;
+ this.pumpTyping();
return entry.last;
}
@@ -354,30 +352,22 @@ export class SubjectMaskCompositor {
}
/** One typing picture, painted on the shared surface. */
- private async drawTyped(
- o: Overlay,
- w: number,
- h: number,
- assets: MediaAsset[],
- key: number,
- word: number,
- chars: number,
- caret: boolean
- ): Promise<void> {
- this.typeSurface ??= scratchCanvas();
- if (this.typeSurface.width !== w) this.typeSurface.width = w;
- if (this.typeSurface.height !== h) this.typeSurface.height = h;
+ private async drawTyped(o: Overlay, w: number, h: number, assets: MediaAsset[], look: Look): Promise<void> {
+ const surface = (this.typeSurface ??= scratchCanvas());
+ if (surface.width !== w) surface.width = w;
+ if (surface.height !== h) surface.height = h;
await paintElementInto(
- this.typeSurface,
- this.pictureOf(o, word, chars),
+ surface,
+ this.pictureOf(o, look.word, look.chars),
cutRenderEnv(assets),
- caret ? CARET_LIT : undefined
+ look.caret ? CARET_LIT : undefined
);
- this.land(o, key, await createImageBitmap(this.typeSurface));
+ this.land(o, look.key, await createImageBitmap(surface));
}
/** Paint the typing pictures elements are waiting on, one after another,
- * each at the latest count its element asked for. */
+ * each at the latest count its element asked for. Elements typing at once
+ * take turns: the one served longest ago goes next. */
private pumpTyping(): void {
if (this.typing) return;
this.typing = true;
@@ -386,18 +376,16 @@ export class SubjectMaskCompositor {
for (;;) {
let next: RasterEntry | null = null;
for (const e of this.rasters.values()) {
- if (e.want !== null) {
- next = e;
- break;
- }
+ if (e.want && (!next || e.typedAt < next.typedAt)) next = e;
}
if (!next) break;
- const key = next.want!;
- next.want = null;
+ const entry = next;
+ const look = entry.want!;
+ entry.want = null;
+ entry.typedAt = this.typed++;
+ entry.pending.add(look.key);
const { w, h, assets } = this.typeArgs;
- await this.drawTyped(next.of, w, h, assets, key, next.wantWord, next.wantChars, next.wantCaret).catch(
- () => {}
- );
+ await this.drawTyped(entry.of, w, h, assets, look).catch(() => entry.pending.delete(look.key));
}
} finally {
this.typing = false;
@@ -433,13 +421,11 @@ export class SubjectMaskCompositor {
for (const o of overlays) {
if (!behindSubjectOverlay(o) || !drawable(o) || t < o.start || t > o.end) continue;
const tLocal = Math.max(0, t - o.start);
- const key = lookAt(o, tLocal, evalOverlayFrame(o, tLocal, w / h));
- const entry = this.entryFor(o);
- if (entry.byWord.has(key)) continue;
- const { word, chars, caret } = look;
+ const look = lookAt(o, tLocal, evalOverlayFrame(o, tLocal, w / h));
+ if (this.entryFor(o).byWord.has(look.key)) continue;
try {
- if (key < 0) await this.drawTyped(o, w, h, assets, key, word, chars, caret);
- else await this.drawRaster(o, w, h, assets, key, word, caret);
+ if (look.key < 0) await this.drawTyped(o, w, h, assets, look);
+ else await this.drawRaster(o, w, h, assets, look.key, look.word, look.caret);
} catch {
// The overlay just draws in front when its raster is missing.
}
@@ -457,6 +443,11 @@ export class SubjectMaskCompositor {
dispose(): void {
this.clear();
this.fx.dispose();
+ if (this.typeSurface) {
+ this.typeSurface.width = 1;
+ this.typeSurface.height = 1;
+ this.typeSurface = null;
+ }
this.releaseMemory();
}
@@ -580,8 +571,6 @@ export class SubjectMaskCompositor {
* the element moves under it. The surfaces live and die with this pass. */
private stampSurface: HTMLCanvasElement | null = null;
private stampScratch: HTMLCanvasElement | null = null;
- /** Where an element a hit darkens is drawn alone (see `darkTarget`). */
- private darkSurface: HTMLCanvasElement | null = null;
mattedStamp(
o: { mask?: { invert?: boolean; feather?: number } },
w: number,
@@ -609,17 +598,6 @@ export class SubjectMaskCompositor {
return this.stampSurface;
}
- /** The darkening surface, cleared and sized to the frame. */
- private darkTarget(w: number, h: number): CanvasRenderingContext2D {
- this.darkSurface ??= scratchCanvas();
- if (this.darkSurface.width !== w) this.darkSurface.width = w;
- if (this.darkSurface.height !== h) this.darkSurface.height = h;
- const c = this.darkSurface.getContext("2d")!;
- c.setTransform(1, 0, 0, 1, 0, 0);
- c.clearRect(0, 0, w, h);
- return c;
- }
-
/**
* Run the behind pass on `canvas` (video layers already drawn): draw the
* behind-tagged rasters, then the segmented person back over them. Also
@@ -682,8 +660,7 @@ export class SubjectMaskCompositor {
for (const o of active) {
const tLocal = Math.max(0, t - o.start);
const ev = evalOverlayFrame(o, tLocal, W / H);
- lookAt(o, tLocal, ev);
- const bmp = this.rasterFor(o, W, H, assets);
+ const bmp = this.rasterFor(o, lookAt(o, tLocal, ev), W, H, assets);
if (!bmp) continue;
// One cached picture per element here, so a per-glyph ramp or loop runs
// its motion over the whole box as a single letter would.
@@ -692,12 +669,9 @@ export class SubjectMaskCompositor {
if (alphaOf <= 0.001) continue;
const cx = o.x * W;
const cy = o.y * H;
- // A hit that darkens is drawn alone first, so the darkening lands on
- // the element's pixels and not on the frame under it. A blurred or
- // streaking element poses into the scratch and lands soft.
- const dark = ev.brightness !== undefined && ev.brightness < 1;
- const c = dark ? this.darkTarget(W, H) : ctx;
- const target = this.fx.begin(c, W, H, elementLook(ev, scale)) as CanvasRenderingContext2D;
+ // A blurred, streaking or darkening element poses into the scratch
+ // alone and lands softened or dimmed.
+ const target = this.fx.begin(ctx, W, H, elementLook(ev, scale)) as CanvasRenderingContext2D;
target.save();
target.globalAlpha = alphaOf;
target.translate(
@@ -718,11 +692,7 @@ export class SubjectMaskCompositor {
}
target.drawImage(bmp, 0, 0, W, H);
target.restore();
- this.fx.end(c);
- if (dark) {
- darkenCanvas(c, W, H, ev.brightness!);
- ctx.drawImage(c.canvas, 0, 0);
- }
+ this.fx.end(ctx);
}
// The person back on top, its edge softened by the widest feather any
diff --git a/site/src/cut/lib/behindPassTyping.test.ts b/site/src/cut/lib/behindPassTyping.test.ts
index a1e37953..8d674b54 100644
--- a/site/src/cut/lib/behindPassTyping.test.ts
+++ b/site/src/cut/lib/behindPassTyping.test.ts
@@ -90,4 +90,31 @@ describe("the behind pass types", () => {
expect([...entryOf(pass).byWord.keys()].some((k) => k < 0)).toBe(false);
pass.dispose();
});
+
+ test("the live pass paints each typed count once, and titles typing together take turns", async () => {
+ const pass = new SubjectMaskCompositor();
+ const painted: string[] = [];
+ const inner = pass as unknown as { drawTyped: (o: TextOverlay, ...rest: unknown[]) => Promise<void> };
+ const drawTyped = inner.drawTyped.bind(pass);
+ inner.drawTyped = (o, ...rest) => {
+ painted.push(o.id);
+ return drawTyped(o, ...rest);
+ };
+ const second: TextOverlay = { ...title, id: "cta2", y: 0.7 };
+ const frame = createRasterCanvas(W, H) as HTMLCanvasElement;
+ const settle = () => new Promise((r) => setTimeout(r, 200));
+
+ // The same moment asked for on two frames before its picture lands.
+ pass.draw(frame, [title], [], title.start + 0.4);
+ pass.draw(frame, [title], [], title.start + 0.4);
+ await settle();
+ expect(painted).toEqual([title.id]);
+
+ // Two titles typing at once, each asking for a new count every frame.
+ painted.length = 0;
+ for (const t of [0.1, 0.2, 0.3, 0.5, 0.6, 0.7]) pass.draw(frame, [title, second], [], title.start + t);
+ await settle();
+ expect(painted).toContain(second.id);
+ pass.dispose();
+ });
});
diff --git a/site/src/cut/lib/caretHitTool.test.ts b/site/src/cut/lib/caretHitTool.test.ts
index a9b1aee9..19c91f1f 100644
--- a/site/src/cut/lib/caretHitTool.test.ts
+++ b/site/src/cut/lib/caretHitTool.test.ts
@@ -61,6 +61,14 @@ describe("set_overlay_animation caret and hit", () => {
expect(overlays()[0].anim?.in?.caret).toBeUndefined();
});
+ test("a caret colour has to be a hex colour", async () => {
+ const o = await aTitle();
+ await runAiTool("set_overlay_animation", { id: o.id, in_style: "typewriter", caret: true, caret_color: "#f50" });
+ expect(overlays()[0].anim?.in?.caret?.color).toBe("#ff5500");
+ expect(await failure({ id: o.id, caret_color: "accent" })).toContain("hex colour");
+ expect(overlays()[0].anim?.in?.caret?.color).toBe("#ff5500");
+ });
+
test("a caret without a typing entrance is refused", async () => {
const o = await aTitle();
await runAiTool("set_overlay_animation", { id: o.id, in_style: "fade" });
diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.ts
index 7d8028e1..7a2a9e28 100644
--- a/site/src/cut/lib/exportRender.ts
+++ b/site/src/cut/lib/exportRender.ts
@@ -46,7 +46,6 @@ import { allowance, canvasBytes, holdMemory } from "./memoryBudget";
import { getClipSpans, overlayLayers, projectDuration, spanSequence } from "./store";
import { captionStyle, cueOverlay, cueWordFrames, laneCues, laneHidden, subtitleLaneCount, trackPos } from "./subtitles";
import { ElementFx, elementLook } from "@donkeycut/effects-kit";
-import { darkenCanvas } from "@donkeycut/effects-kit";
import { applyEffectToCanvas, diveView, divesAt, evalOverlayFrame, measureDiveFocus, retimeOf, grainTile, smoothsAt, isAudioEffect, isMaskAnimated, isOverlayAnimated, maskFrameAt, planAnimatedLayers, type DiveFocus, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";
import { backdropStill, loadBackdropStill } from "./backdropStills";
import { hasSubjectOverlays, SubjectMaskCompositor } from "./behindPass";
@@ -734,11 +733,9 @@ async function stampText(doc: ExportDoc): Promise<StampedLayer[]> {
class StampCache {
private drawn = new Map<StampedLayer, ImageBitmap>();
private maskFrames = new Map<StampedLayer, { t: number; bitmap: ImageBitmap }>();
- /** Scratch for blurred and motion-blurred stamps, made on first use. */
+ /** Scratch for blurred, motion-blurred and darkening stamps, made on first
+ * use. */
readonly fx = new ElementFx(createRasterCanvas);
- /** Where an element a hit darkens is drawn alone, so the darkening lands on
- * its pixels and not on the frame under it. Made on the first such frame. */
- private dark: RasterSurface | null = null;
constructor(
private width: number,
@@ -780,23 +777,6 @@ class StampCache {
return bitmap;
}
- /** What the scratch surfaces hold: the blur and streak scratch, and the
- * darkening surface once a hit has made it. */
- scratchBytes(): number {
- return this.fx.bytes() + (this.dark ? canvasBytes(this.dark.width * this.dark.height) : 0);
- }
-
- /** The darkening surface, cleared, at the frame's size. */
- darkTarget(): CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D {
- this.dark ??= createRasterCanvas(this.width, this.height);
- const c = this.dark.getContext("2d") as
- | CanvasRenderingContext2D
- | OffscreenCanvasRenderingContext2D;
- c.setTransform(1, 0, 0, 1, 0, 0);
- c.clearRect(0, 0, this.width, this.height);
- return c;
- }
-
/** Where a diving element flies into, measured once per look. */
diveFocus(o: Overlay): Promise<DiveFocus> {
return measureDiveFocus(o, this.width / this.height, cutRenderEnv(this.assets));
@@ -819,7 +799,6 @@ class StampCache {
}
dispose() {
- this.dark = null;
for (const bitmap of this.drawn.values()) bitmap.close();
this.drawn.clear();
for (const hit of this.maskFrames.values()) hit.bitmap.close();
@@ -1301,13 +1280,9 @@ async function drawStamps(
const w = Math.max(1, o.w * canvas.width);
const aspect = layer.lottie.width > 0 ? layer.lottie.width / layer.lottie.height : 1;
const h = w / aspect;
- // A hit that darkens is drawn alone first, so the darkening lands on
- // the sticker's pixels; a blurred or streaking one poses into the
- // scratch and lands soft.
- const look = elementLook(ev, scale);
- const dark = ev.brightness !== undefined && ev.brightness < 1;
- const c = dark ? stamps.darkTarget() : ctx;
- const lc = stamps.fx.begin(c, canvas.width, canvas.height, look);
+ // A blurred, streaking or darkening sticker poses into the scratch
+ // alone and lands softened or dimmed.
+ const lc = stamps.fx.begin(ctx, canvas.width, canvas.height, elementLook(ev, scale));
lc.save();
lc.globalAlpha = ev.opacity;
lc.translate(ev.x * canvas.width + ev.dx * scale, ev.y * canvas.height + ev.dy * scale);
@@ -1315,11 +1290,7 @@ async function drawStamps(
lc.scale(ev.scale, ev.scale);
lc.drawImage(layer.lottie.seek(t - (layer.animStart ?? layer.start)), -w / 2, -h / 2, w, h);
lc.restore();
- stamps.fx.end(c);
- if (dark) {
- darkenCanvas(c, canvas.width, canvas.height, ev.brightness!);
- ctx.drawImage(c.canvas, 0, 0);
- }
+ stamps.fx.end(ctx);
continue;
}
// Unkeyed, the bitmap bakes the element's own rotation/opacity and only
@@ -1388,11 +1359,9 @@ async function drawStamps(
c.translate(-cx, -cy);
c.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
};
- // A blurred or streaking element poses into the scratch first and lands
- // softened; a sharp one draws straight onto the frame. A hit that darkens
- // needs a surface holding the element alone: the matte's, or the dark one.
+ // A blurred, streaking or darkening element poses into the scratch alone
+ // and lands softened or dimmed; a sharp one draws straight onto the frame.
const look = elementLook(ev, scale);
- const dark = ev.brightness !== undefined && ev.brightness < 1;
const lay = (into: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => {
const target = stamps.fx.begin(into, canvas.width, canvas.height, look);
target.save();
@@ -1400,7 +1369,6 @@ async function drawStamps(
posed(target);
target.restore();
stamps.fx.end(into);
- if (dark) darkenCanvas(into, canvas.width, canvas.height, ev.brightness!);
};
if (subjectFront) {
// Pose the stamp inside the matte surface, so the matte stays put in
@@ -1408,10 +1376,6 @@ async function drawStamps(
// lands first and the matte trims after, so the person's edge stays
// sharp over a moving element, as the preview shows it.
ctx.drawImage(subject!.mattedStamp(layer.overlay, canvas.width, canvas.height, lay), 0, 0);
- } else if (dark) {
- const c = stamps.darkTarget();
- lay(c);
- ctx.drawImage(c.canvas, 0, 0);
} else {
lay(ctx);
}
@@ -1437,7 +1401,7 @@ export class FramePainter {
private readonly releaseMemory = holdMemory("exportReaders", () => {
let n = 0;
for (const r of this.readers.values()) n += canvasBytes(readerPoolFrames(r) * r.sourcePixels);
- return n + this.stamps.scratchBytes();
+ return n + this.stamps.fx.bytes();
});
/** Frames drawn, which is the clock the readers' eviction order runs on. */
private frameNo = 0;
diff --git a/site/src/cut/lib/itemKinds.test.ts b/site/src/cut/lib/itemKinds.test.ts
index 3e49d795..06815871 100644
--- a/site/src/cut/lib/itemKinds.test.ts
+++ b/site/src/cut/lib/itemKinds.test.ts
@@ -166,4 +166,17 @@ describe("splitting keyed items", () => {
expect(right.kf![0]).toMatchObject({ t: 0, x: 0.3 });
expect(right.mask!.kf![0]).toMatchObject({ t: 0, x: 0.1 });
});
+
+ test("an element's press stays at its moment on the timeline", () => {
+ const o = {
+ id: "h1", kind: "shape", start: 0, end: 4, x: 0.5, y: 0.5,
+ anim: { hit: { style: "press", at: 2.5, seconds: 0.3 } },
+ };
+ const [left, right] = ITEM_KINDS.overlay.split!(o as never, 1);
+ expect(left.anim?.hit).toBeUndefined();
+ expect(right.anim!.hit!.at).toBeCloseTo(1.5, 9);
+ const [early, late] = ITEM_KINDS.overlay.split!(o as never, 3);
+ expect(early.anim!.hit!.at).toBe(2.5);
+ expect(late.anim?.hit).toBeUndefined();
+ });
});
diff --git a/site/src/cut/lib/itemKinds.ts b/site/src/cut/lib/itemKinds.ts
index 76c623aa..1a9f769e 100644
--- a/site/src/cut/lib/itemKinds.ts
+++ b/site/src/cut/lib/itemKinds.ts
@@ -13,7 +13,7 @@
// compile until it has an entry, and `itemKinds.test.ts` walks the table so
// every entry proves it copies and pastes.
-import { KEY_EPSILON, maskKeyAt, poseAt, retimeOf, shiftCamera, type Mask, type OverlayKey, type OverlayPose } from "@donkeycut/effects-kit";
+import { KEY_EPSILON, maskKeyAt, poseAt, retimeOf, shiftCamera, type Mask, type OverlayAnim, type OverlayKey, type OverlayPose } from "@donkeycut/effects-kit";
import { splitEmphasis, withEmphasis } from "./captionEmphasis";
import {
clipPoseAt,
@@ -110,6 +110,20 @@ function splitTracks(
];
}
+/** An element's press cut at `cut` seconds into it: each half keeps it
+ * while its window reaches that half, the right half counting from its own
+ * start, so the press lands where it did on the timeline. */
+function splitHit(anim: OverlayAnim | undefined, cut: number): [OverlayAnim | undefined, OverlayAnim | undefined] {
+ const hit = anim?.hit;
+ if (!hit) return [anim, anim];
+ const { hit: _drop, ...rest } = anim;
+ void _drop;
+ return [
+ hit.at < cut ? anim : rest,
+ hit.at + hit.seconds > cut ? { ...rest, hit: { ...hit, at: hit.at - cut } } : rest,
+ ];
+}
+
function splitMedia<T extends VideoClip | AudioClip>(item: T, at: number): [T, T] {
const cut = retimeOf(item).srcAt(at - item.start);
return item.reverse
@@ -190,9 +204,10 @@ export const ITEM_KINDS: { [K in ItemKind]: ItemKindDef<K> } = {
// The tail keeps filming on the group camera's clock.
split: (o, at) => {
const [keysL, keysR] = splitTracks(o, at - o.start, (t) => poseAt(o, t));
+ const [animL, animR] = splitHit(o.anim, at - o.start);
return [
- { ...o, ...keysL, end: at },
- { ...o, ...keysR, start: at, ...(o.camera ? { camera: shiftCamera(o.camera, o.start - at) } : {}) },
+ { ...o, ...keysL, anim: animL, end: at },
+ { ...o, ...keysR, anim: animR, start: at, ...(o.camera ? { camera: shiftCamera(o.camera, o.start - at) } : {}) },
];
},
clone: deep,
diff --git a/site/src/cut/lib/subtitles.ts b/site/src/cut/lib/subtitles.ts
index 17664846..812e07b6 100644
--- a/site/src/cut/lib/subtitles.ts
+++ b/site/src/cut/lib/subtitles.ts
@@ -511,7 +511,7 @@ function faceEntry(cue: SubtitleCue, pos: CaptionEmphasisOverrides | undefined):
/** Per display word: the emphasis face, or nothing. Null when no word in the
* cue is emphasized. */
-export function cueFaces(
+function cueFaces(
cue: SubtitleCue,
pos: CaptionEmphasisOverrides | undefined
): (WordFace | undefined)[] | null {