diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
| commit | 6933f99ddb2d7103515d26838496309a807bba26 (patch) | |
| tree | 991c2fe7d4a57957222876e190fa4d16a0fb889c /examples/vecanim/scenes/intro.zig | |
| parent | 82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff) | |
| download | sketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip | |
Move standalone sketches into sketchbook examples
Diffstat (limited to 'examples/vecanim/scenes/intro.zig')
| -rw-r--r-- | examples/vecanim/scenes/intro.zig | 72 |
1 files changed, 72 insertions, 0 deletions
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; +} |
