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.

TL3h: a composition's sequence mirrored and cropped: <Nudge mirror crop>, and the timeline's writer for a sequence's still placement (its <Nudge>'s props as they now stand)

shinyobjectz1918b36f9ea0
diff --git a/docs/manual/reference/sdk/raea.md b/docs/manual/reference/sdk/raea.md
index b12fd2a2..5feba702 100644
--- a/docs/manual/reference/sdk/raea.md
+++ b/docs/manual/reference/sdk/raea.md
@@ -188,6 +188,8 @@ component, whose box it can't reach, is placed inside a full-frame layer.
| `scale` | `number` | how much bigger (2) or smaller (0.5), about its centre |
| `rotate` | `number` | a turn, in degrees, about its centre |
| `opacity` | `number` | 0 (gone) to 1 (as it was), times its own opacity |
+| `mirror` | `boolean` | flipped left to right |
+| `crop` | `[left: number, top: number, right: number, bottom: number]` | how much is cut off its left, top, right and bottom, each a share of that side from 0 to 0.9 |
| `keys` | `Partial<Record<NudgeProp, NudgeKey[]>>` | 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. |
| `children` | `ReactNode` | the part it places: an element, or a component, which moves inside a full-frame layer |
diff --git a/docs/timeline.feature b/docs/timeline.feature
index 973450c1..d08fdbc2 100644
--- a/docs/timeline.feature
+++ b/docs/timeline.feature
@@ -90,7 +90,8 @@ Feature: The timeline SDK (0.1.5): CapCut's timeline, on the composition, in the
said, its file in the project's attachments), TL11a for easings, speed curves, transitions and effects as data,
TL12a-b, TL14a-c
in code a composition's sequences: move, trim, split, delete (E3), keys on x, y, scale, rotate and opacity
- (TL5d), a speed or a speed curve for a sequence that holds footage (TL6a-b), a transition on a cut
+ (TL5d), its still placement as its <Nudge>'s props, mirror and crop among them (TL3h), a speed or a
+ speed curve for a sequence that holds footage (TL6a-b), a transition on a cut
(TL7a), an effect as a sequence holding an <Effect>, its kind and amount (TL7b), a sequence of
words as a text clip, its words rewritten, words added as a new sequence (TL7c), a <Video>'s or an
<Audio>'s level (TL8a), a recording as a sequence holding an <Audio> of its file (TL8c); each
@@ -110,8 +111,7 @@ Feature: The timeline SDK (0.1.5): CapCut's timeline, on the composition, in the
decided TL10b, c and e in the SDK (which ranges an edit changes, a range's key, what goes first); a
composition's drafts are kept by their source, the least recently played going first under 1 GiB
(RAEA_DRAFTS_LIMIT), so an undo plays its draft again at once
- not built TL1d, TL3f, mirror and crop on a composition's sequence (its <Nudge> has no such props yet: the
- edit is refused with why), a split of a sequence under a speed curve, effects and text a plugin
+ not built TL1d, TL3f, a split of a sequence under a speed curve, effects and text a plugin
adds, a caption split with Enter, TL9d, TL11a's tools and snapping targets, TL13's measurements. The skills guide's note
on an item's `timeline` field waits for PR #19 to merge
diff --git a/sdk/raea/conformance/nudge-keys.test.tsx b/sdk/raea/conformance/nudge-keys.test.tsx
index 20df9d16..cff36078 100644
--- a/sdk/raea/conformance/nudge-keys.test.tsx
+++ b/sdk/raea/conformance/nudge-keys.test.tsx
@@ -37,3 +37,25 @@ describe("a keyed property's value at a frame", () => {
expect(renderFrame(<Nudge scale={3}><div>hi</div></Nudge>, { ...opts, frame: 0 })).toContain("scale(3)");
});
});
+
+describe("a placed part mirrored and cropped (docs/timeline.feature TL3h)", () => {
+ const at = (el: JSX.Element) => renderFrame(el, { ...opts, frame: 0 });
+
+ test("mirror flips it left to right, with its scale", () => {
+ expect(at(<Nudge mirror><div>hi</div></Nudge>)).toContain("transform:scale(-1, 1)");
+ expect(at(<Nudge mirror scale={2} x={10}><div>hi</div></Nudge>)).toContain("transform:translate(10px, 0px) scale(-2, 2)");
+ });
+
+ test("crop cuts each side off by its share, on an element and on a component's layer", () => {
+ expect(at(<Nudge crop={[0.1, 0, 0, 0.25]}><div>hi</div></Nudge>)).toContain("clip-path:inset(0% 0% 25% 10%)");
+ const Part = () => <p>part</p>;
+ expect(at(<Nudge crop={[0, 0.5, 0, 0]}><Part /></Nudge>)).toContain("clip-path:inset(50% 0% 0% 0%)");
+ // no crop, no clip
+ expect(at(<Nudge crop={[0, 0, 0, 0]}><div>hi</div></Nudge>)).not.toContain("clip-path");
+ });
+
+ test("a crop out of range fails", () => {
+ expect(() => at(<Nudge crop={[1, 0, 0, 0]}><div>hi</div></Nudge>)).toThrow(/crop/);
+ expect(() => at(<Nudge crop={[0, 0, 0] as unknown as [number, number, number, number]}><div>hi</div></Nudge>)).toThrow(/crop/);
+ });
+});
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
index d8a96dc5..75134c2f 100644
--- a/sdk/timeline/src/read.ts
+++ b/sdk/timeline/src/read.ts
@@ -23,6 +23,8 @@ export interface SeqRow {
/** its footage's speed as a curve, each [place 0 to 1, speed] (its media's speedCurve), over `shows` source frames (TL6b) */
curve?: [number, number][]
shows?: number
+ /** how it is placed, as its <Nudge> says without keys: moved, scaled, turned, faded, mirrored, cropped (TL3h) */
+ look?: SeqLook
/** the words it shows, when words are all it shows: a text clip, its words changed by renaming it (TL7c) */
text?: string
/** the effect it puts on the picture under it, when an <Effect> is all it holds: an effect clip (TL7b) */
@@ -36,6 +38,17 @@ export interface SeqRow {
startFrom?: number
volume?: number
}
+/** A sequence's still placement: its <Nudge>'s props (sdk/video nudge.tsx). `crop` is left, top, right, bottom. */
+export interface SeqLook {
+ x?: number
+ y?: number
+ scale?: number
+ rotate?: number
+ opacity?: number
+ mirror?: boolean
+ crop?: [number, number, number, number]
+}
+const CROPS = ["cropLeft", "cropTop", "cropRight", "cropBottom"]
/** 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 }
@@ -48,6 +61,8 @@ export type SeqEdit =
/** the footage it holds played by a speed curve over `source` frames of it, the sequence `frames` long (what the
* curve takes); null takes the curve off (TL6b) */
| { op: "curve"; i: number; curve: [number, number][] | null; source: number; frames: number }
+ /** its still placement as it now stands: written as its <Nudge>'s props, a prop at its plain value taken off (TL3h) */
+ | { op: "nudge"; i: number; look: SeqLook }
/** a text sequence's words (TL7c) */
| { op: "words"; i: number; text: string }
/** words put on the picture from `at` for `frames`: a new sequence after the composition's last (TL7c) */
@@ -69,6 +84,12 @@ export const CODE_KINDS = ["fade", "slide", "wipe", "zoom"]
export const KEYED = ["x", "y", "scale", "rotate", "opacity"]
const seqId = (i: number) => `seq-${i}`
+// a sequence's placement as its clip's props (the crop a prop a side, as a film's clip has it)
+const lookProps = (l: SeqLook): Record<string, number | boolean> => ({
+ ...Object.fromEntries(KEYED.flatMap((k) => (typeof l[k as keyof SeqLook] === "number" ? [[k, l[k as keyof SeqLook] as number]] : []))),
+ ...(l.mirror && { mirror: true }),
+ ...(l.crop && Object.fromEntries(CROPS.map((k, n) => [k, l.crop![n]]))),
+})
// the edits to later elements first (an edit that adds or removes an element moves the places after it)
const later = (a: SeqEdit, b: SeqEdit) => ("i" in b ? b.i : 0) - ("i" in a ? a.i : 0)
const seqOf = (id: string): number | undefined => (/^seq-\d+$/.test(id) ? Number(id.slice(4)) : undefined)
@@ -80,7 +101,7 @@ export function fromSequences(rows: SeqRow[], fps: number): Timeline {
const media = rows.filter((r) => r.media)
rows = rows.filter((r) => !r.media)
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: r.effect ? "effect" : r.text === undefined ? "sequence" : "text", name: r.effect ? (EFFECTS.find((e) => e.kind === r.effect!.kind)?.title ?? r.effect.kind) : (r.text ?? r.label ?? r.name), ...(r.effect && { props: { effect: r.effect.kind, amount: r.effect.amount } }), start: r.from, length: Math.max(1, r.to - r.from), ...(r.locked && { locked: r.locked }), ...(r.speed && r.speed !== 1 && { speed: r.speed }), ...(r.curve?.length && r.shows && { curve: r.curve.map(([at, speed]) => ({ at, speed })), shows: r.shows }), ...(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 clipsOf = (lane: number): Clip[] => rows.filter((r) => r.lane === lane).sort((a, b) => a.from - b.from).map((r) => ({ id: seqId(r.i), kind: r.effect ? "effect" : r.text === undefined ? "sequence" : "text", name: r.effect ? (EFFECTS.find((e) => e.kind === r.effect!.kind)?.title ?? r.effect.kind) : (r.text ?? r.label ?? r.name), ...(r.effect && { props: { effect: r.effect.kind, amount: r.effect.amount } }), ...(r.look && Object.keys(r.look).length && { props: lookProps(r.look) }), start: r.from, length: Math.max(1, r.to - r.from), ...(r.locked && { locked: r.locked }), ...(r.speed && r.speed !== 1 && { speed: r.speed }), ...(r.curve?.length && r.shows && { curve: r.curve.map(([at, speed]) => ({ at, speed })), shows: r.shows }), ...(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) }))
@@ -199,6 +220,19 @@ export function seqEdits(t: Timeline, op: Op): SeqEdit[] | { why: string } {
if (amount !== undefined && !(typeof amount === "number" && amount >= 0 && amount <= 1)) return { why: "an effect's amount runs from 0 to 1" }
return each([op.id], (r) => ({ op: "fx", i: r.i, ...(effect !== undefined && { kind: String(effect) }), ...(amount !== undefined && { amount: Math.round((amount as number) * 1000) / 1000 }) }))
}
+ // how a sequence is placed (moved, scaled, turned, faded, mirrored, cropped): its <Nudge>'s still props
+ if (only.length === 1 && only[0] === "props" && find(t, op.id)?.clip.kind !== "effect") {
+ const bad = Object.keys(op.set.props!).filter((k) => !KEYED.includes(k) && k !== "mirror" && !CROPS.includes(k))
+ if (bad.length) return { why: `a sequence is placed by its ${[...KEYED, "mirror", "crop"].join(", ")}: ${bad.join(", ")} ${bad.length > 1 ? "are" : "is"} its own code's` }
+ const after = apply(t, op)
+ if (!after.ok) return { why: after.why }
+ const p = find(after.timeline, op.id)?.clip.props ?? {}
+ const num = (k: string) => (typeof p[k] === "number" && Number.isFinite(p[k]) ? Math.round((p[k] as number) * 1000) / 1000 : undefined)
+ if ((num("scale") ?? 1) <= 0) return { why: "a scale is above 0" }
+ const crop = CROPS.map((k) => Math.min(0.9, Math.max(0, num(k) ?? 0))) as [number, number, number, number]
+ const look: SeqLook = { ...Object.fromEntries(KEYED.flatMap((k) => (num(k) === undefined ? [] : [[k, num(k)]]))), ...(p.mirror === true && { mirror: true }), ...(crop.some((v) => v > 0) && { crop }) }
+ return each([op.id], (r) => ({ op: "nudge", i: r.i, look }))
+ }
// a text clip's name is its words
if (only.length === 1 && only[0] === "name") {
if (find(t, op.id)?.clip.kind !== "text") return { why: "a sequence's name is its own code's: only a text clip's words change here" }
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
index 892894e9..0cf3c16d 100644
--- a/sdk/timeline/test/read.test.ts
+++ b/sdk/timeline/test/read.test.ts
@@ -64,6 +64,17 @@ describe("TL0c: a timeline is read from a composition", () => {
expect(seqEdits(t, { op: "add", kind: "sound", clip: { id: "voice-1", kind: "audio", name: "Voice 1", props: { file: "/p/attachments/voice-1.wav" }, start: 30, length: 75 } })).toEqual([{ op: "voice", at: 30, frames: 75, file: "/p/attachments/voice-1.wav" }])
})
+ test("a sequence's placement is its <Nudge>'s still props: read as the clip's, written whole as they now stand (TL3h)", () => {
+ const t = fromSequences([{ ...rows[0], look: { scale: 1.2, mirror: true, crop: [0.1, 0, 0, 0.25] } }], 30)
+ expect(t.tracks[0].clips[0].props).toEqual({ scale: 1.2, mirror: true, cropLeft: 0.1, cropTop: 0, cropRight: 0, cropBottom: 0.25 })
+ // one more prop set: the whole placement goes to the host
+ expect(seqEdits(t, { op: "set", id: "seq-0", set: { props: { rotate: 90 } } })).toEqual([{ op: "nudge", i: 0, look: { scale: 1.2, rotate: 90, mirror: true, crop: [0.1, 0, 0, 0.25] } }])
+ // unmirrored and uncropped: those go
+ expect(seqEdits(t, { op: "set", id: "seq-0", set: { props: { mirror: false, cropLeft: 0, cropBottom: 0 } } })).toEqual([{ op: "nudge", i: 0, look: { scale: 1.2 } }])
+ expect(seqEdits(t, { op: "set", id: "seq-0", set: { props: { colour: "red" } } })).toMatchObject({ why: expect.stringContaining("colour is its own code's") })
+ expect(seqEdits(t, { op: "set", id: "seq-0", set: { props: { scale: 0 } } })).toEqual({ why: "a scale is above 0" })
+ })
+
test("a sequence holding an <Effect> is an effect clip: its kind and amount set, and one added is a new sequence (TL7b)", () => {
const t = fromSequences([{ ...rows[0] }, { ...rows[2], effect: { kind: "mono", amount: 0.5 } }], 30)
const fx = t.tracks.flatMap((k) => k.clips).find((c) => c.id === "seq-1")!
diff --git a/sdk/video/src/nudge.tsx b/sdk/video/src/nudge.tsx
index 4520bc18..f8820c3d 100644
--- a/sdk/video/src/nudge.tsx
+++ b/sdk/video/src/nudge.tsx
@@ -1,5 +1,6 @@
// Nudge: a part placed by hand on raea's picture (docs/editing.feature, E1). The editor writes a moved, scaled, turned
-// or faded part back to the composition as one <Nudge x y scale rotate opacity> around the element that holds it; the
+// or faded part back to the composition as one <Nudge x y scale rotate opacity> around the element that holds it (the
+// timeline also writes mirror and crop, docs/timeline.feature TL3h); 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";
@@ -38,6 +39,10 @@ export interface NudgeProps {
rotate?: number;
/** 0 (gone) to 1 (as it was), times its own opacity */
opacity?: number;
+ /** flipped left to right */
+ mirror?: boolean;
+ /** how much is cut off its left, top, right and bottom, each a share of that side from 0 to 0.9 */
+ crop?: [left: number, top: number, right: number, bottom: 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[]>>;
@@ -59,7 +64,7 @@ function Keyed({ keys, children, ...still }: NudgeProps & { keys: NonNullable<Nu
return <Placed {...still} {...now}>{children}</Placed>;
}
-function Placed({ x = 0, y = 0, scale = 1, rotate = 0, opacity = 1, children }: Omit<NudgeProps, "keys">) {
+function Placed({ x = 0, y = 0, scale = 1, rotate = 0, opacity = 1, mirror = false, crop, 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}`);
@@ -67,8 +72,11 @@ function Placed({ x = 0, y = 0, scale = 1, rotate = 0, opacity = 1, children }:
const move = [
(x || y) && `translate(${x}px, ${y}px)`,
rotate && `rotate(${rotate}deg)`,
- scale !== 1 && `scale(${scale})`,
+ (scale !== 1 || mirror) && (mirror ? `scale(${-scale}, ${scale})` : `scale(${scale})`),
].filter(Boolean).join(" ") || undefined;
+ if (crop !== undefined && (!Array.isArray(crop) || crop.length !== 4 || crop.some((v) => !(v >= 0 && v <= 0.9)))) throw new RangeError(`Nudge: crop is [left, top, right, bottom], each from 0 to 0.9, got ${JSON.stringify(crop)}`);
+ // (inset's order is top, right, bottom, left)
+ const cut = crop?.some((v) => v > 0) ? `inset(${crop[1] * 100}% ${crop[2] * 100}% ${crop[3] * 100}% ${crop[0] * 100}%)` : undefined;
if (isValidElement(children) && typeof children.type === "string") {
const style = ((children.props as { style?: CSSProperties }).style ?? {}) as CSSProperties;
const own = typeof style.opacity === "number" ? style.opacity : style.opacity === undefined ? 1 : Number(style.opacity);
@@ -77,8 +85,9 @@ function Placed({ x = 0, y = 0, scale = 1, rotate = 0, opacity = 1, children }:
...style,
...(move ? { transform: style.transform ? `${move} ${style.transform}` : move } : {}),
...(fade !== 1 ? { opacity: own * fade } : {}),
+ ...(cut ? { clipPath: cut } : {}),
},
});
}
- return <AbsoluteFill style={{ ...(move ? { transform: move } : {}), ...(fade !== 1 ? { opacity: fade } : {}) }}>{children}</AbsoluteFill>;
+ return <AbsoluteFill style={{ ...(move ? { transform: move } : {}), ...(fade !== 1 ? { opacity: fade } : {}), ...(cut ? { clipPath: cut } : {}) }}>{children}</AbsoluteFill>;
}