Skip to content

⚠ Early release: v0.3.2.sophistry is not ready for production use. Until 1.0, any minor release may break the API; expect missing features and bugs too.Stability and versioning

A scrubbable timeline

Build a scene whose whole animation can be scrubbed backwards and forwards with a slider, with a play/pause button and chapter markers positioned from the steps’ durations.

Drag the slider, press play, or jump to a chapter. Scrubbing backwards un-draws the circle and fades things out again, because seeking restores earlier steps to their start state.

Scrubbable explainer with chapters

A four-step explainer on one timeline. Chapter buttons sit at offsets computed from each step's duration; the slider and the buttons both just call seek.

ScrubChapters.tsx
19 collapsed lines
import {
derive,
draw,
fadeIn,
moveAlong,
sequence,
track,
trackPoint,
} from "@sophistry/core";
import { circlePolyline } from "@sophistry/core/geometry";
import {
Circle,
Dot,
Stage,
Text,
usePlayer,
useTrackValue,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const ORBIT = circlePolyline(0, 0, 2);
export default function ScrubChapters() {
const drawn = track(0);
const dotOpacity = track(0);
const dot = trackPoint(2, 0);
const labelOpacity = track(0);
const steps = [
{ title: "1. Draw the orbit", anim: draw(drawn, { duration: 1.5 }) },
{ title: "2. Place a body", anim: fadeIn(dotOpacity, { duration: 0.8 }) },
{
title: "3. Send it around",
anim: moveAlong(dot, ORBIT, { duration: 3 }),
},
{ title: "4. Name it", anim: fadeIn(labelOpacity, { duration: 0.8 }) },
];
// Each chapter starts where the previous steps' durations add up.
let at = 0;
const chapters = steps.map((step) => {
const chapter = {
title: step.title,
start: at,
end: at + step.anim.duration,
};
at = chapter.end;
return chapter;
});
const tl = usePlayer().timeline(sequence(...steps.map((s) => s.anim)));
const chapter = derive(
tl.time,
(t) => chapters.filter((c) => t >= c.start).pop() ?? chapters[0],
);
const time = useTrackValue(tl.time);
const playing = useTrackValue(tl.playing);
const current = useTrackValue(chapter);
onSettled(() => void tl.play());
return (
<div>
<Stage units={10}>
<Circle radius={2} progress={drawn} />
<Dot center={dot} radius={0.2} opacity={dotOpacity} />
<Text text="orbit" x={0} y={0} opacity={labelOpacity} />
<Text text={derive(chapter, (c) => c.title)} y={-2.5} fontSize={0.4} />
</Stage>
<div
style={{
display: "flex",
gap: "0.75rem",
"align-items": "center",
"margin-top": "0.5rem",
}}
>
<button
type="button"
style={{ "min-width": "5rem" }}
onClick={() => (playing() ? tl.pause() : void tl.play())}
>
{playing() ? "Pause" : "Play"}
</button>
<div style={{ flex: "1", position: "relative" }}>
<input
type="range"
aria-label="Timeline position"
min={0}
max={tl.duration}
step="any"
value={time()}
style={{ width: "100%" }}
onInput={(e) => {
tl.pause();
tl.seek(Number(e.currentTarget.value));
}}
/>
<div style={{ position: "relative", height: "1.6rem" }}>
{chapters.map((c, i) => (
<button
type="button"
title={c.title}
aria-label={`Jump to ${c.title}`}
aria-current={current() === c ? "step" : undefined}
style={{
position: "absolute",
left: `${(c.start / tl.duration) * 100}%`,
"font-size": "0.75rem",
padding: "0 0.4rem",
"font-weight": current() === c ? "700" : "400",
}}
onClick={() => {
tl.pause();
tl.seek(c.start);
}}
>
{i + 1}
</button>
))}
</div>
</div>
</div>
</div>
);
}

Describe each step as data: a title and an animation. The animation is a plain sequence of the steps, so the total length is the sum of their durations. The tracks are what the scene binds to.

2 collapsed lines
import { draw, fadeIn, moveAlong, track, trackPoint } from "@sophistry/core";
import { circlePolyline } from "@sophistry/core/geometry";
const orbit = circlePolyline(0, 0, 2);
const drawn = track(0);
const dotOpacity = track(0);
const dot = trackPoint(2, 0);
const labelOpacity = track(0);
const steps = [
{ title: "1. Draw the orbit", anim: draw(drawn, { duration: 1.5 }) },
{ title: "2. Place a body", anim: fadeIn(dotOpacity, { duration: 0.8 }) },
{ title: "3. Send it around", anim: moveAlong(dot, orbit, { duration: 3 }) },
{ title: "4. Name it", anim: fadeIn(labelOpacity, { duration: 0.8 }) },
];

The controls (button, slider) are DOM beside the <Stage>, so the timeline is built in the component that renders both. Get it from usePlayer(), which aborts it when the component is disposed, and start it from onSettled.

3 collapsed lines
import { createClock, sequence } from "@sophistry/core";
import { usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const clock = createClock();
const tl = usePlayer().timeline(sequence(...steps.map((step) => step.anim)), { clock });
onSettled(() => void tl.play());

The stage renders the scene with the tracks as props, on the same clock:

<Stage units={10} clock={clock}>
<Circle radius={2} progress={drawn} />
<Dot center={dot} radius={0.2} opacity={dotOpacity} />
<Text text="orbit" opacity={labelOpacity} />
</Stage>

usePlayer() here is called outside the stage, so on its own it would run on the shared defaultClock. Giving the stage and the timeline one createClock() ties them together, which has two bonuses: the clock is suspended while the stage is off-screen, so playback does not run away unseen, and pausing or slowing it touches this scene only. (The live example can leave the clock out: on this site every example already runs on a clock of its own.)

A native <input type="range">, bound to tl.time and tl.seek. tl.time and tl.playing are Readables; useTrackValue turns them into Solid accessors for the DOM. Pause on input so the user’s position is not overwritten by the next clock step.

import { useTrackValue } from "@sophistry/solidjs";
const time = useTrackValue(tl.time);
const playing = useTrackValue(tl.playing);
<button type="button" onClick={() => (playing() ? tl.pause() : void tl.play())}>
{playing() ? "Pause" : "Play"}
</button>
<input
type="range"
aria-label="Timeline position"
min={0}
max={tl.duration}
step="any"
value={time()}
onInput={(e) => {
tl.pause();
tl.seek(Number(e.currentTarget.value));
}}
/>

The thumb follows playback and the button label stays correct with no extra state. Pressing play at the end restarts from 0. Only the DOM controls read the time through Solid; the scene itself is bound to the tracks, so scrubbing runs no Solid code inside the stage.

A native range input is keyboard-accessible (arrows, Home, End) for free, which is a good reason to use one rather than a draggable canvas handle. If you want the knob inside the scene instead, see the ParameterSlider example in Interaction.

Each step’s start is the running sum of the durations before it. Compute them once, then use them for both the buttons and the “current chapter” readout.

import { derive } from "@sophistry/core";
let at = 0;
const chapters = steps.map((step) => {
const chapter = { title: step.title, start: at, end: at + step.anim.duration };
at = chapter.end;
return chapter;
});
const chapter = derive(tl.time, (t) => chapters.filter((c) => t >= c.start).pop() ?? chapters[0]);
const current = useTrackValue(chapter); // for the DOM buttons
<Text text={derive(chapter, (c) => c.title)} y={-2.5} fontSize={0.4} />; // in the scene

Position a button for each chapter at start / tl.duration percent along the slider, and have it call tl.pause(); tl.seek(c.start). chapter is a derive of tl.time, so the caption in the scene binds to it directly, and the highlighted button follows it through current(), as the timeline plays or is scrubbed.

  • Looping. Wrap the sequence in repeat(..., 3), or await tl.play() in a loop. Note that tl.duration of forever(...) is Infinity, which a slider cannot map; use repeat with a finite count.
  • Speed control. The timeline runs on clock, so clock.setSpeed(0.5) slows everything, and a <select> of speeds is one line.
  • Single steps. clock.advance(1 / 30) while paused nudges time forward by hand. Or tl.seek(tl.time.peek() + 0.1).
  • A trail that scrubs. <TracedPath point={dot} time={tl.time} /> draws where the body has been, and shortens as you scrub back.
  • A seek-only scene. If you never call tl.play(), the timeline is purely a function from time to state, which is exactly what scroll-driven animation uses.