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.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
TL5d: in progress: <Nudge keys> plays a placed part's properties over time, and a key on a sequence is that edit
shinyobjectz7bfa7f392cef
diff --git a/sdk/raea/conformance/nudge-keys.test.tsx b/sdk/raea/conformance/nudge-keys.test.tsxnew file mode 100644index 00000000..20df9d16--- /dev/null+++ b/sdk/raea/conformance/nudge-keys.test.tsx@@ -0,0 +1,39 @@+// <Nudge keys>: a placed part's properties over time, as the timeline writes them (docs/timeline.feature TL5d).+import { describe, expect, test } from "bun:test";+import { keyedAt, Nudge, type NudgeKey } from "@raea/video/nudge";+import { Sequence } from "../src";+import { renderFrame } from "../src/test";++const opts = { fps: 30, width: 1920, height: 1080, durationInFrames: 300 };++describe("a keyed property's value at a frame", () => {+ const scale: NudgeKey[] = [[10, 1], [40, 2]];++ test("holds before its first key and after its last, and travels in a straight line between", () => {+ expect([0, 10, 25, 40, 99].map((f) => keyedAt(scale, f))).toEqual([1, 1, 1.5, 2, 2]);+ });++ test("follows the first key's ease: hold stays then jumps, a bezier is CSS's", () => {+ expect(keyedAt([[10, 1, "hold"], [40, 2]], 39)).toBe(1);+ expect(keyedAt([[10, 1, "hold"], [40, 2]], 40)).toBe(2);+ const eased = keyedAt([[0, 0, [0.42, 0, 0.58, 1]], [100, 1]], 25);+ expect(eased).toBeCloseTo(0.1292, 3);+ // a handle above the graph overshoots+ expect(Math.max(...Array.from({ length: 50 }, (_, i) => keyedAt([[0, 0, [0.3, 1.6, 0.6, 1]], [49, 1]], i)))).toBeGreaterThan(1);+ });++ test("keys are read in time order whatever order they are written in", () => {+ expect(keyedAt([[40, 2], [10, 1]], 25)).toBe(1.5);+ });++ test("in a <Sequence> the keys run from the sequence's first frame, and a keyed property wins over its still value", () => {+ const at = (frame: number) => renderFrame(<Sequence from={50}><Nudge scale={3} keys={{ scale: [[0, 1], [30, 2]], opacity: [[0, 0], [10, 1]] }}><div>hi</div></Nudge></Sequence>, { ...opts, frame });+ expect(at(50)).toContain("opacity:0");+ expect(at(55)).toContain("opacity:0.5");+ expect(at(65)).toContain("scale(1.5)");+ expect(at(80)).toContain("scale(2)");+ expect(at(80)).not.toContain("scale(3)");+ // with no keys it is the still Nudge it was+ expect(renderFrame(<Nudge scale={3}><div>hi</div></Nudge>, { ...opts, frame: 0 })).toContain("scale(3)");+ });+});diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.tsindex e59446a3..c1161a07 100644--- a/sdk/timeline/src/read.ts+++ b/sdk/timeline/src/read.ts@@ -2,8 +2,8 @@// lists them (raea-feed host/sequences.ts), or a film with no composition as its scenes and its sound. And an// operation on a sequences' timeline as the host's own edits to those elements (docs/editing.feature E3b), until// every operation is a code edit of its own (edits.ts).-import { type Clip, empty, end, find, type Frames, type Timeline, type Track } from "./model"-import type { Op } from "./ops"+import { type Clip, type Ease, empty, end, find, type Frames, type Key, type Timeline, type Track } from "./model"+import { apply, type Op } from "./ops"/** A <Sequence> as the host lists it: its place among the file's elements, its span in the composition's frames,* the lane it sits on (nested ones below their parent, overlapping ones apart), and why it can't be rewritten. */@@ -15,9 +15,19 @@ export interface SeqRow {to: numberlane: numberlocked?: string+ /** the keys its <Nudge> holds, each [frame of the sequence, value, ease?] (TL5d) */+ keys?: Record<string, [number, number, Ease?][]>}/** The host's edit to a sequence: its new span, a split, a delete. */-export type SeqEdit = { op: "move"; i: number; from: number; to: number } | { op: "split"; i: number; at: number } | { op: "delete"; i: number }+export type SeqEdit =+ | { op: "move"; i: number; from: number; to: number }+ | { op: "split"; i: number; at: number }+ | { op: "delete"; i: number }+ /** the sequence's keyed properties as they now stand: written as its <Nudge keys>, one a property (TL5d) */+ | { op: "keys"; i: number; keys: Record<string, [number, number, Ease?][]> }++/** The properties a sequence's <Nudge> can key (sdk/video nudge.tsx). */+export const KEYED = ["x", "y", "scale", "rotate", "opacity"]const seqId = (i: number) => `seq-${i}`const seqOf = (id: string): number | undefined => (/^seq-\d+$/.test(id) ? Number(id.slice(4)) : undefined)@@ -27,7 +37,7 @@ const seqOf = (id: string): number | undefined => (/^seq-\d+$/.test(id) ? Number* sequences are where its code puts them, and nothing here moves one that wasn't dragged. */export function fromSequences(rows: SeqRow[], fps: number): Timeline {const lanes = [...new Set(rows.map((r) => r.lane))].sort((a, b) => a - b)- const clipsOf = (lane: number): Clip[] => rows.filter((r) => r.lane === lane).sort((a, b) => a.from - b.from).map((r) => ({ id: seqId(r.i), kind: "sequence", name: r.label ?? r.name, start: r.from, length: Math.max(1, r.to - r.from), ...(r.locked && { locked: r.locked }) }))+ const clipsOf = (lane: number): Clip[] => rows.filter((r) => r.lane === lane).sort((a, b) => a.from - b.from).map((r) => ({ id: seqId(r.i), kind: "sequence", name: r.label ?? r.name, start: r.from, length: Math.max(1, r.to - r.from), ...(r.locked && { locked: r.locked }), ...(r.keys && Object.keys(r.keys).length && { keys: Object.fromEntries(Object.entries(r.keys).map(([p, ks]) => [p, ks.map(([at, value, ease]): Key => ({ at, value, ...(ease !== undefined && { ease }) }))])) }) }))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) }))@@ -80,6 +90,20 @@ 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 "key":+ case "unkey":+ case "moveKey":+ case "ease": {+ if (!KEYED.includes(op.prop)) return { why: `a sequence keys its ${KEYED.join(", ")}: ${op.prop} is its own code's` }+ if (op.op === "key" && typeof op.value !== "number") return { why: `${op.prop} is a number` }+ const r = row(op.id)+ if (!r) return { why: "that isn't one of the composition's sequences" }+ // the keys as the operation leaves them, every property: the wrapper's one prop is written whole+ const after = apply(t, op)+ if (!after.ok) return { why: after.why }+ const keys = find(after.timeline, op.id)?.clip.keys ?? {}+ return [{ op: "keys", i: r.i, keys: Object.fromEntries(Object.entries(keys).filter(([p]) => KEYED.includes(p)).map(([p, ks]) => [p, ks.map((k): [number, number, Ease?] => (k.ease === undefined || k.ease === "linear" ? [k.at, k.value as number] : [k.at, k.value as number, k.ease]))])) }]+ }default:return { why: `the composition's sequences don't take "${op.op}" yet` }}diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.tsindex 8c54b8b1..c5444ba8 100644--- a/sdk/timeline/test/read.test.ts+++ b/sdk/timeline/test/read.test.ts@@ -36,7 +36,16 @@ describe("TL0c: a timeline is read from a composition", () => {// every clip under the playhead that can be changed, the later one firstexpect(seqEdits(t, { op: "split", at: 45 })).toEqual([{ op: "split", i: 1, at: 45 }, { op: "split", i: 0, at: 45 }])expect(seqEdits(t, { op: "remove", ids: ["seq-0", "seq-3"] })).toEqual([{ op: "delete", i: 3 }, { op: "delete", i: 0 }])- expect(seqEdits(t, { op: "key", id: "seq-0", prop: "scale", at: 0, value: 1 })).toEqual({ why: 'the composition\'s sequences don\'t take "key" yet' })+ expect(seqEdits(t, { op: "set", id: "seq-0", set: { speed: 2 } })).toEqual({ why: 'the composition\'s sequences don\'t take "set" yet' })+ })++ 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] }] })+ expect(seqEdits(keyed, { op: "key", id: "seq-0", prop: "opacity", at: 10, value: 0.5 })).toEqual([{ op: "keys", i: 0, keys: { scale: [[0, 1], [45, 1.2, [0.2, 0, 0, 1]]], opacity: [[10, 0.5]] } }])+ expect(seqEdits(keyed, { op: "ease", id: "seq-0", prop: "scale", at: 0, ease: "hold" })).toEqual([{ op: "keys", i: 0, keys: { scale: [[0, 1, "hold"], [45, 1.2, [0.2, 0, 0, 1]]] } }])+ expect(seqEdits(keyed, { op: "unkey", id: "seq-0", prop: "scale", at: 45 })).toEqual([{ op: "keys", i: 0, keys: { scale: [[0, 1]] } }])+ expect(seqEdits(keyed, { op: "key", id: "seq-0", prop: "blur", at: 0, value: 1 })).toEqual({ why: "a sequence keys its x, y, scale, rotate, opacity: blur is its own code's" })})})diff --git a/sdk/video/src/nudge.tsx b/sdk/video/src/nudge.tsxindex 1a5d7700..4520bc18 100644--- a/sdk/video/src/nudge.tsx+++ b/sdk/video/src/nudge.tsx@@ -2,8 +2,31 @@// or faded part back to the composition as one <Nudge x y scale rotate opacity> around the element that holds it; the// next change to the same part re-writes that line.import { cloneElement, isValidElement, type CSSProperties, type ReactElement, type ReactNode } from "react";+import { useCurrentFrame } from "@raea/app/clock";+import { Easing } from "@raea/math/easing";import { AbsoluteFill } from "./timeline";+/** A property's value at a frame, and how it leaves for the next key: a straight line (the default), no travel+ * ("hold": it stays, then jumps), or a cubic bezier's two handles [x1, y1, x2, y2] as CSS writes them. The timeline+ * writes these (docs/timeline.feature TL5d). */+export type NudgeKey = [frame: number, value: number, ease?: "linear" | "hold" | [number, number, number, number]];+export type NudgeProp = "x" | "y" | "scale" | "rotate" | "opacity";++/** A keyed property's value at `frame`: before its first key and after its last it holds; between two it follows+ * the first one's ease. */+export function keyedAt(keys: NudgeKey[], frame: number): number {+ const ks = [...keys].sort((a, b) => a[0] - b[0]);+ if (frame <= ks[0][0]) return ks[0][1];+ const last = ks[ks.length - 1];+ if (frame >= last[0]) return last[1];+ const i = ks.findIndex((k) => k[0] > frame) - 1;+ const [f0, v0, ease] = ks[i];+ const [f1, v1] = ks[i + 1];+ const p = (frame - f0) / (f1 - f0);+ const k = ease === "hold" ? 0 : Array.isArray(ease) ? Easing.bezier(...ease)(p) : p;+ return v0 + (v1 - v0) * k;+}+export interface NudgeProps {/** how far to move it, in the composition's pixels */x?: number;@@ -15,13 +38,28 @@ export interface NudgeProps {rotate?: number;/** 0 (gone) to 1 (as it was), times its own opacity */opacity?: number;+ /** any of the above over time: its keys, each [frame, value, ease?], in frames from where this part's time starts+ * (its <Sequence>'s first frame). A keyed property's still value is ignored. */+ keys?: Partial<Record<NudgeProp, NudgeKey[]>>;/** the part it places: an element, or a component, which moves inside a full-frame layer */children?: ReactNode;}/** Places its child: an element gets the move in front of its own transform and the fade times its own opacity; a* component, whose box it can't reach, is placed inside a full-frame layer. */-export function Nudge({ x = 0, y = 0, scale = 1, rotate = 0, opacity = 1, children }: NudgeProps) {+export function Nudge({ keys, ...still }: NudgeProps) {+ // keyed: the frame is read where the keys are (a hook, so in a component of its own), and the values placed as still ones+ return keys && Object.values(keys).some((k) => k?.length) ? <Keyed keys={keys} {...still} /> : <Placed {...still} />;+}++function Keyed({ keys, children, ...still }: NudgeProps & { keys: NonNullable<NudgeProps["keys"]> }) {+ const frame = useCurrentFrame();+ const now: Partial<Record<NudgeProp, number>> = {};+ for (const [prop, ks] of Object.entries(keys) as [NudgeProp, NudgeKey[] | undefined][]) if (ks?.length) now[prop] = keyedAt(ks, frame);+ return <Placed {...still} {...now}>{children}</Placed>;+}++function Placed({ x = 0, y = 0, scale = 1, rotate = 0, opacity = 1, children }: Omit<NudgeProps, "keys">) {for (const [k, v] of Object.entries({ x, y, scale, rotate, opacity }))if (!Number.isFinite(v)) throw new RangeError(`Nudge: ${k} must be a finite number, got ${v}`);if (scale <= 0) throw new RangeError(`Nudge: scale must be above 0, got ${scale}`);