diff --git a/sdk/app/src/clock.tsx b/sdk/app/src/clock.tsx index c4c19caf..243158a1 100644 --- a/sdk/app/src/clock.tsx +++ b/sdk/app/src/clock.tsx @@ -87,6 +87,8 @@ export interface LocalTime { width: number; /** in px, what the Sequence gives its children */ height: number; + /** frames the subtree lasts: the composition's at the root, a Sequence's (cut short by its parent's) inside one */ + duration: number; } export const ClockContext = createContext(null); @@ -198,8 +200,8 @@ export function useLocalTime(caller: string): LocalTime { useClockTick(local ? null : clock); if (local) return local; let t = rootTimes.get(clock); - if (!t || t.frame !== clock.frame || t.width !== clock.config.width || t.height !== clock.config.height) { - t = { frame: clock.frame, origin: 0, width: clock.config.width, height: clock.config.height }; + if (!t || t.frame !== clock.frame || t.width !== clock.config.width || t.height !== clock.config.height || t.duration !== clock.config.durationInFrames) { + t = { frame: clock.frame, origin: 0, width: clock.config.width, height: clock.config.height, duration: clock.config.durationInFrames }; rootTimes.set(clock, t); } return t; @@ -210,12 +212,13 @@ export function useCurrentFrame(): number { return useLocalTime("useCurrentFrame").frame; } -/** The settings of the composition being rendered (a Sequence's width and height win inside it). */ +/** The settings of the composition being rendered (a Sequence's width, height and length win inside it). */ export function useVideoConfig(): VideoConfig { const clock = useClockState("useVideoConfig"); const local = useContext(LocalTimeContext); - if (!local || (local.width === clock.config.width && local.height === clock.config.height)) return clock.config; - return { ...clock.config, width: local.width, height: local.height }; + const c = clock.config; + if (!local || (local.width === c.width && local.height === c.height && local.duration === c.durationInFrames)) return c; + return { ...c, width: local.width, height: local.height, durationInFrames: local.duration }; } export interface ClockProps { @@ -303,7 +306,7 @@ export function Clock(props: ClockProps) { if (!LITE) return {props.children}; return ( - {props.children} + {props.children} ); } diff --git a/sdk/raea/conformance/timeline.test.tsx b/sdk/raea/conformance/timeline.test.tsx index 6de06f81..c068d15d 100644 --- a/sdk/raea/conformance/timeline.test.tsx +++ b/sdk/raea/conformance/timeline.test.tsx @@ -99,6 +99,17 @@ describe("useVideoConfig and AbsoluteFill", () => { at(0, ); expect(cfg).toMatchObject({ width: 540, height: 960 }); }); + test("inside a Sequence, durationInFrames is the Sequence's", () => { + const d: Record = {}; + function Read({ id }: { id: string }) { d[id] = useVideoConfig().durationInFrames; return null; } + at(15, ); + at(15, ); + at(275, ); + at(45, ); + at(5, ); + at(5, ); + expect(d).toEqual({ own: 20, open: 290, cut: 50, nested: 35, loop: 12, freeze: 300 }); + }); test("AbsoluteFill renders its children", () => { at(3, ); expect(seen.a).toBe(3); diff --git a/sdk/raea/spec/motion.feature b/sdk/raea/spec/motion.feature index 53a4f24b..39572af3 100644 --- a/sdk/raea/spec/motion.feature +++ b/sdk/raea/spec/motion.feature @@ -21,7 +21,9 @@ Feature: raea, the behaviour of its API And defaultProps are JSON-like values handed to the component as its props Scenario: What a component can ask Then `useVideoConfig()` returns {id, width, height, fps, durationInFrames, defaultProps} of the composition it is - rendered in (a Sequence given width and height reports those to its children instead) + rendered in (a Sequence given width and height reports those to its children instead, and inside a + Sequence durationInFrames is the Sequence's: its own, cut short where its parent's ends; a Loop's children + get one pass) And `useCurrentFrame()` returns the frame being rendered, measured from the start of the nearest enclosing Sequence (the composition's frame outside any Sequence) Scenario: AbsoluteFill diff --git a/sdk/video/src/timeline.tsx b/sdk/video/src/timeline.tsx index d61b3330..d53ddcc4 100644 --- a/sdk/video/src/timeline.tsx +++ b/sdk/video/src/timeline.tsx @@ -115,11 +115,11 @@ function StopsAt(props: { end: number; children?: ReactNode }) { } /** The same LocalTime object as last render when nothing in it changed (a Freeze's subtree, a held frame). */ -function useStableTime(frame: number, origin: number, width: number, height: number): LocalTime { +function useStableTime(frame: number, origin: number, width: number, height: number, duration: number): LocalTime { const r = useRef(null); const p = r.current; - if (p && p.frame === frame && p.origin === origin && p.width === width && p.height === height) return p; - return (r.current = { frame, origin, width, height }); + if (p && p.frame === frame && p.origin === origin && p.width === width && p.height === height && p.duration === duration) return p; + return (r.current = { frame, origin, width, height, duration }); } /** Shifts time by `from` and shows its children only for `durationInFrames` frames. */ @@ -149,7 +149,9 @@ export function Sequence(props: SequenceProps) { const p = r.current; const w = width ?? parent.width; const h = height ?? parent.height; - const time: LocalTime = p && p.frame === f - from && p.origin === parent.origin + from && p.width === w && p.height === h ? p : (r.current = { frame: f - from, origin: parent.origin + from, width: w, height: h }); + // its own length, cut short where the parent's ends (unbounded: what's left of the parent's) + const d = Math.min(durationInFrames, parent.duration - from); + const time: LocalTime = p && p.frame === f - from && p.origin === parent.origin + from && p.width === w && p.height === h && p.duration === d ? p : (r.current = { frame: f - from, origin: parent.origin + from, width: w, height: h, duration: d }); const base = timeBase(pbase.origin + from, pbase.freeze === null ? null : pbase.freeze - from); // no box: the new time around the children here, as Shifted would put it (a game's sound cues, a component less each) if (layout === "none" && style === undefined && className === undefined) { @@ -259,7 +261,7 @@ export function Loop(props: LoopProps) { const lf = f - pass * durationInFrames; const lo = parent.origin + pass * durationInFrames; const p = r.current; - const time: LocalTime = p && p.frame === lf && p.origin === lo && p.width === parent.width && p.height === parent.height ? p : (r.current = { frame: lf, origin: lo, width: parent.width, height: parent.height }); + const time: LocalTime = p && p.frame === lf && p.origin === lo && p.width === parent.width && p.height === parent.height && p.duration === durationInFrames ? p : (r.current = { frame: lf, origin: lo, width: parent.width, height: parent.height, duration: durationInFrames }); const base = timeBase(pbase.origin + pass * durationInFrames, pbase.freeze === null ? null : pbase.freeze - pass * durationInFrames); return ( @@ -280,7 +282,7 @@ export function Freeze(props: FreezeProps) { checkFinite("Freeze", "frame", props.frame); const parent = useLocalTime("Freeze"); const pbase = useContext(TimeBaseContext); - const time = useStableTime(props.frame, parent.origin, parent.width, parent.height); + const time = useStableTime(props.frame, parent.origin, parent.width, parent.height, parent.duration); return ( {props.children}