summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 22:59:59 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit6b07c3b2ee86592a28a713b4c6c40007c28eb6b8 (patch)
treecc95801ec3fd34e90315bf033db3a714a68dc8c0
parent7e3475a779f9e8dcb14b96cfc59e2b92a20c0cda (diff)
downloadsketchbook-6b07c3b2ee86592a28a713b4c6c40007c28eb6b8.tar.gz
sketchbook-6b07c3b2ee86592a28a713b4c6c40007c28eb6b8.zip
examples: a talk that runs its own Deck, and its script
examples/talk.zig keeps an anim.Deck in its State beside its own data, turns clicks and keys into asks and paints with an anim.Painter: two slides, a mass that bobs (loop) while the deck waits, a damping slider laid out and hit-tested in the talk (it takes its clicks), a choice gate that branches on it, and slide 1 cleared beside slide 2's writing. test/talk.script (zig build test-talk) steps it, finishes a step on a click, goes back, jumps slides, drags the slider without advancing, reloads mid-wait with the first step retimed (same step, t a second later) and checks two exports with 2 s holds frame by frame. The vecanim live scene's words follow the new pause.
-rw-r--r--build.zig22
-rw-r--r--examples/talk.zig160
-rw-r--r--examples/vecanim/scenes/live.zig7
-rw-r--r--examples/vecanim/test/live.script5
-rw-r--r--test/talk.script104
5 files changed, 293 insertions, 5 deletions
diff --git a/build.zig b/build.zig
index 9c6085c..3739bcf 100644
--- a/build.zig
+++ b/build.zig
@@ -249,6 +249,28 @@ pub fn build(b: *Build) void {
test_step.dependOn(&compiled_script.step);
test_anim.dependOn(&compiled_script.step);
+ // The talk example: a Deck in a sketch's own State, stepped, sent back,
+ // jumped by slides, clicked on its slider, reloaded mid-wait with its
+ // first step retimed, and exported twice, with the same frames.
+ const talk_imports: []const Build.Module.Import = &.{ .{ .name = "anim", .module = sb.modules.anim }, .{ .name = "sketch", .module = sketch } };
+ const talk = sb.app(.{ .name = "talk", .root_source_file = b.path("examples/talk.zig"), .imports = &.{
+ .{ .name = "anim", .module = sb.modules.anim },
+ .{ .name = "typst", .module = sb.addTypst(b.createModule(.{ .root_source_file = b.path("examples/talk.zig"), .imports = talk_imports })) },
+ } });
+ const talk_check = talk.addReloadCheck(.{ .script = b.path("test/talk.script"), .edits = &.{.{
+ .file = "examples/talk.zig",
+ .old = "s.play(.{ title.write(), law.write() }, .{});",
+ .new = "s.play(.{ title.write(), law.write() }, .{ .seconds = 3 });",
+ }} });
+ const talk_exe = talk.addExecutable();
+ for ([_][]const u8{ "a", "b" }, [_]u64{ 400, 401 }) |define, frames| {
+ talk_check.run.addArg("--define");
+ talk_check.run.addPrefixedDirectoryArg(b.fmt("{s}=", .{define}), talk.addExport(talk_exe, .{ .fps = 30, .frames = frames, .size = .{ 640, 360 } }).frames);
+ }
+ const test_talk = b.step("test-talk", "Play the talk example: steps, back, slides, its slider, a reload mid-wait and its export");
+ test_talk.dependOn(&talk_check.run.step);
+ test_step.dependOn(&talk_check.run.step);
+
// Both vector renderers against resvg, through the host (tools/svgdiff.py).
const svgview = sb.app(.{ .name = "svgview", .root_source_file = b.path("test/svgview.zig"), .imports = &.{.{ .name = "vector", .module = sb.modules.vector }} });
const diff = b.addSystemCommand(&.{ "python3", "tools/svgdiff.py" });
diff --git a/examples/talk.zig b/examples/talk.zig
new file mode 100644
index 0000000..f2a2d9c
--- /dev/null
+++ b/examples/talk.zig
@@ -0,0 +1,160 @@
+//! A talk in a sketch of its own: the sketch keeps an `anim.Deck` in its
+//! State beside its own data, decides which keys and clicks step the talk,
+//! and paints with an `anim.Painter`. The talk is a function run from the
+//! top every frame; it may change the app in `update` (`anim.mutable`),
+//! so the slider below is laid out, drawn and hit-tested in one place.
+//!
+//! click, space, right, page down next step left, page up back
+//! ] / [ next / previous slide
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+/// This file's Typst, compiled at build time (`sb.addTypst`).
+const svgs = @import("typst");
+
+pub const name = "talk";
+pub const width = anim.width;
+pub const height = anim.height;
+
+pub const typst_preamble =
+ \\#set page(width: auto, height: auto, margin: 3pt, fill: none)
+ \\#set text(fill: rgb("#ece6e2"), size: 28pt)
+;
+pub const typst = struct {
+ pub const title = "#text(size: 40pt, weight: \"bold\")[Damping]";
+ pub const law = "$ x'' + 2 zeta x' + x = 0 $";
+ pub const hint = "#text(size: 18pt, fill: rgb(\"#58c4dd\"))[drag $zeta$, then go on]";
+ pub const rings = "#text(size: 24pt)[$zeta < 1$: it rings]";
+ pub const creeps = "#text(size: 24pt)[$zeta >= 1$: it creeps back]";
+ pub const end = "#text(size: 32pt)[$zeta = 1$: home fastest, no overshoot]";
+};
+/// What the build compiles.
+pub const scene = struct {
+ pub const typst_sources = anim.sources(.{typst});
+};
+
+const App = struct {
+ zeta: f32 = 0.2,
+ /// The slider is held.
+ dragging: bool = false,
+};
+
+pub const State = struct {
+ deck: anim.Deck = .{ .hold = 2 },
+ app: App = .{},
+};
+
+pub const Session = struct {
+ painter: anim.Painter = .{},
+};
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ var ask: anim.Ask = .{};
+ for (in.events) |e| switch (e.kind) {
+ // Every click is the presenter's, unless the talk takes it.
+ .mouse_up => ask.next = true,
+ .key_down => switch (@as(sk.Key, @fromBackingInt(@intCast(@as(u8, @truncate(e.code)))))) {
+ .space, .right, .page_down => ask.next = true,
+ .left, .page_up => ask.back = true,
+ .right_bracket => ask.next_slide = true,
+ .left_bracket => ask.back_slide = true,
+ else => {},
+ },
+ else => {},
+ };
+ s.deck.update(in, ask, talk, .{&s.app});
+}
+
+pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
+ const mouse = anim.Deck.pointer(f.in, f.in.mouse_x, f.in.mouse_y);
+ const shot = s.deck.shot(f.scratch, f.in, mouse, talk, .{&s.app}) catch |err| return f.host.print("talk: {t}", .{err});
+ ss.painter.paint(f, shot, svgs, .{}) catch |err| f.host.print("paint: {t}", .{err});
+ if (f.in.exporting and s.deck.t >= shot.duration) f.quit();
+}
+
+/// Run in `update` with `app: *App`, and drawn with `app: *const App`.
+fn talk(s: *anim.Frame, app: anytype) void {
+ // Slide 1: the law, and a mass on a spring that bobs while we talk.
+ s.slide();
+ const title = s.typst(typst.title, .{ .y = -270 });
+ const law = s.typst(typst.law, .{ .y = -180 });
+ const mass = s.rect(.{ .w = 60, .h = 60, .fill = anim.blue });
+ s.play(.{ title.write(), law.write() }, .{});
+ s.play(.{mass.fadeIn()}, .{ .seconds = 0.5 });
+ s.loop(bob, .{mass});
+ s.pause();
+
+ // The slider, laid out before the gate it waits at: a click on it is
+ // its own, not the presenter's.
+ const plot = s.custom(s.new(Plot{ .zeta = app.zeta }), .{});
+ const hint = s.typst(typst.hint, .{ .y = 290 });
+ s.play(.{ mass.fadeOut(), plot.fadeIn(), hint.fadeIn() }, .{ .seconds = 0.6 });
+ if (anim.mutable(app)) |a| if (s.pose(plot).visible) drag(s, a);
+ // A choice: the regime the slider is in when the presenter goes on.
+ const regime = s.gate(if (s.next) @intFromBool(app.zeta >= 1) else null) orelse return;
+ const verdict = s.typst(if (regime == 0) typst.rings else typst.creeps, .{ .y = 290 });
+ s.play(.{ hint.fadeOut(), verdict.write() }, .{ .seconds = 1 });
+ s.pause();
+
+ // Slide 2, written while slide 1 goes.
+ s.beside(anim.Frame.clear, .{.{ .seconds = 0.8 }});
+ s.slide();
+ s.play(.{s.typst(typst.end, .{}).write()}, .{});
+}
+
+/// Down and up again, in a second.
+fn bob(s: *anim.Frame, mass: anim.Obj) void {
+ s.play(.{mass.move(.{ .y = 80 })}, .{ .seconds = 0.5 });
+ s.play(.{mass.move(.{ .y = 0 })}, .{ .seconds = 0.5 });
+}
+
+/// The slider's track, in scene units: zeta from 0 to 2 along it.
+const track: sk.Rect = .{ .x = -300, .y = 200, .w = 600, .h = 40 };
+
+/// The slider follows a press on it to where it is let go, and takes that
+/// click: it is not the presenter's next.
+fn drag(s: *anim.Frame, app: *App) void {
+ for (s.events) |e| {
+ if (e.kind != .mouse_down and e.kind != .mouse_move and e.kind != .mouse_up) continue;
+ if (e.kind == .mouse_down) app.dragging = track.contains(e.x, e.y);
+ if (!app.dragging) continue;
+ app.zeta = std.math.clamp((e.x - track.x) / track.w * 2, 0, 2);
+ if (e.kind == .mouse_up) {
+ app.dragging = false;
+ s.next = false;
+ }
+ }
+}
+
+/// The motion zeta makes, x(t) from x = 1 at rest, over the slider: a
+/// custom object at the scene's origin, so its units are scene units.
+const Plot = struct {
+ zeta: f32,
+
+ pub fn draw(p: *const Plot, f: *sk.Frame, at: anim.Placed) void {
+ var points: [121]sk.math.Vec = undefined;
+ for (&points, 0..) |*q, i| {
+ const t: f32 = @as(f32, @floatFromInt(i)) / 10;
+ q.* = .{ -300 + 50 * t, 60 - 120 * response(p.zeta, t) };
+ }
+ sk.draw.line(f, .{ -300, 60 }, .{ 300, 60 }, at.pen(.{ .stroke = .hex(0x8c8c8c), .width = 1 }));
+ sk.draw.polyline(f, &points, at.pen(.{ .stroke = .hex(anim.yellow), .width = 3 }));
+ sk.draw.rect(f, .{ .x = track.x, .y = track.y + 18, .w = track.w, .h = 4 }, at.pen(.{ .fill = .hex(0x8c8c8c) }));
+ sk.draw.circle(f, .{ track.x + p.zeta / 2 * track.w, track.y + 20 }, 14, at.pen(.{ .fill = .hex(anim.blue) }));
+ var label: [16]u8 = undefined;
+ at.text(f, track.x + p.zeta / 2 * track.w - 30, track.y - 22, 1.5, std.fmt.bufPrint(&label, "{d:.2}", .{p.zeta}) catch "", .white);
+ }
+};
+
+/// x(t) of x'' + 2 zeta x' + x = 0 from x = 1, x' = 0.
+fn response(zeta: f32, t: f32) f32 {
+ if (zeta < 1) {
+ const w = @sqrt(1 - zeta * zeta);
+ return @exp(-zeta * t) * (@cos(w * t) + zeta / w * @sin(w * t));
+ }
+ if (zeta == 1) return @exp(-t) * (1 + t);
+ const r = @sqrt(zeta * zeta - 1);
+ const a = -zeta + r;
+ const b = -zeta - r;
+ return (b * @exp(a * t) - a * @exp(b * t)) / (b - a);
+}
diff --git a/examples/vecanim/scenes/live.zig b/examples/vecanim/scenes/live.zig
index 4af7998..9b94d17 100644
--- a/examples/vecanim/scenes/live.zig
+++ b/examples/vecanim/scenes/live.zig
@@ -1,6 +1,7 @@
//! A live scene, presented like slides: `frame` runs every frame, so the
-//! code after the plays can follow the mouse, the clock or any data, and
-//! `pause` holds until a click. Everything it needs is in this file.
+//! code after the plays can follow the mouse, the time or any data, and
+//! what comes after a `pause` waits for a click while time runs on.
+//! Everything it needs is in this file.
const anim = @import("anim");
pub const typst_preamble =
@@ -28,7 +29,7 @@ fn frame(s: *anim.Frame) void {
s.play(.{ title.fadeOut(), hint.fadeOut() }, .{ .seconds = 0.6 });
// A wave of dots: its height is a value the timeline animates, its
- // phase the wall clock, which runs on through pauses.
+ // phase the scene's time, which runs on while a pause waits.
const wave = s.typst(typst.wave, .{ .y = -220, .scale = 1.2 });
const note = s.typst(typst.note, .{ .y = 230 });
const height = s.value(0);
diff --git a/examples/vecanim/test/live.script b/examples/vecanim/test/live.script
index a3ddcdc..d955b6f 100644
--- a/examples/vecanim/test/live.script
+++ b/examples/vecanim/test/live.script
@@ -1,5 +1,6 @@
-# Plays the live scene headless: it holds at each pause until a click, the
-# dots follow the pointer, and the guides draw over everything.
+# Plays the live scene headless: it waits at each pause for a click, the
+# wave running on, the dots follow the pointer, and the guides draw over
+# everything.
frames 150
expect t 2
screenshot .zig-cache/e2e/live-title.png
diff --git a/test/talk.script b/test/talk.script
new file mode 100644
index 0000000..e123d59
--- /dev/null
+++ b/test/talk.script
@@ -0,0 +1,104 @@
+# The talk example (examples/talk.zig): a Deck in the sketch's own State,
+# stepped by the clicks and keys the sketch turns into asks.
+frames 90
+expect deck.waiting false
+expect deck.slide 1
+# Slide 2 lies after the choice, which has not been made.
+expect deck.slides 1
+# A click during the first step finishes it: held at the first pause.
+click 100 100
+expect deck.t 2.5 ~0.0001
+expect deck.waiting true
+expect deck.log.len 0
+# The mass bobs while the deck waits.
+screenshot .zig-cache/talk/bob-a.png
+frames 14
+screenshot .zig-cache/talk/bob-b.png
+expect-different-screenshot .zig-cache/talk/bob-a.png .zig-cache/talk/bob-b.png
+# The next click opens the pause where it lands: the slider comes in, and
+# the deck waits at the choice.
+click 100 100
+expect deck.log.len 1
+expect deck.log.buffer[0].wait 0.2833 ~0.001
+await deck.waiting true
+frames 5
+# Reloaded mid-wait, the first step retimed from 2 s to 3: the same step,
+# as far into it, a second later.
+console set deck.playing false
+expect deck.t 3.4833 ~0.001
+load {v2}
+expect deck.t 4.4833 ~0.001
+expect deck.waiting true
+expect deck.log.len 1
+expect deck.log.buffer[0].wait 0.2833 ~0.001
+console set deck.playing true
+# A click on the slider is the slider's: zeta goes there, the deck stays.
+click 790 580
+expect app.zeta 1.5 ~0.01
+expect deck.log.len 1
+expect deck.waiting true
+# So is a drag along it, let go and all.
+down 640 580
+move 400 580
+up 400 580
+expect app.zeta 0.2 ~0.01
+expect app.dragging false
+expect deck.log.len 1
+expect deck.waiting true
+# Going on chooses by the slider: under 1, it rings; slide 2 is laid.
+key space
+expect deck.log.len 2
+expect deck.log.buffer[1].value 0
+expect deck.slides 2
+frames 90
+screenshot .zig-cache/talk/rings.png
+# Back: held at the choice again; over 1, it creeps.
+key page_up
+expect deck.log.len 1
+expect deck.waiting true
+click 840 580
+expect app.zeta 1.667 ~0.01
+key space
+expect deck.log.len 2
+expect deck.log.buffer[1].value 1
+frames 90
+screenshot .zig-cache/talk/creeps.png
+expect-different-screenshot .zig-cache/talk/rings.png .zig-cache/talk/creeps.png
+# Slide 2 at rest: it holds no pause, so its end.
+key right_bracket
+expect deck.slide 2
+expect deck.log.len 3
+expect deck.waiting false
+# Back a slide: slide 1 at rest, held at its first pause (3 s of writing
+# in this build, then the mass).
+key left_bracket
+expect deck.slide 1
+expect deck.log.len 0
+expect deck.t 3.5 ~0.0001
+expect deck.waiting true
+# Recorded, clicks, the slider and keys replay to the same State: the deck
+# opens gates in update, from events and State alone.
+console record 0
+click 100 100
+frames 40
+click 790 580
+key space
+frames 30
+key page_up
+key right_bracket
+frames 10
+console save 2
+console play 0
+frames 120
+expect-snapshot 2
+console stop
+# Exported ({a}, {b}: 30 fps, half size), each gate holds 2 s: the mass
+# bobs through the first hold (to 4.5 s), nothing moves at the choice
+# (5.1 to 7.1 s), and the verdict writes after it. Twice, the same frames.
+expect-different-screenshot {a}/000080.png {a}/000090.png
+expect-different-screenshot {a}/000130.png {a}/000170.png
+expect-same-screenshot {a}/000170.png {a}/000200.png
+expect-different-screenshot {a}/000200.png {a}/000230.png
+expect-same-screenshot {a}/000080.png {b}/000080.png
+expect-same-screenshot {a}/000230.png {b}/000230.png
+expect-same-screenshot {a}/000350.png {b}/000350.png