summaryrefslogtreecommitdiff
path: root/examples/vecanim
diff options
context:
space:
mode:
Diffstat (limited to 'examples/vecanim')
-rw-r--r--examples/vecanim/.gitignore5
-rw-r--r--examples/vecanim/README.md39
-rw-r--r--examples/vecanim/build.zig31
-rw-r--r--examples/vecanim/build.zig.zon10
-rw-r--r--examples/vecanim/scenes/intro.zig72
-rw-r--r--examples/vecanim/scenes/live.zig70
-rw-r--r--examples/vecanim/test/intro.script29
-rw-r--r--examples/vecanim/test/live.script24
8 files changed, 280 insertions, 0 deletions
diff --git a/examples/vecanim/.gitignore b/examples/vecanim/.gitignore
new file mode 100644
index 0000000..ff18573
--- /dev/null
+++ b/examples/vecanim/.gitignore
@@ -0,0 +1,5 @@
+zig-out/
+.zig-cache/
+zig-pkg/
+*.state
+*.state.inputs
diff --git a/examples/vecanim/README.md b/examples/vecanim/README.md
new file mode 100644
index 0000000..cc6dc0e
--- /dev/null
+++ b/examples/vecanim/README.md
@@ -0,0 +1,39 @@
+# vecanim
+
+Scenes for sketchbook's [`anim`](../../README.md#animation)
+module: manim-style vector animation, with Typst for text and math, scenes
+written as code (compiled, or live and interactive), cameras, morphs and
+slide-style pauses. The library, its player and their tests live in
+sketchbook; this package is the scenes, a script for each, and a
+`build.zig` that lists them.
+
+```sh
+zig build run # play scenes/intro.zig, with hot reload
+zig build lib --watch # second terminal: save the scene, see it live
+zig build run -Dscene=scenes/live.zig # any other scene file (and lib -Dscene=...)
+zig build test # each scene played through its script, headless
+zig build web # the same player in the browser (CPU renderer)
+zig build export -- frames --size 1920x1080 # every frame as frames/000000.png, ...
+zig build export -- - --size 3840x2160 --fps 60 |
+ ffmpeg -f rawvideo -pix_fmt rgba -s 3840x2160 -r 60 -i - -pix_fmt yuv420p intro.mp4
+```
+
+Keys: click, space or page down for the next step of a scene that pauses
+(space plays and pauses one that does not), page up back a step, `]`/`[`
+next/previous slide, P play/pause, left/right previous/next play, `,`/`.`
+step a frame, home/end, up/down speed, B GPU/CPU, L loop, G guides
+(bounding boxes, origins and the scene's axes, with scene coordinates and
+the pointer's); drag and wheel to look around, R to reset the view.
+
+- `scenes/intro.zig`, compiled: shapes, Pythagoras morphing from one
+ equation to the next, a camera move with an updater swinging an arrow,
+ Typst math and a paragraph.
+- `scenes/live.zig`, live, presented like slides: a wave of dots whose
+ height the timeline animates, whose phase runs on while a pause waits,
+ and which rise to meet the mouse.
+
+`build.zig` lists the scenes with the script `zig build test` plays each
+through (screenshots in `.zig-cache/e2e`). `-Dscene` picks the one `run`,
+`lib`, `web` and `export` use. Each player is its own sketchbook app named
+after its scene (`vecanim-intro`, `vecanim-live`), so several can run and
+reload side by side with their own state.
diff --git a/examples/vecanim/build.zig b/examples/vecanim/build.zig
new file mode 100644
index 0000000..b7e5623
--- /dev/null
+++ b/examples/vecanim/build.zig
@@ -0,0 +1,31 @@
+const std = @import("std");
+const sketchbook = @import("sketchbook");
+
+pub fn build(b: *std.Build) void {
+ const target = b.standardTargetOptions(.{});
+ const optimize = b.standardOptimizeOption(.{});
+ const sb = sketchbook.init(b, .{ .target = target, .optimize = optimize });
+
+ // A scene is one file, played by sketchbook's `anim` player. `-Dscene`
+ // picks the one `run`, `lib`, `web` and `export` use; `test` plays each
+ // of these through its script.
+ const scenes = [_]struct { []const u8, []const u8 }{
+ .{ "scenes/intro.zig", "test/intro.script" },
+ .{ "scenes/live.zig", "test/live.script" },
+ };
+ const selected = b.option([]const u8, "scene", "The scene to play (default scenes/intro.zig)") orelse scenes[0][0];
+ const player = addPlayer(sb, selected);
+ const steps = player.app.addSteps(.{ .web = .{} });
+ for (scenes) |entry| {
+ const same = std.mem.eql(u8, entry[0], selected);
+ const each = if (same) player else addPlayer(sb, entry[0]);
+ const exe = if (same) steps.exe else each.app.addExecutable();
+ steps.e2e.dependOn(&each.app.addScript(exe, b.path(entry[1]), .{}).step);
+ }
+}
+
+/// The player built around one scene file, named after it (`vecanim-intro`),
+/// so that players of different scenes keep their own state and library.
+fn addPlayer(sb: sketchbook.Sketchbook, path: []const u8) sketchbook.Sketchbook.Scene {
+ return sb.addScene(.{ .name = sb.b.fmt("vecanim-{s}", .{std.fs.path.stem(path)}), .root_source_file = sb.b.path(path) });
+}
diff --git a/examples/vecanim/build.zig.zon b/examples/vecanim/build.zig.zon
new file mode 100644
index 0000000..5e24b1b
--- /dev/null
+++ b/examples/vecanim/build.zig.zon
@@ -0,0 +1,10 @@
+.{
+ .name = .vecanim,
+ .version = "0.1.0",
+ .dependencies = .{
+ .sketchbook = .{ .path = "../.." },
+ },
+ .minimum_zig_version = "0.17.0",
+ .paths = .{""},
+ .fingerprint = 0xd290125ce3ec4622,
+}
diff --git a/examples/vecanim/scenes/intro.zig b/examples/vecanim/scenes/intro.zig
new file mode 100644
index 0000000..07205e7
--- /dev/null
+++ b/examples/vecanim/scenes/intro.zig
@@ -0,0 +1,72 @@
+//! The intro: shapes, Pythagoras, a camera move, and Typst math and text.
+//! Everything this scene needs is in this file. Save it while the player
+//! runs: the library rebuilds, and the animation carries on from the same
+//! instant.
+const std = @import("std");
+const anim = @import("anim");
+
+/// Put before every Typst source below: transparent, tight pages and light
+/// text, ready to animate on a dark background.
+pub const typst_preamble =
+ \\#set page(width: auto, height: auto, margin: 3pt, fill: none)
+ \\#set text(fill: rgb("#ece6e2"), size: 28pt)
+;
+
+pub const scene = anim.Scene(construct);
+
+fn construct(s: *anim.Builder) void {
+ const title = s.typst("#text(size: 40pt, weight: \"bold\")[wow sketches]", .{ .y = -40 });
+ const subtitle = s.typst("#text(fill: rgb(\"#58c4dd\"))[SVG, #emph[Typst] and the GPU, live!!!]", .{ .y = 20 });
+ s.play(.{title.write()}, .{});
+ s.play(.{subtitle.fadeIn()}, .{});
+ s.wait(0.8);
+ s.play(.{ title.fadeOut(), subtitle.fadeOut() }, .{});
+
+ const square = s.rect(.{ .w = 180, .h = 180, .x = -320, .y = 40, .stroke = anim.blue, .fill = 0x58c4dd33, .width = 6 });
+ const circle = s.circle(.{ .r = 95, .x = 320, .y = 40, .stroke = anim.red, .fill = 0xfc625533, .width = 6 });
+ const triangle = s.polygon(.{ .points = &[_][2]f32{ .{ -90, 60 }, .{ 90, 60 }, .{ -90, -75 } }, .y = 40, .stroke = anim.yellow, .width = 5 });
+ s.play(.{ square.create(), circle.create(), triangle.create() }, .{ .seconds = 1.5 });
+
+ // One equation turning into the next, each morph ending on its target.
+ const equations = [_]anim.Obj{
+ s.typst("$ a^2 + b^2 = c^2 $", .{ .y = 220, .scale = 1.4 }),
+ s.typst("$ c = sqrt(a^2 + b^2) $", .{ .y = 220, .scale = 1.4 }),
+ s.typst("$ 3^2 + 4^2 = 5^2 $", .{ .y = 220, .scale = 1.4 }),
+ };
+ s.play(.{equations[0].write()}, .{});
+ s.play(.{ square.rotate(std.math.pi / 4.0), circle.scaleTo(0.6) }, .{ .seconds = 1.2 });
+ s.play(.{equations[0].transformInto(equations[1])}, .{ .seconds = 1.5 });
+ s.play(.{equations[1].indicate()}, .{});
+ s.play(.{square.transformInto(circle)}, .{ .seconds = 1.5 });
+
+ // The arrow swings about as the camera leans in; how far is a value
+ // the timeline animates.
+ const arrow = s.arrow(.{ .dx = 220, .dy = 0, .y = -150, .stroke = anim.green, .width = 5 });
+ const swing = s.value(0);
+ s.always(arrow, swinging, .{swing});
+ s.play(.{ anim.camera(.{ .zoom = 1.4, .y = 50 }), arrow.create(), swing.to(0.4) }, .{ .seconds = 1.6 });
+ s.play(.{ equations[1].transformInto(equations[2]), swing.to(0) }, .{});
+ s.play(.{ anim.camera(.{ .zoom = 1, .y = 0 }), triangle.fadeOut(), circle.fadeOut(), arrow.fadeOut(), equations[2].fadeOut() }, .{ .seconds = 1.2 });
+
+ const euler = s.typst("$ e^(i pi) + 1 = 0 $", .{ .y = -40, .scale = 1.6 });
+ const gauss = s.typst("$ integral_(-infinity)^infinity e^(-x^2) dif x = sqrt(pi) $", .{ .y = -20, .scale = 1.2 });
+ const caption = s.typst(
+ \\#set text(size: 18pt)
+ \\#block(width: 420pt)[
+ \\ Every frame is a pure function of time: shapes are cubic Béziers,
+ \\ animations are `lerp`s between plain structs, and the GPU fills
+ \\ polygons by stencil and cover.
+ \\]
+ , .{ .y = 170 });
+ s.play(.{euler.write()}, .{ .seconds = 1.5 });
+ s.play(.{ euler.move(.{ .y = -210, .scale = 1 }), gauss.write() }, .{ .seconds = 2 });
+ s.play(.{caption.write()}, .{ .seconds = 3, .lag = 0.02 });
+ s.wait(1.5);
+ s.play(.{ euler.fadeOut(), gauss.fadeOut(), caption.fadeOut() }, .{});
+}
+
+fn swinging(now: anim.Now, swing: anim.Value) anim.Pose {
+ var p = now.pose;
+ p.rotation += now.value(swing) * @sin(now.t * 4);
+ return p;
+}
diff --git a/examples/vecanim/scenes/live.zig b/examples/vecanim/scenes/live.zig
new file mode 100644
index 0000000..9b94d17
--- /dev/null
+++ b/examples/vecanim/scenes/live.zig
@@ -0,0 +1,70 @@
+//! A live scene, presented like slides: `frame` runs every frame, so the
+//! 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 =
+ \\#set page(width: auto, height: auto, margin: 3pt, fill: none)
+ \\#set text(fill: rgb("#ece6e2"), size: 28pt)
+;
+
+/// The Typst this scene uses, compiled at build time.
+pub const typst = struct {
+ pub const title = "#text(size: 40pt, weight: \"bold\")[Live scenes]";
+ pub const hint = "#text(fill: rgb(\"#58c4dd\"), size: 18pt)[click to continue]";
+ pub const wave = "$ y = a sin(x/90 - t) $";
+ pub const damped = "$ y = a e^(-x^2 slash 10^5) sin(x/90 - t) $";
+ pub const note = "#text(size: 18pt)[the dots rise to meet the mouse]";
+};
+
+pub const scene = anim.Live(frame, typst);
+
+fn frame(s: *anim.Frame) void {
+ const title = s.typst(typst.title, .{ .y = -30 });
+ const hint = s.typst(typst.hint, .{ .y = 40 });
+ s.play(.{title.write()}, .{ .seconds = 1.5 });
+ s.play(.{hint.fadeIn()}, .{ .seconds = 0.5 });
+ s.pause();
+ s.play(.{ title.fadeOut(), hint.fadeOut() }, .{ .seconds = 0.6 });
+
+ // A wave of dots: its height is a value the timeline animates, its
+ // 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);
+ const damping = s.value(0);
+ var dots: [33]anim.Obj = undefined;
+ var appear: [dots.len + 1]anim.Animation = undefined;
+ for (&dots, appear[0..dots.len], 0..) |*d, *a, i| {
+ d.* = s.circle(.{ .r = 8, .x = (@as(f32, @floatFromInt(i)) - 16) * 34, .fill = anim.blue });
+ a.* = d.fadeIn();
+ }
+ appear[dots.len] = note.fadeIn();
+ s.play(.{wave.write()}, .{ .seconds = 1.2 });
+ s.play(appear, .{ .seconds = 0.6 });
+ s.play(.{height.to(90)}, .{ .seconds = 1.5, .ease = anim.ease.outBack });
+ s.pause();
+
+ const damped = s.typst(typst.damped, .{ .y = -220, .scale = 1.2 });
+ s.play(.{ wave.transformInto(damped), damping.to(1), anim.camera(.{ .zoom = 1.25, .y = 20 }) }, .{ .seconds = 1.5 });
+ s.pause();
+
+ var leave: [dots.len + 2]anim.Animation = undefined;
+ for (dots, leave[0..dots.len]) |d, *a| a.* = d.fadeOut();
+ leave[dots.len] = damped.fadeOut();
+ leave[dots.len + 1] = note.fadeOut();
+ s.play(leave, .{});
+ s.play(.{anim.camera(.{ .zoom = 1, .y = 0 })}, .{ .seconds = 0.5 });
+
+ // Plain code, after every play, so nothing overrides it: the wave, and
+ // each dot growing and rising as the mouse comes near.
+ for (dots) |d| {
+ const p = s.pose(d);
+ const envelope = 1 - s.get(damping) * (1 - @exp(-p.x * p.x / 1e5));
+ p.y = s.get(height) * envelope * @sin(p.x / 90 - s.t * 2);
+ const near = @max(0, 1 - @abs(p.x - s.pointer[0]) / 120);
+ p.scale = 1 + near;
+ p.y -= 40 * near;
+ }
+}
diff --git a/examples/vecanim/test/intro.script b/examples/vecanim/test/intro.script
new file mode 100644
index 0000000..9dd61bb
--- /dev/null
+++ b/examples/vecanim/test/intro.script
@@ -0,0 +1,29 @@
+# Plays the intro headless on the GPU, with screenshots along the way,
+# then the same instants on the CPU renderer.
+frames 60
+screenshot .zig-cache/e2e/title.png
+console set deck.t 7.5
+frames 1
+screenshot .zig-cache/e2e/shapes.png
+console set deck.t 13
+frames 1
+screenshot .zig-cache/e2e/morph.png
+console set deck.t 16.5
+frames 1
+screenshot .zig-cache/e2e/zoom.png
+console set deck.t 24
+frames 1
+screenshot .zig-cache/e2e/text.png
+key b
+expect backend cpu
+console set deck.t 13
+frames 1
+screenshot .zig-cache/e2e/morph-cpu.png
+console set deck.t 24
+frames 1
+screenshot .zig-cache/e2e/text-cpu.png
+key g
+expect guides true
+console set deck.t 16.5
+frames 1
+screenshot .zig-cache/e2e/guides.png
diff --git a/examples/vecanim/test/live.script b/examples/vecanim/test/live.script
new file mode 100644
index 0000000..d955b6f
--- /dev/null
+++ b/examples/vecanim/test/live.script
@@ -0,0 +1,24 @@
+# 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
+click 640 360
+move 900 360
+frames 300
+screenshot .zig-cache/e2e/live-wave.png
+key g
+frames 1
+screenshot .zig-cache/e2e/live-guides.png
+key g
+click 640 360
+frames 150
+screenshot .zig-cache/e2e/live-damped.png
+key b
+expect backend cpu
+frames 1
+screenshot .zig-cache/e2e/live-damped-cpu.png
+click 640 360
+frames 120
+screenshot .zig-cache/e2e/live-end.png