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

Sequencing and timelines

Combining animations and controlling them once they run. Sequencing shows sequence, parallel and lagged; the Scrub* examples drive a timeline() from a slider; CustomAnimationLeaf writes an animation from scratch.

Sequencing animations

The same three lifts played one after another, all together, and staggered, using sequence, parallel and lagged.

Sequencing.tsx
11 collapsed lines
import {
derive,
hold,
lagged,
parallel,
sequence,
track,
valueTo,
} from "@sophistry/core";
import { Circle, Stage, Text, usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const XS = [-2.5, 0, 2.5];
export default function Sequencing() {
const { play, signal } = usePlayer();
const ys = XS.map(() => track(-0.5));
const caption = track("");
const up = () => ys.map((y) => valueTo(y, 0.8, { duration: 0.6 }));
const down = parallel(...ys.map((y) => valueTo(y, -0.5, { duration: 0.4 })));
onSettled(() => {
void (async () => {
while (!signal.aborted) {
caption.set("sequence: one after another");
await play(sequence(...up(), hold(0.5), down, hold(0.5)));
caption.set("parallel: all at once");
await play(sequence(parallel(...up()), hold(0.5), down, hold(0.5)));
caption.set("lagged: staggered starts");
await play(sequence(lagged(up(), { lag: 0.2 }), hold(0.5), down));
await play(hold(1));
}
})();
});
return (
<Stage units={10}>
{XS.map((x, i) => (
<Circle center={derive(ys[i], (y) => ({ x, y }))} radius={0.6} />
))}
<Text text={caption} y={-2.4} />
</Stage>
);
}

A scrubbable timeline

One script, three steps. player.timeline() runs it and exposes seek, time and playing, so a button and a range slider can pause, resume and scrub it in either direction.

ScrubbableTimeline.tsx
17 collapsed lines
import {
derive,
draw,
moveTo,
scaleTo,
sequence,
track,
trackPoint,
} from "@sophistry/core";
import {
Circle,
Stage,
Text,
usePlayer,
useTrackValue,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
export default function ScrubbableTimeline() {
const drawn = track(0);
const position = trackPoint(-3, 0);
const scale = track(1);
const tl = usePlayer().timeline(
sequence(
draw(drawn, { duration: 1.5 }),
moveTo(position, { x: 3, y: 0 }, { duration: 2 }),
scaleTo(scale, 1.8, { duration: 1 }),
),
);
const time = useTrackValue(tl.time);
const playing = useTrackValue(tl.playing);
onSettled(() => void tl.play());
return (
<div>
<Stage units={10}>
<Circle center={position} radius={1} scale={scale} progress={drawn} />
<Text
text={derive(tl.time, (t) => `t = ${t.toFixed(2)} s`)}
y={-2.4}
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>
<input
type="range"
aria-label="Timeline position"
min={0}
max={tl.duration}
step="any"
value={time()}
style={{ flex: "1" }}
onInput={(e) => {
tl.pause();
tl.seek(Number(e.currentTarget.value));
}}
/>
</div>
</div>
);
}

First scene: scrub it with a timeline

Step four of the first-scene tutorial: the whole script becomes a timeline you can seek with a slider, backwards as well as forwards.

FirstSceneTimeline.tsx
20 collapsed lines
import {
derive,
draw,
sequence,
type Track,
track,
valueTo,
} from "@sophistry/core";
import {
Arc,
Circle,
Dot,
Line,
Stage,
Text,
usePlayer,
useScene,
useTrackValue,
} from "@sophistry/solidjs";
import { onSettled } from "solid-js";
const RADIUS = 2.4;
/** The ring, radius and angle, coloured from the stage's theme. */
function Ring(props: { drawn: Track<number>; theta: Track<number> }) {
const palette = derive(useScene().theme, (t) => t.palette);
const tip = derive(props.theta, (theta) => ({
x: RADIUS * Math.cos(theta),
y: RADIUS * Math.sin(theta),
}));
return (
<>
<Circle
radius={RADIUS}
stroke={derive(palette, (p) => p.grid)}
progress={props.drawn}
/>
<Line
from={{ x: 0, y: 0 }}
to={tip}
stroke={derive(palette, (p) => p.series[0])}
/>
<Arc
radius={0.8}
angle={props.theta}
stroke={derive(palette, (p) => p.series[1])}
/>
<Dot center={tip} />
</>
);
}
export default function FirstSceneTimeline() {
const drawn = track(0);
const theta = track(0);
const tl = usePlayer().timeline(
sequence(
draw(drawn, { duration: 1.2 }),
valueTo(theta, 2 * Math.PI, { duration: 3, easing: "smooth" }),
),
);
const time = useTrackValue(tl.time);
const playing = useTrackValue(tl.playing);
onSettled(() => void tl.play());
return (
<>
<Stage units={10} aspect={4 / 3}>
<Ring drawn={drawn} theta={theta} />
<Text text={derive(tl.time, (t) => `t = ${t.toFixed(1)} s`)} y={-3} />
</Stage>
<div
style={{
display: "flex",
gap: "0.75rem",
"align-items": "center",
"margin-top": "0.5rem",
}}
>
<button
type="button"
onClick={() => (playing() ? tl.pause() : void tl.play())}
>
{playing() ? "Pause" : "Play"}
</button>
<input
type="range"
aria-label="Timeline position"
style={{ flex: "1" }}
min={0}
max={tl.duration}
step="any"
value={time()}
onInput={(e) => {
tl.pause();
tl.seek(Number(e.currentTarget.value));
}}
/>
</div>
</>
);
}

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>
);
}

