Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

TL7a: in progress: <Transition> brings a part in and takes it out, and a transition on a cut is that code

shinyobjectz0173108f72b2
diff --git a/docs/manual/reference/sdk/index.md b/docs/manual/reference/sdk/index.md
index 9ad2af3f..a5a33bbb 100644
--- a/docs/manual/reference/sdk/index.md
+++ b/docs/manual/reference/sdk/index.md
@@ -31,7 +31,7 @@ The composition, the app, and the specs and notes an agent writes.
| Import | What it's for | Exports |
| --- | --- | --- |
-| [`raea`](raea.md) | The main entry: compositions, sequences, time, interpolation, springs, footage, sound and words. | 18 components, 8 hooks, 55 functions, 9 values, 91 types |
+| [`raea`](raea.md) | The main entry: compositions, sequences, time, interpolation, springs, footage, sound and words. | 19 components, 8 hooks, 55 functions, 9 values, 94 types |
| [`raea/app`](app.md) | The App, its plugins and schedules, and the registry a video's compositions are kept in. | 1 component, 4 functions, 3 classes, 17 values, 8 types |
| [`raea/brief`](brief.md) | The specs an agent writes as data: brief, script, taste, plan and controller, checked against raea's schemas. | 6 functions, 32 types |
| [`raea/note`](note.md) | A person's note as a file in the project: their words kept as written, and the asks the agent restates it as. | 1 function, 4 types |
diff --git a/docs/manual/reference/sdk/raea.md b/docs/manual/reference/sdk/raea.md
index 379ee00e..236677f3 100644
--- a/docs/manual/reference/sdk/raea.md
+++ b/docs/manual/reference/sdk/raea.md
@@ -38,7 +38,7 @@ See [Your first video](../../start/first-video.md) for the whole loop, and
import { AbsoluteFill, ACTION_KEY, actionsIn, Animate } from "raea"
```
-`raea` exports 18 components, 8 hooks, 55 functions, 9 values, 91 types. Source: [sdk/raea/src/index.tsx](https://github.com/shinyobjectz/raea/blob/main/sdk/raea/src/index.tsx).
+`raea` exports 19 components, 8 hooks, 55 functions, 9 values, 94 types. Source: [sdk/raea/src/index.tsx](https://github.com/shinyobjectz/raea/blob/main/sdk/raea/src/index.tsx).
## Components
@@ -246,6 +246,16 @@ render gives the same element back.
Also takes the props of [`StaggerProps`](animation.md#staggerprops) (`each`, `grid`, `exitEach`); [`AnimateOwnProps`](animation.md#animateownprops).
+### `Transition`
+
+Its children, coming in over the part's first frames and going out over its last.
+
+| Prop | Type | What it does |
+| --- | --- | --- |
+| `in` | `TransitionEdge` | over the part's first frames |
+| `out` | `TransitionEdge` | over the part's last frames |
+| `children` | `ReactNode` | the part it brings in and takes out: what the <Sequence> holds |
+
### `Video`
A video clip in step with the frame; `data-source-time` is the source time (seconds) to show.
@@ -1687,6 +1697,27 @@ many steps in the state at least (`after`), and what to do on the way (`actions`
type TransitionConfig = Target<N> & When<C, E> & { /** steps in the state before this transition may fire (0) */ after?: number; actions?: Actions<C, E>; /** what the transition means: a decider reads it (a model's option description), reports show it */ description?: string; }
```
+### `TransitionEdge`
+
+| Field | Type | What it is |
+| --- | --- | --- |
+| `kind` (required) | `TransitionKind` | which transition: fade, slide, wipe or zoom |
+| `frames` (required) | `number` | how many of the part's frames it takes |
+
+### `TransitionKind`
+
+fade: through black. slide: in from the right, out to the left. wipe: uncovered from the left, covered from the
+left. zoom: grows in from 85%, shrinks out to 85%, fading. A dissolve between two parts that only touch has no
+frames of the other to mix with, so it is written as a fade.
+
+```ts
+type TransitionKind = "fade" | "slide" | "wipe" | "zoom"
+```
+
+### `TransitionProps`
+
+The props of [`Transition`](#transition), tabled there.
+
### `TransitionRecord`
One transition as a row: what a trace keeps and rdb loads (step, machine, from, to, edge, decider, evidence). `from` is
diff --git a/sdk/raea/conformance/transition.test.tsx b/sdk/raea/conformance/transition.test.tsx
new file mode 100644
index 00000000..a15ebd00
--- /dev/null
+++ b/sdk/raea/conformance/transition.test.tsx
@@ -0,0 +1,36 @@
+// <Transition>: how a part comes in and goes out, as the timeline writes a transition on a cut
+// (docs/timeline.feature TL7a).
+import { describe, expect, test } from "bun:test";
+import { Transition, transitionAt, transitionStyle } from "@raea/video/transition";
+import { Sequence } from "../src";
+import { renderFrame } from "../src/test";
+
+const opts = { fps: 30, width: 1920, height: 1080, durationInFrames: 300 };
+
+describe("a part comes in over its first frames and goes out over its last", () => {
+ test("how far into view it is: 0 before, 1 once in, counted in the part's own frames", () => {
+ const edge = { kind: "fade" as const, frames: 10 };
+ expect([0, 4, 9, 50].map((f) => transitionAt(edge, "in", f, 60))).toEqual([0.1, 0.5, 1, 1]);
+ expect([0, 49, 50, 55, 59].map((f) => transitionAt(edge, "out", f, 60))).toEqual([1, 1, 1, 0.5, 0.1]);
+ expect(transitionAt(undefined, "in", 0, 60)).toBe(1);
+ expect(transitionAt({ kind: "fade", frames: 0 }, "in", 0, 60)).toBe(1);
+ });
+
+ test("each kind's look: a fade's opacity, a slide's move, a wipe's cover, a zoom's size; nothing once it is in", () => {
+ expect(transitionStyle("fade", "in", 0.5)).toEqual({ opacity: 0.5 });
+ expect(transitionStyle("slide", "in", 0)).toEqual({ transform: "translateX(100%)" });
+ expect(transitionStyle("slide", "out", 0)).toEqual({ transform: "translateX(-100%)" });
+ expect(transitionStyle("wipe", "in", 0)).toEqual({ clipPath: "inset(0 100% 0 0)" });
+ expect(transitionStyle("zoom", "in", 0)).toEqual({ transform: "scale(0.85)", opacity: 0 });
+ expect(transitionStyle("slide", "in", 1)).toEqual({});
+ });
+
+ test("in a <Sequence> it plays at the sequence's ends and leaves the middle as it was", () => {
+ const at = (frame: number) => renderFrame(<Sequence from={100} durationInFrames={60}><Transition in={{ kind: "fade", frames: 10 }} out={{ kind: "slide", frames: 10 }}><p>hi</p></Transition></Sequence>, { ...opts, frame });
+ expect(at(104)).toContain("opacity:0.5");
+ expect(at(130)).not.toContain("opacity");
+ expect(at(130)).not.toContain("translateX");
+ expect(at(159)).toContain("translateX(-81%)");
+ expect(() => renderFrame(<Transition in={{ kind: "spin" as never, frames: 10 }}>x</Transition>, { ...opts, frame: 0 })).toThrow(/kind is one of/);
+ });
+});
diff --git a/sdk/raea/src/index.tsx b/sdk/raea/src/index.tsx
index db72cb39..cd074f44 100644
--- a/sdk/raea/src/index.tsx
+++ b/sdk/raea/src/index.tsx
@@ -56,6 +56,7 @@ export {
} from "@raea/video/registry";
export { Nudge, type NudgeProps } from "@raea/video/nudge";
+export { Transition, type TransitionProps, type TransitionEdge, type TransitionKind } from "@raea/video/transition";
export { Region, useRegion, proofBoxes, regionBand, type RegionName, type RegionBox, type RegionProps, type ProofInput, type ProofPart } from "@raea/video/region";
export { InputRegion } from "@raea/video/input-region";
export { lyria, sao, fish, isGeneratedAsset, type AssetKind, type GeneratedAsset } from "@raea/asset/assets";
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
index 9a4f0306..67db6991 100644
--- a/sdk/timeline/src/read.ts
+++ b/sdk/timeline/src/read.ts
@@ -19,6 +19,9 @@ export interface SeqRow {
keys?: Record<string, [number, number, Ease?][]>
/** how fast the footage it holds plays (its media's playbackRate), when it holds footage at one rate (TL6a) */
speed?: number
+ /** how it comes in and goes out: its <Transition>'s in and out (TL7a) */
+ in?: { kind: string; frames: number }
+ out?: { kind: string; frames: number }
}
/** The host's edit to a sequence: its new span, a split, a delete. */
export type SeqEdit =
@@ -29,7 +32,11 @@ export type SeqEdit =
| { op: "keys"; i: number; keys: Record<string, [number, number, Ease?][]> }
/** the footage it holds played at `speed`, its length following (TL6a) */
| { op: "speed"; i: number; speed: number }
+ /** how it comes in ("in") or goes out ("out"): its <Transition>'s edge set, or taken off with 0 frames (TL7a) */
+ | { op: "edge"; i: number; side: "in" | "out"; kind: string; frames: number }
+/** The transitions a composition's <Transition> draws (sdk/video transition.tsx). */
+export const CODE_KINDS = ["fade", "slide", "wipe", "zoom"]
/** The properties a sequence's <Nudge> can key (sdk/video nudge.tsx). */
export const KEYED = ["x", "y", "scale", "rotate", "opacity"]
@@ -45,8 +52,18 @@ export function fromSequences(rows: SeqRow[], fps: number): Timeline {
const first = lanes.length ? clipsOf(lanes[0]) : []
const gapless = first.length > 0 && first.every((c, n) => c.start === (n ? end(first[n - 1]) : 0))
const tracks: Track[] = lanes.map((lane, n) => ({ id: n === 0 && gapless ? "main" : `overlay-${lane}`, kind: n === 0 && gapless ? "main" : "overlay", clips: clipsOf(lane) }))
+ // a transition is on a cut: the part before it goes out and the part after it comes in, the same way
+ const by = new Map(rows.map((r) => [seqId(r.i), r]))
+ const transitions = tracks.flatMap((k) =>
+ k.clips.flatMap((c) => {
+ const out = by.get(c.id)?.out
+ const next = k.clips.find((o) => o.start === end(c))
+ const into = next && by.get(next.id)?.in
+ return out && into && out.kind === into.kind ? [{ id: `x-${c.id}`, kind: out.kind, after: c.id, length: out.frames + into.frames }] : []
+ }),
+ )
// lanes below the first are nested deeper: drawn under it, as the strip has them
- return { ...empty(fps), tracks, magnet: gapless }
+ return { ...empty(fps), tracks, transitions, magnet: gapless }
}
/** A film with no composition behind it: its scenes as clips of one main track (cut where the picture cuts). Each
@@ -93,6 +110,22 @@ export function seqEdits(t: Timeline, op: Op): SeqEdit[] | { why: string } {
const r = each(op.ids, (x) => ({ op: "delete", i: x.i }))
return Array.isArray(r) ? r.sort((a, b) => b.i - a.i) : r
}
+ case "transition":
+ case "untransition": {
+ const x = op.op === "transition" ? op.transition : t.transitions.find((o) => o.id === op.id)
+ if (!x) return { why: "there is no such transition" }
+ if (op.op === "transition" && !CODE_KINDS.includes(x.kind)) return { why: `a composition draws ${CODE_KINDS.join(", ")}: "${x.kind}" isn't one its <Transition> has` }
+ const a = row(x.after), at = find(t, x.after)
+ const b = at ? at.track.clips.find((c) => c.start === end(at.clip)) : undefined
+ const nb = b && row(b.id)
+ if (!a || !nb) return { why: "a transition sits on a cut between two of the composition's sequences" }
+ // half on each side of the cut, no longer than either part
+ const len = op.op === "transition" ? Math.max(1, Math.min(Math.round(x.length), a.clip.length * 2, nb.clip.length * 2)) : 0
+ return [
+ { op: "edge", i: a.i, side: "out", kind: x.kind, frames: Math.ceil(len / 2) },
+ { op: "edge", i: nb.i, side: "in", kind: x.kind, frames: Math.floor(len / 2) },
+ ]
+ }
case "set": {
// a speed alone is the footage's rate; anything else a sequence has is its own code's
const only = Object.keys(op.set).filter((k) => op.set[k as keyof typeof op.set] !== undefined)
diff --git a/sdk/timeline/src/registry.ts b/sdk/timeline/src/registry.ts
index 9ad9c2ab..728e526c 100644
--- a/sdk/timeline/src/registry.ts
+++ b/sdk/timeline/src/registry.ts
@@ -18,12 +18,13 @@ export interface Adds {
transitions?: TransitionKind[]
}
-/** raea's own transitions (TL7a). */
+/** raea's own transitions (TL7a): the kinds its <Transition> draws (sdk/video transition.tsx). Two parts that only
+ * touch have no frames of each other to mix, so there is no dissolve: a fade goes through black. */
export const TRANSITIONS: TransitionKind[] = [
- { kind: "dissolve", title: "Dissolve" },
- { kind: "fade", title: "Fade through black" },
- { kind: "wipe", title: "Wipe" },
+ { kind: "fade", title: "Fade" },
{ kind: "slide", title: "Slide" },
+ { kind: "wipe", title: "Wipe" },
+ { kind: "zoom", title: "Zoom" },
]
const isEase = (e: unknown): e is Ease => e === "linear" || e === "hold" || (Array.isArray(e) && e.length === 4 && e.every((n) => typeof n === "number" && Number.isFinite(n)) && e[0] >= 0 && e[0] <= 1 && e[2] >= 0 && e[2] <= 1)
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
index 1dc3268b..37214aab 100644
--- a/sdk/timeline/test/read.test.ts
+++ b/sdk/timeline/test/read.test.ts
@@ -43,6 +43,23 @@ describe("TL0c: a timeline is read from a composition", () => {
expect(fromSequences([{ ...rows[0], speed: 2 }], 30).tracks[0].clips[0].speed).toBe(2)
})
+ test("a transition on a cut is the part before going out and the part after coming in, and is read back from them", () => {
+ const t = fromSequences(rows, 30)
+ expect(seqEdits(t, { op: "transition", transition: { id: "x-seq-0", kind: "slide", after: "seq-0", length: 15 } })).toEqual([
+ { op: "edge", i: 0, side: "out", kind: "slide", frames: 8 },
+ { op: "edge", i: 3, side: "in", kind: "slide", frames: 7 },
+ ])
+ const cut = fromSequences([{ ...rows[0], out: { kind: "slide", frames: 8 } }, { ...rows[1], in: { kind: "slide", frames: 7 } }], 30)
+ expect(cut.transitions).toEqual([{ id: "x-seq-0", kind: "slide", after: "seq-0", length: 15 }])
+ expect(seqEdits(cut, { op: "untransition", id: "x-seq-0" })).toEqual([
+ { op: "edge", i: 0, side: "out", kind: "slide", frames: 0 },
+ { op: "edge", i: 3, side: "in", kind: "slide", frames: 0 },
+ ])
+ // a kind the composition's <Transition> doesn't draw, and a part with nothing after it
+ expect(seqEdits(t, { op: "transition", transition: { id: "x", kind: "Glitch", after: "seq-0", length: 15 } })).toEqual({ why: 'a composition draws fade, slide, wipe, zoom: "Glitch" isn\'t one its <Transition> has' })
+ expect(seqEdits(t, { op: "transition", transition: { id: "x", kind: "fade", after: "seq-3", length: 15 } })).toEqual({ why: "a transition sits on a cut between two of the composition's sequences" })
+ })
+
test("a key on a sequence is its <Nudge>'s keys, written whole; what the wrapper can't key is refused", () => {
const keyed = fromSequences([{ ...rows[0], keys: { scale: [[0, 1], [45, 1.2, [0.2, 0, 0, 1]]] } }, rows[1]], 30)
expect(keyed.tracks[0].clips[0].keys).toEqual({ scale: [{ at: 0, value: 1 }, { at: 45, value: 1.2, ease: [0.2, 0, 0, 1] }] })
diff --git a/sdk/timeline/test/registry.test.ts b/sdk/timeline/test/registry.test.ts
index 448a09ec..1622b3d7 100644
--- a/sdk/timeline/test/registry.test.ts
+++ b/sdk/timeline/test/registry.test.ts
@@ -5,7 +5,7 @@ describe("TL11a: a plugin adds easings, speed curves and transitions", () => {
const raw = {
eases: { snap: [0.9, 0, 0.1, 1], back: [0.3, 1.6, 0.6, 1], linear: "hold", bad: [2, 0, 0, 1] },
curves: { pulse: [{ at: 0, speed: 1 }, { at: 0.5, speed: 4 }, { at: 1, speed: 1 }], hero: [{ at: 0, speed: 1 }, { at: 1, speed: 1 }], wild: [{ at: 0, speed: 50 }, { at: 1, speed: 1 }] },
- transitions: [{ kind: "Glitch", title: "Glitch" }, { kind: "dissolve", title: "Mine" }, { kind: "", title: "x" }],
+ transitions: [{ kind: "Glitch", title: "Glitch" }, { kind: "fade", title: "Mine" }, { kind: "", title: "x" }],
}
test("what is well formed is offered after raea's own; the rest is left out with why", () => {
@@ -16,7 +16,7 @@ describe("TL11a: a plugin adds easings, speed curves and transitions", () => {
'timeline.eases.bad is "linear", "hold" or a bezier\'s handles [x1, y1, x2, y2] with x1 and x2 from 0 to 1',
"timeline.curves.hero: raea has a speed curve of that name",
"timeline.curves.wild is two or more points { at, speed }, at from 0 to 1 and speed from 0.1 to 10",
- 'timeline.transitions[1]: raea has a transition "dissolve"',
+ 'timeline.transitions[1]: raea has a transition "fade"',
"timeline.transitions[2] is { kind, title }: kind a name, title what the person sees",
])
const all = palette([adds, { eases: { snap: "hold" } }])
diff --git a/sdk/video/package.json b/sdk/video/package.json
index 58ad11e0..ae43ab46 100644
--- a/sdk/video/package.json
+++ b/sdk/video/package.json
@@ -12,6 +12,7 @@
"./timeline": "./src/timeline.tsx",
"./registry": "./src/registry.tsx",
"./nudge": "./src/nudge.tsx",
+ "./transition": "./src/transition.tsx",
"./words": "./src/words.tsx",
"./region": "./src/region.tsx",
"./overlay": "./src/overlay.tsx",
diff --git a/sdk/video/src/index.ts b/sdk/video/src/index.ts
index af72020e..a158ca6c 100644
--- a/sdk/video/src/index.ts
+++ b/sdk/video/src/index.ts
@@ -3,6 +3,7 @@ export * from "./media";
export * from "./timeline";
export * from "./registry";
export * from "./nudge";
+export * from "./transition";
export * from "./words";
export * from "./region";
export * from "./overlay";
diff --git a/sdk/video/src/transition.tsx b/sdk/video/src/transition.tsx
new file mode 100644
index 00000000..d5258aac
--- /dev/null
+++ b/sdk/video/src/transition.tsx
@@ -0,0 +1,65 @@
+// Transition: how a part comes in and goes out (docs/timeline.feature TL7a). The timeline writes a transition on a
+// cut as one <Transition> around what each of the two parts holds: `out` on the part before the cut, `in` on the
+// part after it. It plays inside the part's own time (its <Sequence>'s first frames for `in`, its last for `out`),
+// so nothing outside the two parts changes and the film is as long as it was.
+import type { CSSProperties, ReactNode } from "react";
+import { useLocalTime } from "@raea/app/clock";
+import { AbsoluteFill } from "./timeline";
+
+/** fade: through black. slide: in from the right, out to the left. wipe: uncovered from the left, covered from the
+ * left. zoom: grows in from 85%, shrinks out to 85%, fading. A dissolve between two parts that only touch has no
+ * frames of the other to mix with, so it is written as a fade. */
+export type TransitionKind = "fade" | "slide" | "wipe" | "zoom";
+export const TRANSITION_KINDS: TransitionKind[] = ["fade", "slide", "wipe", "zoom"];
+
+export interface TransitionEdge {
+ /** which transition: fade, slide, wipe or zoom */
+ kind: TransitionKind;
+ /** how many of the part's frames it takes */
+ frames: number;
+}
+
+export interface TransitionProps {
+ /** over the part's first frames */
+ in?: TransitionEdge;
+ /** over the part's last frames */
+ out?: TransitionEdge;
+ /** the part it brings in and takes out: what the <Sequence> holds */
+ children?: ReactNode;
+}
+
+/** How far a part is into view: 0 not at all, 1 fully. `frame` is the part's own, `duration` its length. */
+export function transitionAt(edge: TransitionEdge | undefined, side: "in" | "out", frame: number, duration: number): number {
+ if (!edge || !(edge.frames > 0)) return 1;
+ const p = side === "in" ? (frame + 1) / edge.frames : (duration - frame) / edge.frames;
+ return Math.min(1, Math.max(0, p));
+}
+
+/** The style a kind gives at `p` (0 out of view, 1 in view), on the side it plays. */
+export function transitionStyle(kind: TransitionKind, side: "in" | "out", p: number): CSSProperties {
+ if (p >= 1) return {};
+ // eased out, so it settles
+ const e = 1 - (1 - p) * (1 - p);
+ if (kind === "slide") return { transform: `translateX(${(side === "in" ? 1 - e : e - 1) * 100}%)` };
+ if (kind === "wipe") return { clipPath: side === "in" ? `inset(0 ${(1 - e) * 100}% 0 0)` : `inset(0 0 0 ${(1 - e) * 100}%)` };
+ if (kind === "zoom") return { transform: `scale(${0.85 + 0.15 * e})`, opacity: e };
+ return { opacity: p };
+}
+
+/** Its children, coming in over the part's first frames and going out over its last. */
+export function Transition({ in: enter, out: exit, children }: TransitionProps) {
+ const t = useLocalTime("Transition");
+ for (const [side, edge] of [["in", enter], ["out", exit]] as const) {
+ if (!edge) continue;
+ if (!TRANSITION_KINDS.includes(edge.kind)) throw new RangeError(`Transition: ${side}.kind is one of ${TRANSITION_KINDS.join(", ")}, got ${String(edge.kind)}`);
+ if (!Number.isFinite(edge.frames) || edge.frames < 0) throw new RangeError(`Transition: ${side}.frames must be 0 or more, got ${edge.frames}`);
+ }
+ const a = enter ? transitionStyle(enter.kind, "in", transitionAt(enter, "in", t.frame, t.duration)) : {};
+ const b = exit ? transitionStyle(exit.kind, "out", transitionAt(exit, "out", t.frame, t.duration)) : {};
+ // both at once (a part shorter than its two transitions): the moves side by side, the fades multiplied
+ const transform = [a.transform, b.transform].filter(Boolean).join(" ") || undefined;
+ const opacity = a.opacity === undefined && b.opacity === undefined ? undefined : Number(a.opacity ?? 1) * Number(b.opacity ?? 1);
+ const clipPath = a.clipPath ?? b.clipPath;
+ if (!transform && opacity === undefined && !clipPath) return <>{children}</>;
+ return <AbsoluteFill style={{ ...(transform ? { transform } : {}), ...(opacity !== undefined ? { opacity } : {}), ...(clipPath ? { clipPath } : {}) }}>{children}</AbsoluteFill>;
+}