summaryrefslogtreecommitdiff
path: root/examples/vecanim/scenes/live.zig
blob: 9b94d17a881a6bc7c24466155f35d8e6ba0be9c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
    }
}