summaryrefslogtreecommitdiff
path: root/examples/talk.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/talk.zig')
-rw-r--r--examples/talk.zig160
1 files changed, 160 insertions, 0 deletions
diff --git a/examples/talk.zig b/examples/talk.zig
new file mode 100644
index 0000000..2ad3d70
--- /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 (e.key()) {
+ .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);
+}