Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions sdk/app/src/clock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export interface LocalTime {
width: number;
/** in px, what the Sequence gives its children */
height: number;
/** frames the subtree runs for from its frame 0, which useVideoConfig reports as durationInFrames: a Sequence's own
* length cut where its parent's ends, a Loop's one pass, the composition's at the root */
duration: number;
}

export const ClockContext = createContext<ClockState | null>(null);
Expand Down Expand Up @@ -198,8 +201,9 @@ 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 };
const c = clock.config;
if (!t || t.frame !== clock.frame || t.width !== c.width || t.height !== c.height || t.duration !== c.durationInFrames) {
t = { frame: clock.frame, origin: 0, width: c.width, height: c.height, duration: c.durationInFrames };
rootTimes.set(clock, t);
}
return t;
Expand All @@ -210,12 +214,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 (inside a Sequence, its width, height and length win). */
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 {
Expand Down Expand Up @@ -303,7 +308,7 @@ export function Clock(props: ClockProps) {
if (!LITE) return <ClockContext.Provider value={state}>{props.children}</ClockContext.Provider>;
return (
<ClockContext.Provider value={state}>
<LocalTimeContext.Provider value={{ frame, origin: 0, width, height }}>{props.children}</LocalTimeContext.Provider>
<LocalTimeContext.Provider value={{ frame, origin: 0, width, height, duration: durationInFrames }}>{props.children}</LocalTimeContext.Provider>
</ClockContext.Provider>
);
}
Expand Down
11 changes: 11 additions & 0 deletions sdk/raea/conformance/timeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ describe("useVideoConfig and AbsoluteFill", () => {
at(0, <Sequence width={540} height={960}><Read /></Sequence>);
expect(cfg).toMatchObject({ width: 540, height: 960 });
});
test("inside a Sequence, durationInFrames is the Sequence's", () => {
const d: Record<string, number> = {};
function Read({ id }: { id: string }) { d[id] = useVideoConfig().durationInFrames; return null; }
at(15, <Sequence from={10} durationInFrames={20}><Read id="own" /></Sequence>);
at(15, <Sequence from={10}><Read id="open" /></Sequence>);
at(275, <Sequence from={250} durationInFrames={100}><Read id="cut" /></Sequence>);
at(45, <Sequence from={30} durationInFrames={40}><Sequence from={5}><Read id="nested" /></Sequence></Sequence>);
at(5, <Loop durationInFrames={12}><Read id="loop" /></Loop>);
at(5, <Freeze frame={2}><Read id="freeze" /></Freeze>);
expect(d).toEqual({ own: 20, open: 290, cut: 50, nested: 35, loop: 12, freeze: 300 });
});
test("AbsoluteFill renders its children", () => {
at(3, <AbsoluteFill style={{ background: "black" }}><Probe id="a" /></AbsoluteFill>);
expect(seen.a).toBe(3);
Expand Down
4 changes: 3 additions & 1 deletion sdk/raea/spec/motion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions sdk/video/src/timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalTime | null>(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. */
Expand Down Expand Up @@ -149,7 +149,12 @@ 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 length: its own, cut where its 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) {
Expand Down Expand Up @@ -259,7 +264,11 @@ 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 });
// its children's length is one pass
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 (
<Shifted owner="Loop" time={time} base={base} layout={layout} style={style} className={className} label={name} stops={lo + durationInFrames}>
Expand All @@ -280,7 +289,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 (
<TimeBaseContext.Provider value={timeBase(pbase.origin, props.frame)}>
<LocalTimeContext.Provider value={time}>{props.children}</LocalTimeContext.Provider>
Expand Down
Loading