From 71e534631bf68f33f802d64794340870374b9f69 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:27:39 -0300 Subject: sketchbook: anim, manim-style scenes as a module (from the game's vecanim), with objects a scene draws itself The player takes its canvas from the room it has and fits the scene's frame into it, plays through and quits when exported, and draws vector objects in runs around custom ones. The renderers' resvg comparison and the timings come along as `zig build diff` and `zig build bench`; the comparison's reference is padded to whole blocks rather than squeezed, which had put Typst text over the limit. Co-Authored-By: Claude Fable 5.2 --- test/scene_compiled.script | 13 ++++++++ test/scene_compiled.zig | 34 ++++++++++++++++++++ test/scene_live.script | 37 +++++++++++++++++++++ test/scene_live.zig | 38 ++++++++++++++++++++++ test/svg/dashes.svg | 6 ++++ test/svg/fill-rules.svg | 6 ++++ test/svg/gradients.svg | 16 ++++++++++ test/svg/opacity.svg | 7 ++++ test/svg/paths.svg | 8 +++++ test/svg/shapes.svg | 8 +++++ test/svg/strokes.svg | 11 +++++++ test/svg/transforms.svg | 13 ++++++++ test/svg/typst-equation.typ | 3 ++ test/svg/typst-shapes.typ | 8 +++++ test/svg/typst-text.typ | 5 +++ test/svg/use.svg | 11 +++++++ test/svgview.zig | 78 +++++++++++++++++++++++++++++++++++++++++++++ 17 files changed, 302 insertions(+) create mode 100644 test/scene_compiled.script create mode 100644 test/scene_compiled.zig create mode 100644 test/scene_live.script create mode 100644 test/scene_live.zig create mode 100644 test/svg/dashes.svg create mode 100644 test/svg/fill-rules.svg create mode 100644 test/svg/gradients.svg create mode 100644 test/svg/opacity.svg create mode 100644 test/svg/paths.svg create mode 100644 test/svg/shapes.svg create mode 100644 test/svg/strokes.svg create mode 100644 test/svg/transforms.svg create mode 100644 test/svg/typst-equation.typ create mode 100644 test/svg/typst-shapes.typ create mode 100644 test/svg/typst-text.typ create mode 100644 test/svg/use.svg create mode 100644 test/svgview.zig (limited to 'test') diff --git a/test/scene_compiled.script b/test/scene_compiled.script new file mode 100644 index 0000000..3c9ef44 --- /dev/null +++ b/test/scene_compiled.script @@ -0,0 +1,13 @@ +# The compiled test scene, through the player, and exported ({frames}: +# 30 fps, 640x360, so half size, played once without the player's controls). +frames 30 +screenshot .zig-cache/anim/compiled-written.png +console set t 1.5 +frames 1 +screenshot .zig-cache/anim/compiled-morph.png +# Frame 68 is 2.27 s in: the dot over the panel's corner, at half size. +expect-pixel {frames}/000068.png 400 240 88 196 221 255 1 +expect-pixel {frames}/000068.png 250 235 42 42 48 255 1 +# No bar of controls along the bottom, and the last frame is the end, 3 s. +expect-pixel {frames}/000068.png 8 352 28 28 31 255 +expect-pixel {frames}/000090.png 0 0 28 28 31 255 diff --git a/test/scene_compiled.zig b/test/scene_compiled.zig new file mode 100644 index 0000000..f4a917c --- /dev/null +++ b/test/scene_compiled.zig @@ -0,0 +1,34 @@ +//! The anim module's test scene, compiled: Typst written and morphed, and +//! a custom object whose data is a constant, shown as far as it is `drawn`. +const anim = @import("anim"); +const sk = @import("sketch"); + +pub const typst_preamble = + \\#set page(width: auto, height: auto, margin: 3pt, fill: none) + \\#set text(fill: white, size: 28pt) +; +pub const scene = anim.Scene(construct); + +/// Lines of code in a panel of 320x120 units around its origin. +const Code = struct { + lines: []const []const u8, + + pub fn draw(c: *const Code, f: *sk.Frame, at: anim.Placed) void { + sk.draw.rect(f, .{ .x = -160, .y = -60, .w = 320, .h = 120 }, at.pen(.{ .fill = .hex(0x2a2a30) })); + const count: f32 = @floatFromInt(c.lines.len); + const shown: usize = @intFromFloat(at.pose.drawn * count); + for (c.lines[0..shown], 0..) |line, i| at.text(f, -150, -50 + 24 * @as(f32, @floatFromInt(i)), 1.5, line, .white); + } +}; +const code: Code = .{ .lines = &.{ "mov rax, 1", "add rax, rbx", "ret" } }; + +fn construct(s: *anim.Builder) void { + const pythagoras = s.typst("$ a^2 + b^2 = c^2 $", .{ .y = -200 }); + const solved = s.typst("$ c = sqrt(a^2 + b^2) $", .{ .y = -200 }); + const panel = s.custom(&code, .{ .y = 60 }); + const dot = s.circle(.{ .r = 20, .x = 160, .y = 120, .fill = anim.blue }); + s.play(.{ pythagoras.write(), panel.write() }, .{ .seconds = 1 }); + s.play(.{ pythagoras.transformInto(solved), dot.fadeIn() }, .{ .seconds = 1 }); + s.wait(0.5); + s.play(.{anim.camera(.{ .zoom = 1.5 })}, .{ .seconds = 0.5 }); +} diff --git a/test/scene_live.script b/test/scene_live.script new file mode 100644 index 0000000..30b2087 --- /dev/null +++ b/test/scene_live.script @@ -0,0 +1,37 @@ +# The live test scene, through the player. It holds at the pause. +frames 150 +expect t 2 +screenshot .zig-cache/anim/live.png +# The custom panel is over the circle added before it, +expect-pixel .zig-cache/anim/live.png 600 330 32 64 96 255 1 +expect-pixel .zig-cache/anim/live.png 450 360 252 98 85 255 1 +# under the square added after it, +expect-pixel .zig-cache/anim/live.png 725 400 131 193 103 255 1 +# and its bar is as full as the value the timeline moved to 0.5. +expect-pixel .zig-cache/anim/live.png 600 390 240 172 95 255 1 +expect-pixel .zig-cache/anim/live.png 680 390 32 64 96 255 1 +# Half way through its move it is half way. +console set playing false +console set t 1.5 +screenshot .zig-cache/anim/live-moving.png +expect-pixel .zig-cache/anim/live-moving.png 400 330 32 64 96 255 1 +expect-pixel .zig-cache/anim/live-moving.png 600 330 252 98 85 255 1 +# The CPU renderer layers the same way. +console set t 2 +key b +expect backend cpu +screenshot .zig-cache/anim/live-cpu.png +expect-pixel .zig-cache/anim/live-cpu.png 600 330 32 64 96 255 1 +expect-pixel .zig-cache/anim/live-cpu.png 725 400 131 193 103 255 1 +key b +# A click continues: the camera zooms to 2 and the panel fades to half, +# over the background where it now reaches. +click 640 360 +expect playing true +frames 70 +screenshot .zig-cache/anim/live-zoom.png +expect-pixel .zig-cache/anim/live-zoom.png 800 300 30 46 64 255 2 +# Guides draw over everything. +key g +expect guides true +screenshot .zig-cache/anim/live-guides.png diff --git a/test/scene_live.zig b/test/scene_live.zig new file mode 100644 index 0000000..cb21b9b --- /dev/null +++ b/test/scene_live.zig @@ -0,0 +1,38 @@ +//! The anim module's test scene, live: a custom object drawn with 2D +//! commands between two vector objects, on the same timeline and camera. +const anim = @import("anim"); +const sk = @import("sketch"); + +pub const typst = struct { + pub const title = "#text(fill: white, size: 30pt)[registers]"; +}; +pub const scene = anim.Live(frame, typst); + +/// A panel of 200x100 units around its origin, with a label and a bar. +const Panel = struct { + label: []const u8, + /// How much of the bar is full, 0 to 1. + level: f32, + + pub fn draw(p: *const Panel, f: *sk.Frame, at: anim.Placed) void { + sk.draw.rect(f, .{ .x = -100, .y = -50, .w = 200, .h = 100 }, at.pen(.{ .fill = .hex(0x204060) })); + sk.draw.rect(f, .{ .x = -100, .y = 10, .w = 200 * p.level, .h = 40 }, at.pen(.{ .fill = .hex(0xf0ac5f) })); + at.text(f, -92, -42, 1.5, p.label, .white); + } +}; + +fn frame(s: *anim.Frame) void { + const title = s.typst(typst.title, .{ .y = -250 }); + // Added in this order, shown in this order: circle, panel, square. + const under = s.circle(.{ .r = 120, .x = -100, .fill = anim.red }); + const panel = s.new(Panel{ .label = "rax", .level = 0 }); + const table = s.custom(panel, .{ .x = -300 }); + const over = s.rect(.{ .w = 60, .h = 60, .x = 100, .y = 50, .fill = anim.green }); + const level = s.value(0); + s.play(.{ title.fadeIn(), under.fadeIn(), table.fadeIn(), over.fadeIn() }, .{ .seconds = 1 }); + s.play(.{ table.move(.{ .x = 0 }), level.to(0.5) }, .{ .seconds = 1 }); + s.pause(); + s.play(.{ anim.camera(.{ .zoom = 2 }), table.move(.{ .opacity = 0.5 }) }, .{ .seconds = 1 }); + // After the plays: the value as it stands now. + panel.level = s.get(level); +} diff --git a/test/svg/dashes.svg b/test/svg/dashes.svg new file mode 100644 index 0000000..ebd9ce6 --- /dev/null +++ b/test/svg/dashes.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/svg/fill-rules.svg b/test/svg/fill-rules.svg new file mode 100644 index 0000000..af0e3b8 --- /dev/null +++ b/test/svg/fill-rules.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/svg/gradients.svg b/test/svg/gradients.svg new file mode 100644 index 0000000..a5f2c81 --- /dev/null +++ b/test/svg/gradients.svg @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/test/svg/opacity.svg b/test/svg/opacity.svg new file mode 100644 index 0000000..c0d0b8a --- /dev/null +++ b/test/svg/opacity.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/test/svg/paths.svg b/test/svg/paths.svg new file mode 100644 index 0000000..173ec9e --- /dev/null +++ b/test/svg/paths.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/svg/shapes.svg b/test/svg/shapes.svg new file mode 100644 index 0000000..07aa01a --- /dev/null +++ b/test/svg/shapes.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/svg/strokes.svg b/test/svg/strokes.svg new file mode 100644 index 0000000..6a25a16 --- /dev/null +++ b/test/svg/strokes.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/test/svg/transforms.svg b/test/svg/transforms.svg new file mode 100644 index 0000000..0eb7756 --- /dev/null +++ b/test/svg/transforms.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/test/svg/typst-equation.typ b/test/svg/typst-equation.typ new file mode 100644 index 0000000..da0c34c --- /dev/null +++ b/test/svg/typst-equation.typ @@ -0,0 +1,3 @@ +#set page(width: auto, height: auto, margin: 6pt, fill: none) +#set text(fill: rgb("#ece6e2"), size: 28pt) +$ integral_0^infinity e^(-x^2) dif x = sqrt(pi) / 2 $ diff --git a/test/svg/typst-shapes.typ b/test/svg/typst-shapes.typ new file mode 100644 index 0000000..b9bcdff --- /dev/null +++ b/test/svg/typst-shapes.typ @@ -0,0 +1,8 @@ +#set page(width: auto, height: auto, margin: 6pt, fill: none) +#stack(dir: ltr, spacing: 8pt, + rect(width: 60pt, height: 30pt, stroke: 2pt + red, fill: blue, radius: 5pt), + circle(radius: 16pt, fill: gradient.linear(red, blue)), + circle(radius: 16pt, fill: gradient.radial(yellow, purple)), + line(length: 60pt, stroke: (paint: green, thickness: 3pt, dash: "dashed")), + polygon(fill: teal, stroke: 1pt + white, (0pt, 20pt), (20pt, 0pt), (40pt, 20pt)), +) diff --git a/test/svg/typst-text.typ b/test/svg/typst-text.typ new file mode 100644 index 0000000..ce6281d --- /dev/null +++ b/test/svg/typst-text.typ @@ -0,0 +1,5 @@ +#set page(width: 300pt, height: auto, margin: 6pt, fill: none) +#set text(fill: rgb("#ece6e2"), size: 12pt) += Vector sketches +The quick brown fox jumps over the lazy dog. *Bold*, _italic_, `mono`, and +Unicode: é ß ñ — “quotes” — 1 ≤ 2 → ∞. diff --git a/test/svg/use.svg b/test/svg/use.svg new file mode 100644 index 0000000..5336f01 --- /dev/null +++ b/test/svg/use.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/test/svgview.zig b/test/svgview.zig new file mode 100644 index 0000000..0675c83 --- /dev/null +++ b/test/svgview.zig @@ -0,0 +1,78 @@ +//! svgview: shows one SVG from a list, fitted to its own canvas size, with +//! either renderer. The differential tests drive it headless: set `index` +//! and `backend` from the console, screenshot, compare with resvg. +//! +//! The list is `$SVGVIEW_LIST` (default `svgview.txt`): one `path width +//! height` per line. +const std = @import("std"); +const sk = @import("sketch"); +const vector = @import("vector"); + +pub const name = "svgview"; +pub const width = 512; +pub const height = 512; + +pub const State = struct { + index: u32 = 0, + backend: vector.Backend = .gpu, +}; + +pub const Session = struct { + gpu: ?vector.Renderer(.gpu) = null, + cpu: ?vector.Renderer(.cpu) = null, +}; + +const Entry = struct { path: []const u8, w: u32, h: u32 }; + +fn entry(host: sk.Host, index: u32) ?Entry { + const list = host.readFile(host.env("SVGVIEW_LIST") orelse "svgview.txt"); + var lines = std.mem.tokenizeScalar(u8, list, '\n'); + var i: u32 = 0; + while (lines.next()) |line| : (i += 1) { + if (i != index) continue; + var words = std.mem.tokenizeScalar(u8, line, ' '); + const path = words.next() orelse return null; + const w = std.fmt.parseInt(u32, words.next() orelse return null, 10) catch return null; + const h = std.fmt.parseInt(u32, words.next() orelse return null, 10) catch return null; + return .{ .path = path, .w = w, .h = h }; + } + return null; +} + +pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + const e = entry(f.host, s.index) orelse { + f.clear(0x400000); + f.text(8, 8, 2, "no such entry", 0xffffff); + return; + }; + f.canvas(e.w, e.h); + show(s, ss, f, e) catch |err| { + f.clear(0x400000); + f.print(8, 8, 2, 0xffffff, "{s}: {t}", .{ e.path, err }); + }; +} + +fn show(s: *const State, ss: *Session, f: *sk.Frame, e: Entry) !void { + const d = try vector.svg.parse(f.scratch, f.host.readFile(e.path)); + // As resvg -w does: scale to the width, from the top-left corner (the + // height is rounded, so fitting both would drift by a fraction). + const s_ = @as(f32, @floatFromInt(e.w)) / d.view.w; + const place = vector.geom.Affine.scale(s_, s_).mul(.translate(-d.view.x, -d.view.y)); + switch (s.backend) { + .gpu => try render(&ss.gpu, f, d, place, e), + .cpu => try render(&ss.cpu, f, d, place, e), + } +} + +fn render(slot: anytype, f: *sk.Frame, d: vector.Drawing, place: vector.geom.Affine, e: Entry) !void { + if (slot.*) |*r| if (r.width() != e.w or r.height() != e.h) { + r.deinit(f.host); + slot.* = null; + }; + if (slot.* == null) slot.* = try .init(f.session, f.host, e.w, e.h); + const r = &slot.*.?; + // resvg's `--background '#808080'`. + r.begin(f, .{ 128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1 }); + try r.draw(d, place, 1); + r.present(f, sk.Rect{ .w = @floatFromInt(e.w), .h = @floatFromInt(e.h) }); +} -- cgit v1.3