One track, many drivers

The same x track is written by dragging the knob, by a tween from the button, and by a slider. Nothing is wired together: they all just write the track.

OneTrackManyDrivers.tsx
17 collapsed lines
import {
derive,
draggable,
scalarParam,
type Track,
track,
valueTo,
} from "@sophistry/core";
import {
Dot,
Line,
Stage,
Text,
usePlayer,
useScene,
useTrackValue,
} from "@sophistry/solidjs";
const FROM = { x: -3, y: 0 };
const TO = { x: 3, y: 0 };
function Knob(props: { x: Track<number> }) {
const palette = derive(useScene().theme, (t) => t.palette);
const param = scalarParam(FROM, TO, { range: [-3, 3] });
return (
<>
<Line from={FROM} to={TO} stroke={derive(palette, (p) => p.grid)} />
<Dot
center={derive(props.x, (x) => ({ x, y: 0 }))}
radius={0.22}
label="value"
{...draggable(props.x, { param })}
/>
<Text text={derive(props.x, (x) => `x = ${x.toFixed(2)}`)} y={1.2} />
<Text
text="drag it, tween it, or slide it"
y={-1.2}
fontSize={0.28}
color={derive(palette, (p) => p.muted)}
/>
</>
);
}
export default function OneTrackManyDrivers() {
const { play } = usePlayer();
const x = track(0);
const value = useTrackValue(x);
return (
<>
<Stage units={10}>
<Knob x={x} />
</Stage>
<div
style={{
display: "flex",
gap: "0.75rem",
"align-items": "center",
"margin-top": "0.5rem",
}}
>
<button
type="button"
onClick={() => void play(valueTo(x, x.peek() > 0 ? -3 : 3))}
>
Tween to the other end
</button>
<input
type="range"
aria-label="x"
style={{ flex: "1" }}
min={-3}
max={3}
step="any"
value={value()}
onInput={(e) => x.set(Number(e.currentTarget.value))}
/>
</div>
</>
);
}

A custom animation leaf

animation({...}) builds a seekable leaf from begin and seek. This one makes a counter tick in whole numbers while a built-in valueTo slides the dot, and both obey scrubbing and composites.

CustomAnimationLeaf.tsx
11 collapsed lines
import {
animation,
derive,
parallel,
sequence,
type Track,
track,
valueTo,
} from "@sophistry/core";
import { Circle, Stage, Text, usePlayer } from "@sophistry/solidjs";
import { onSettled } from "solid-js";
/** Counts `value` up by `amount` in whole steps, relative to where it starts. */
function countUp(value: Track<number>, amount: number, duration: number) {
let from = 0;
return animation({
duration,
begin: () => {
from = value.peek();
},
// Pure function of t plus what begin() captured, so seeking is safe either way.
seek: (t) => value.set(from + Math.round(amount * (t / duration))),
});
}
export default function CustomAnimationLeaf() {
const { play, signal } = usePlayer();
const x = track(-3);
const count = track(0);
onSettled(() => {
void (async () => {
while (!signal.aborted) {
x.set(-3);
count.set(0);
await play(
sequence(
parallel(valueTo(x, 3, { duration: 2 }), countUp(count, 12, 2)),
parallel(valueTo(x, -3, { duration: 2 }), countUp(count, 12, 2)),
),
);
}
})();
});
return (
<Stage units={10}>
<Circle
center={derive(x, (x) => ({ x, y: 0.4 }))}
radius={0.5}
fill="#58c4dd"
/>
<Text
text={derive(count, (count) => `ticks: ${count}`)}
y={-1.2}
fontSize={0.5}
/>
</Stage>
);
}