Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

5.5 KB
Raw Blame
/**
 * Keys, tracks and timelines (pure). A track is a list of keys { t, v, ease } sampled at an absolute time; a timeline
 * is a scene's list of timed events (what moves, when, for how long, on which curve), built once per recipe and then
 * read at any t. The same events drive the timing checks in the tests and, later, the sound made from motion.
 */

import { curve, type CurveName, type Ease } from "./ease";
import { local, mix } from "./time";

export interface Key {
  t: number;
  v: number;
  /** The curve into this key from the one before it (default "move"). */
  ease?: CurveName;
}

/** A track's value at `t`: the first key's before it, the last key's after it, eased in between. */
export function sample(keys: readonly Key[], t: number): number {
  if (!keys.length) return 0;
  if (t <= keys[0].t) return keys[0].v;
  const last = keys[keys.length - 1];
  if (t >= last.t) return last.v;
  for (let i = 1; i < keys.length; i++) {
    const b = keys[i];
    if (t <= b.t) {
      const a = keys[i - 1];
      return mix(a.v, b.v, curve(b.ease ?? "move")(local(t, a.t, b.t)));
    }
  }
  return last.v;
}

/** When item `i` of `n` starts: `each` seconds apart, squeezed so the last starts at most `total` after the first. */
export function stagger(i: number, n: number, each: number, total = Infinity): number {
  if (n <= 1) return 0;
  return i * Math.min(each, total / (n - 1));
}

/**
 * What a timing rule calls a moving thing (tests/motion-timing.test.ts checks each kind's limits). `hero` is the
 * entrance of type taller than a quarter of the frame (it may take up to 1 s); `slam` is the short accelerating hit of
 * a stomp (0.12–0.3 s).
 */
export type EventKind = "enter" | "hero" | "slam" | "pop" | "fade" | "draw" | "move" | "camera" | "exit";

/** How a group of events is spaced: letters (20–50 ms), words (40–80 ms), lines (80–200 ms) or elements (80–140 ms). */
export type StaggerUnit = "letters" | "words" | "lines" | "elements";

export interface MotionEvent {
  /** Stable name ("headline.line.1", "cta.pill"). */
  id: string;
  kind: EventKind;
  start: number;
  dur: number;
  curve: CurveName;
  /** Events in one staggered group share it, with how they are spaced. */
  group?: string;
  unit?: StaggerUnit;
  /** A retimed motion group's clock (Kinetic's editor): plain progress in, warped progress out (never falls). */
  warp?: (u: number) => number;
}

export interface Timeline {
  events: MotionEvent[];
  byId: Map<string, MotionEvent>;
  /** Every element is set by then. */
  rest: number;
  /** A motion feel's curves, read before the named ones (absent: the curves as designed). */
  curves?: Partial<Record<CurveName, Ease>>;
}

export function timeline(events: MotionEvent[], curves?: Partial<Record<CurveName, Ease>>): Timeline {
  const byId = new Map(events.map((e) => [e.id, e]));
  const tl: Timeline = { events, byId, rest: events.reduce((m, e) => Math.max(m, e.start + e.dur), 0) };
  if (curves && Object.keys(curves).length) tl.curves = curves;
  return tl;
}

/** The curve a timeline uses for a name (its feel's, else the named one). */
export const curveIn = (tl: Pick<Timeline, "curves">, name: CurveName): Ease => tl.curves?.[name] ?? curve(name);

/** An event's eased progress at `t` (0 before it, 1 after it; an unknown id is done). */
export function progress(tl: Timeline, id: string, t: number): number {
  const e = tl.byId.get(id);
  if (!e) return 1;
  const u = local(t, e.start, e.start + e.dur);
  return curveIn(tl, e.curve)(e.warp ? e.warp(u) : u);
}

/** An event's plain (linear) progress at `t` (a retimed group's warp included). */
export function linearProgress(tl: Timeline, id: string, t: number): number {
  const e = tl.byId.get(id);
  if (!e) return 1;
  const u = local(t, e.start, e.start + e.dur);
  return e.warp ? e.warp(u) : u;
}

/** Limits per kind, in seconds: an entrance 350–700 ms, nothing that moves under 200 ms. */
export const DURATION_LIMITS: Record<EventKind, [number, number]> = {
  enter: [0.35, 0.7],
  hero: [0.35, 1],
  slam: [0.12, 0.3],
  pop: [0.35, 0.7],
  fade: [0.2, 1.5],
  draw: [0.2, 1.5],
  move: [0.2, 3],
  camera: [0.2, 30],
  exit: [0.2, 0.7],
};

export const STAGGER_LIMITS: Record<StaggerUnit, [number, number]> = {
  letters: [0.02, 0.05],
  words: [0.04, 0.08],
  lines: [0.08, 0.2],
  elements: [0.08, 0.14],
};

const clampTo = (v: number, [lo, hi]: [number, number]) => Math.min(hi, Math.max(lo, v));

/**
 * A timeline written for a 2.5 s card (everything set by 1.3 s), scaled by `k` for a shorter or longer one: starts and
 * spacing scale, durations scale within their kind's limits, staggers within their unit's.
 */
export interface Cue {
  id: string;
  kind: EventKind;
  /** Seconds at k = 1. */
  at: number;
  dur: number;
  curve: CurveName;
  /** A staggered group: this is item `index` of `count`, `each` seconds apart (at k = 1). */
  stagger?: { group: string; unit: StaggerUnit; index: number; each: number };
}

export function scaleCues(cues: Cue[], k: number): MotionEvent[] {
  return cues.map((c) => {
    const each = c.stagger ? clampTo(c.stagger.each * k, STAGGER_LIMITS[c.stagger.unit]) : 0;
    const base = c.stagger ? c.at - c.stagger.index * c.stagger.each : c.at;
    const start = base * k + (c.stagger ? c.stagger.index * each : 0);
    return {
      id: c.id,
      kind: c.kind,
      start: Math.round(start * 1e6) / 1e6,
      dur: Math.round(clampTo(c.dur * k, DURATION_LIMITS[c.kind]) * 1e6) / 1e6,
      curve: c.curve,
      ...(c.stagger && { group: c.stagger.group, unit: c.stagger.unit }),
    };
  });
}