Reintent

@reintent/scene/timeline

Time in a scene: one clock, and a timeline that says when things happen, what the scene's moments are called, and what a person does to it.

Beats are named times, in seconds, each with a length when it takes one (the door opens at 9.5, over 6.5; "the door slower" is beat.door.over). Phases are the states of the scene's machine, named: what a still is in. Inputs are what a person does, and the numbers that shape it (how long a blink has to last to count). The timeline is declared once, beside the scene's (state, t) function, and read through useTimeline(), which lays overrides over it (keys beat.door with at and over, and input.hold with its params): an inspector or a sentence can retime a scene without its code changing, the way params reshape a thing.

Classes

Clock

Defined in: packages/scene/src/clock.ts

A scene's time, in seconds: running on animation frames, or held at a moment. <Stage> makes one; useTime() and useFrame() read it.

Example

import { Clock } from '@reintent/scene/timeline';

const clock = new Clock();
clock.freeze(9.5);
console.log(clock.now(), clock.isFrozen());
// → 9.5 true

Constructors

Constructor
new Clock(): Clock;
Returns

Clock

Methods

freeze()
freeze(t?): void;

Defined in: packages/scene/src/clock.ts

Hold time at t (or where it is).

Parameters
ParameterType
tnumber
Returns

void

isFrozen()
isFrozen(): boolean;

Defined in: packages/scene/src/clock.ts

Is time held?

Returns

boolean

now()
now(): number;

Defined in: packages/scene/src/clock.ts

The time now, in seconds.

Returns

number

onFrame()
onFrame(fn): () => void;

Defined in: packages/scene/src/clock.ts

For side effects (useFrame): called every frame, before renders.

Parameters
ParameterType
fnListener
Returns

() => void

resume()
resume(): void;

Defined in: packages/scene/src/clock.ts

Let time run again from where it is held.

Returns

void

start()
start(): () => void;

Defined in: packages/scene/src/clock.ts

Run on animation frames; the function returned stops it.

Returns

() => void

subscribe()
subscribe(fn): () => void;

Defined in: packages/scene/src/clock.ts

For render subscribers (useTime): called every frame, and when time is held.

Parameters
ParameterType
fn() => void
Returns

() => void

Interfaces

BeatSpec

Defined in: packages/scene/src/timeline.ts

A named time.

Properties

PropertyTypeDescription
aka?readonly string[]-
atnumberWhen, in seconds.
note?stringOne line: what happens then.
over?numberHow long it takes, when it takes time.

InputSpec

Defined in: packages/scene/src/timeline.ts

Something a person does, and the numbers that shape it.

Type Parameters

Type ParameterDefault type
P extends ParamsParams

Properties

PropertyTypeDescription
aka?readonly string[]-
does?stringOne line: what it does.
params?P-

PhaseSpec

Defined in: packages/scene/src/timeline.ts

A named state of the scene.

Properties

PropertyType
aka?readonly string[]
label?string

TimelineSpec

Defined in: packages/scene/src/timeline.ts

A scene's beats, phases and inputs.

Properties

PropertyType
beatsReadonly<Record<string, BeatSpec>>
inputs?Readonly<Record<string, InputSpec<Params>>>
phasesReadonly<Record<string, PhaseSpec>>

TimelineValues

Defined in: packages/scene/src/timeline.ts

The timeline's values: each beat's timing and each input's params, with overrides laid over them.

Type Parameters

Type ParameterDefault type
T extends TimelineSpecTimelineSpec

Properties

PropertyType
beat{ [K in string | number | symbol]: Timing }
inputInputValues<NonNullable<T["inputs"]>>

Timing

Defined in: packages/scene/src/timeline.ts

A beat's time, with its overrides.

Properties

PropertyTypeDescription
atnumber-
endnumberat + over
overnumber-

Functions

beat()

function beat(at, o?): BeatSpec;

Defined in: packages/scene/src/timeline.ts

A beat at at seconds.

Parameters

ParameterType
atnumber
oOmit<BeatSpec, "at">

Returns

BeatSpec


resolveTimeline()

function resolveTimeline<T>(spec, overrides): TimelineValues<T>;

Defined in: packages/scene/src/timeline.ts

The timeline's values, with overrides (by key: beat.door, input.hold) laid over the declared ones.

Type Parameters

Type Parameter
T extends TimelineSpec

Parameters

ParameterType
specT
overrides(key) => Record<string, unknown> | undefined

Returns

TimelineValues<T>


timeline()

function timeline<T>(spec): T;

Defined in: packages/scene/src/timeline.ts

Declare the scene's timeline. Returns it, typed, for <Stage timeline> and useTimeline().

Type Parameters

Type Parameter
T extends TimelineSpec

Parameters

ParameterType
specT

Returns

T

Example

import { beat, resolveTimeline, timeline } from '@reintent/scene/timeline';

const tl = timeline({
  beats: { door: beat(9.5, { over: 6.5 }) },
  phases: { asleep: {}, awake: {} },
});
const values = resolveTimeline(tl, (key) =>
  key === 'beat.door' ? { over: 3 } : undefined,
);
console.log(values.beat.door);
// → { at: 9.5, over: 3, end: 12.5 }

useFrame()

function useFrame(fn): void;

Defined in: packages/scene/src/timeline.ts

Run a side effect every frame (sound, haptics), with the scene's time. Never draw from here.

Parameters

ParameterType
fn(t, dt) => void

Returns

void


useNow()

function useNow(): () => number;

Defined in: packages/scene/src/timeline.ts

The clock's reading, without drawing again every frame (for event handlers).

Returns

() => number


useTime()

function useTime(): number;

Defined in: packages/scene/src/timeline.ts

The scene's time, in seconds. The component draws again every frame.

Returns

number


useTimeline()

function useTimeline<T>(spec?): TimelineValues<T>;

Defined in: packages/scene/src/timeline.ts

The stage's timeline, with overrides laid over it. Pass the declaration (the one given to <Stage timeline>) for its types.

Type Parameters

Type Parameter
T extends TimelineSpec

Parameters

ParameterType
spec?T

Returns

TimelineValues<T>

Last updated on

Edit on GitHub