Open source CapCut alternative.
TypeScript
feat(effects-kit): seed slot reels by the title's text [rebuild]
David Le2eedf9029d4e
diff --git a/site/packages/effects-kit/src/dive.test.ts b/site/packages/effects-kit/src/dive.test.tsindex 760f3373..dd45c4e5 100644--- a/site/packages/effects-kit/src/dive.test.ts+++ b/site/packages/effects-kit/src/dive.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from "bun:test";-import { diveView, DIVE_MAX_SCALE, slotReel } from "./dive";+import { diveView, DIVE_MAX_SCALE, slotReel, slotSeed } from "./dive";import { evalOverlayAnim } from "./anim";const frame = { width: 1080, height: 1920, scale: 1 };@@ -11,7 +11,7 @@ describe("slot reels", () => {for (const ch of ["A", "z", "7"]) {for (let i = 0; i < 40; i++) {for (let roll = 0; roll <= 5; roll += 0.13) {- for (const r of slotReel(ch, i, roll)) {+ for (const r of slotReel(ch, i, roll, slotSeed("THE END"))) {expect(r.ch.length).toBe(1);if (ch === "7") expect(r.ch).toMatch(/[0-9]/);else if (ch === "A") expect(r.ch).toMatch(/[A-Z]/);@@ -23,8 +23,17 @@ describe("slot reels", () => {});test("a landed reel shows the character alone, and a space never rolls", () => {- expect(slotReel("Q", 3, 0)).toEqual([{ ch: "Q", y: 0, alpha: 1 }]);- expect(slotReel(" ", 3, 2.5)).toEqual([{ ch: " ", y: 0, alpha: 1 }]);+ expect(slotReel("Q", 3, 0, 1)).toEqual([{ ch: "Q", y: 0, alpha: 1 }]);+ expect(slotReel(" ", 3, 2.5, 1)).toEqual([{ ch: " ", y: 0, alpha: 1 }]);+ });++ test("each title rolls through its own fillers, the same way every time", () => {+ const fillers = (title: string) =>+ [...title].flatMap((ch, i) => [1, 2, 3, 4].map((roll) => slotReel(ch, i, roll, slotSeed(title))[0].ch)).join("");+ expect(fillers("THE END")).toBe(fillers("THE END"));+ expect(fillers("THE END")).not.toBe(fillers("THE ENE"));+ // Wrapping only moves line breaks, so it never changes the roll.+ expect(slotSeed("THE\nEND")).toBe(slotSeed("THE END"));});});diff --git a/site/packages/effects-kit/src/dive.ts b/site/packages/effects-kit/src/dive.tsindex 501447e3..98e6c2d9 100644--- a/site/packages/effects-kit/src/dive.ts+++ b/site/packages/effects-kit/src/dive.ts@@ -95,12 +95,14 @@ export const SLOT_PITCH = 0.62;/** A slot character's reel: what is in sight at `roll`, each at its vertical* offset in line heights (positive is below) and its own opacity. Letters roll* through letters of their own case and digits through digits; anything else- * — a space, a mark — holds still. The fillers are a fixed draw per position,- * so the same title always rolls through the same characters. */+ * — a space, a mark — holds still. The fillers are a fixed draw per title and+ * position (`seed` is `slotSeed` of the title), so each title rolls through+ * its own characters and rolls the same way every time it plays. */export function slotReel(ch: string,index: number,- roll: number+ roll: number,+ seed: number): { ch: string; y: number; alpha: number }[] {const pool = reelPool(ch);if (!pool || roll <= 0) return [{ ch, y: 0, alpha: 1 }];@@ -110,7 +112,7 @@ export function slotReel(const y = (roll - j) * SLOT_PITCH;// Past the band's edge nothing of the character shows.if (Math.abs(y) >= 0.9) continue;- out.push({ ch: j === 0 ? ch : filler(pool, ch, index, j), y, alpha: 1 - Math.min(1, Math.abs(y)) * 0.7 });+ out.push({ ch: j === 0 ? ch : filler(pool, ch, index, j, seed), y, alpha: 1 - Math.min(1, Math.abs(y)) * 0.7 });}return out;}@@ -125,8 +127,16 @@ function reelPool(ch: string): string | null {return ch === ch.toUpperCase() ? UPPER : LOWER;}-function filler(pool: string, ch: string, index: number, step: number): string {- let h = (Math.imul(index + 1, 2654435761) ^ Math.imul(step, 40503)) >>> 0;+/** A title's reel seed: its characters, with the line breaks wrapping puts in+ * left out, so every renderer seeds the same title the same way. */+export function slotSeed(text: string): number {+ let h = 2166136261;+ for (const ch of text.replace(/\s+/g, "")) h = Math.imul(h ^ ch.codePointAt(0)!, 16777619);+ return h >>> 0;+}++function filler(pool: string, ch: string, index: number, step: number, seed: number): string {+ let h = (Math.imul(index + 1, 2654435761) ^ Math.imul(step, 40503) ^ seed) >>> 0;h = Math.imul(h ^ (h >>> 15), 2246822519) >>> 0;const pick = pool[((h ^ (h >>> 13)) >>> 0) % pool.length];return pick === ch ? pool[(pool.indexOf(pick) + 1) % pool.length] : pick;diff --git a/site/packages/effects-kit/src/render.ts b/site/packages/effects-kit/src/render.tsindex b9692a4e..42a8c264 100644--- a/site/packages/effects-kit/src/render.ts+++ b/site/packages/effects-kit/src/render.ts@@ -17,7 +17,7 @@ import {type GlyphPhase,type OverlayAnim,} from "./anim";-import { diveView, slotReel, type DiveFocus, type DiveView } from "./dive";+import { diveView, slotReel, slotSeed, type DiveFocus, type DiveView } from "./dive";import { presetExtent } from "./motion/evaluate";import { evalOverlayFrame, hasOverlayKeys, poseAt, poseExtent, sortedKeys } from "./keys";import { applyMaskToCanvas, isMaskAnimated } from "./mask";@@ -519,7 +519,7 @@ async function paintText(ctx.rect(-w * 3, -lineH / 2, w * 6, lineH);ctx.clip();const a = ctx.globalAlpha;- for (const r of slotReel(ch, gi - 1, g.roll)) {+ for (const r of slotReel(ch, gi - 1, g.roll, slotSeed(overlay.text))) {ctx.globalAlpha = a * r.alpha;drawText(r.ch, 0, r.y * lineH);}diff --git a/site/src/cut/components/AnimationTiles.tsx b/site/src/cut/components/AnimationTiles.tsxindex 9447601e..5acd5d4a 100644--- a/site/src/cut/components/AnimationTiles.tsx+++ b/site/src/cut/components/AnimationTiles.tsx@@ -12,6 +12,7 @@ import {presetExtent,sampleProperties,slotReel,+ slotSeed,TEXT_ONLY_ANIM_STYLE_IDS,OVERLAY_ANIM_STYLE_IDS,OVERLAY_ANIM_STYLE_LABELS,@@ -235,7 +236,7 @@ function LiveName({/** A slot letter's reel, drawn into its empty windows. */const drawReel = (kid: HTMLElement, i: number, roll: number) => {const windows = kid.querySelectorAll<HTMLElement>("[data-reel]");- const reel = roll ? slotReel(chars[i] ?? "", i, roll) : [{ ch: chars[i] ?? "", y: 0, alpha: 1 }];+ const reel = roll ? slotReel(chars[i] ?? "", i, roll, slotSeed(label)) : [{ ch: chars[i] ?? "", y: 0, alpha: 1 }];windows.forEach((w, k) => {const r = reel[k];w.textContent = r ? r.ch : "";diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsxindex 114d9f98..c821a200 100644--- a/site/src/cut/components/OverlayLayer.tsx+++ b/site/src/cut/components/OverlayLayer.tsx@@ -18,7 +18,7 @@ import {subtitleLaneCount,trackPos,} from "@/cut/lib/subtitles";-import { diveView, evalOverlayFrame, glyphStateAt, measureDiveFocus, paintElementInto, slotReel, hasGlyphMotion, hasMaskKeys, hasOverlayKeys, isOverlayAnimated, lineLikeShape, MASK_FEATHER_MAX, MASK_RADIUS_MAX, maskFrameAt, maskHasRadius, maskInverts, maskOutlinePathD, maskSizeAxes, overlayWords, paintMaskCoverage, PEN_MIN_POINTS, penClosed, resolveShadow, shapeMetrics, shapePathD, WORD_ACCENT_DEFAULT, wordDrawsAt, type LottieHandle, type Mask, type MaskKey, type DiveFocus, type MaskPoint, type OverlayFrameState, type WordDraw } from "@donkeycut/effects-kit";+import { diveView, evalOverlayFrame, glyphStateAt, measureDiveFocus, paintElementInto, slotReel, slotSeed, hasGlyphMotion, hasMaskKeys, hasOverlayKeys, isOverlayAnimated, lineLikeShape, MASK_FEATHER_MAX, MASK_RADIUS_MAX, maskFrameAt, maskHasRadius, maskInverts, maskOutlinePathD, maskSizeAxes, overlayWords, paintMaskCoverage, PEN_MIN_POINTS, penClosed, resolveShadow, shapeMetrics, shapePathD, WORD_ACCENT_DEFAULT, wordDrawsAt, type LottieHandle, type Mask, type MaskKey, type DiveFocus, type MaskPoint, type OverlayFrameState, type WordDraw } from "@donkeycut/effects-kit";import {LINE_HEIGHT,PLATE_PAD_X,@@ -1285,6 +1285,7 @@ function GlyphText({scale: number;}) {const total = Math.max(1, [...text].length);+ const seed = slotSeed(text);let gi = 0;return (<>@@ -1294,7 +1295,7 @@ function GlyphText({const g = glyphStateAt(phase, i, total);// A slot character shows its reel through its own line band; the// band clips top and bottom only, so a wide filler is not cut.- const reel = g.roll ? slotReel(ch, i, g.roll) : null;+ const reel = g.roll ? slotReel(ch, i, g.roll, seed) : null;return (<spankey={i}