Skip to content

Open source CapCut alternative.

TypeScript
7.0 KB
Raw Blame
import { describe, expect, test } from "bun:test";
import { diveView, DIVE_MAX_SCALE, reelBand, slotReel, slotSeed } from "./dive";
import { evalOverlayAnim } from "./anim";

const frame = { width: 1080, height: 1920, scale: 1 };
const rest = { x: 0.5, y: 0.5 };
const still = { x: 0.5, y: 0.5, dx: 0, dy: 0, scale: 1, rotation: 0 };

describe("slot reels", () => {
  test("every character in sight is a real character of the target's kind", () => {
    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, 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]/);
            else expect(r.ch).toMatch(/[a-z]/);
          }
        }
      }
    }
  });

  test("a landed reel shows the character alone, and a space never rolls", () => {
    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"));
  });
});

describe("dive", () => {
  const focus = { x: 120, y: 30, r: 24 };

  test("at rest the view is the identity", () => {
    const v = diveView(0, focus, still, rest, frame);
    expect(v.s).toBe(1);
    expect(v.tx).toBe(v.fx);
    expect(v.ty).toBe(v.fy);
  });

  test("landed, the ink around the focus reaches every corner from the frame's center", () => {
    const v = diveView(1, focus, still, rest, frame);
    expect(v.tx).toBeCloseTo(540, 6);
    expect(v.ty).toBeCloseTo(960, 6);
    expect(v.s * focus.r).toBeGreaterThanOrEqual(Math.hypot(540, 960));
    expect(v.s).toBeLessThanOrEqual(DIVE_MAX_SCALE);
  });

  test("a posed element lands its focus on the frame's center through the pose", () => {
    const pose = { x: 0.3, y: 0.7, dx: 10, dy: -20, scale: 1.5, rotation: 30 };
    const v = diveView(1, focus, pose, rest, frame);
    const cx = rest.x * frame.width;
    const cy = rest.y * frame.height;
    // P(t): the pose applied to the view's target.
    const th = (pose.rotation * Math.PI) / 180;
    const ux = v.tx - cx;
    const uy = v.ty - cy;
    const X = pose.x * frame.width + pose.dx + pose.scale * (Math.cos(th) * ux - Math.sin(th) * uy);
    const Y = pose.y * frame.height + pose.dy + pose.scale * (Math.sin(th) * ux + Math.cos(th) * uy);
    expect(X).toBeCloseTo(540, 6);
    expect(Y).toBeCloseTo(960, 6);
  });

  test("anchored, the dive zooms about its point where it stands until the ink around it covers the frame", () => {
    const anchored = { ...focus, at: { out: { x: -60, y: -200, r: 18 } } };
    const pose = { ...still, diveEdge: "out" as const };
    // The anchor's place on screen: the element's rest plus its offset.
    const ax = 540 - 60;
    const ay = 960 - 200;
    for (const e of [0.25, 0.5, 1]) {
      const v = diveView(e, anchored, pose, rest, frame);
      expect(v.fx).toBeCloseTo(ax, 6);
      expect(v.fy).toBeCloseTo(ay, 6);
      expect(v.tx).toBeCloseTo(ax, 6);
      expect(v.ty).toBeCloseTo(ay, 6);
    }
    const landed = diveView(1, anchored, pose, rest, frame);
    const far = Math.max(...[[0, 0], [1080, 0], [0, 1920], [1080, 1920]].map(([x, y]) => Math.hypot(x - ax, y - ay)));
    expect(landed.s * 18).toBeGreaterThanOrEqual(far);
    // The other edge, or none named, flies into the deepest ink as before.
    expect(diveView(1, anchored, still, rest, frame).tx).toBeCloseTo(540, 6);
    expect(diveView(1, anchored, { ...still, diveEdge: "in" as const }, rest, frame).tx).toBeCloseTo(540, 6);
  });

  test("an anchored edge names itself on the pose; an unanchored one doesn't", () => {
    const anchor = { x: -60, y: -200 };
    expect(evalOverlayAnim({ out: { style: "dive", seconds: 1, anchor } }, 2.5, 3).diveEdge).toBe("out");
    expect(evalOverlayAnim({ in: { style: "dive", seconds: 1, anchor } }, 0.5, 3).diveEdge).toBe("in");
    expect(evalOverlayAnim({ out: { style: "dive", seconds: 1 } }, 2.5, 3).diveEdge).toBeUndefined();
  });

  test("an ease shapes the flight: an ease-in holds back early and lands on time", () => {
    const own = { out: { style: "dive" as const, seconds: 1 } };
    const eased = { out: { style: "dive" as const, seconds: 1, ease: "power2.in" as const } };
    expect(evalOverlayAnim(eased, 2.3, 3).dive!).toBeLessThan(evalOverlayAnim(own, 2.3, 3).dive! - 0.05);
    expect(evalOverlayAnim(eased, 2.3, 3).dive).toBeCloseTo(0.09, 1);
    expect(evalOverlayAnim(eased, 3, 3).dive).toBeCloseTo(1, 6);
  });

  test("an exit dives in across its ramp and an entrance pulls back out", () => {
    const out = { out: { style: "dive" as const, seconds: 1 } };
    expect(evalOverlayAnim(out, 2.001, 3).dive).toBeCloseTo(0, 2);
    expect(evalOverlayAnim(out, 2.5, 3).dive).toBeCloseTo(0.5, 1);
    expect(evalOverlayAnim(out, 3, 3).dive).toBeCloseTo(1, 6);
    const inn = { in: { style: "dive" as const, seconds: 1 } };
    expect(evalOverlayAnim(inn, 0, 3).dive).toBeCloseTo(1, 6);
    expect(evalOverlayAnim(inn, 0.999, 3).dive).toBeLessThan(0.01);
  });
});

describe("a slot reel's own shape", () => {
  const seed = slotSeed("2026");

  test("a reel turning down brings the landing character in from above", () => {
    const up = slotReel("6", 3, 0.5, seed).find((r) => r.ch === "6")!;
    const down = slotReel("6", 3, 0.5, seed, { dir: "down" }).find((r) => r.ch === "6")!;
    expect(up.y).toBeGreaterThan(0);
    expect(down.y).toBeCloseTo(-up.y, 6);
  });

  test("its characters sit `pitch` em apart, shown through a band `band` em either side", () => {
    // 1 em is 1/1.2 of a line at line height 1.2.
    const em = 1 / 1.2;
    const reel = slotReel("6", 3, 0.25, seed, { pitch: 1.5, band: 1 }, em);
    const ys = reel.map((r) => r.y).sort((a, b) => a - b);
    for (let k = 1; k < ys.length; k++) expect(ys[k] - ys[k - 1]).toBeCloseTo(1.5 * em, 6);
    expect(reelBand({ band: 1 }, em)).toBeCloseTo(em, 6);
    expect(reelBand(undefined, em)).toBe(0.5);
  });

  test("a solid reel keeps every character at full strength", () => {
    for (const r of slotReel("6", 3, 0.4, seed, { solid: true })) expect(r.alpha).toBe(1);
    expect(slotReel("6", 3, 0.4, seed).some((r) => r.alpha < 1)).toBe(true);
  });

  test("a reel run past its slot shows the landing character beyond it", () => {
    const past = slotReel("6", 3, -0.3, seed, { dir: "down", pitch: 1.5 }, 1 / 1.2);
    const six = past.find((r) => r.ch === "6")!;
    // Turning down, the overshoot carries it below its slot.
    expect(six.y).toBeGreaterThan(0);
  });
});