summaryrefslogtreecommitdiff
path: root/src/animation.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/animation.zig')
-rw-r--r--src/animation.zig137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/animation.zig b/src/animation.zig
index ae6f9ca0..764aa0b3 100644
--- a/src/animation.zig
+++ b/src/animation.zig
@@ -949,6 +949,143 @@ test "a flavour's stretched pane lands exactly on its box, and a click finds the
}
}
+/// A view that follows its input (anim 9, 11): the picture's offset from
+/// the core's position now (in the view's own unit: rows of text, pixels of
+/// a PDF strip; negative, the picture still shows what is above), its speed
+/// (units a second), when it was last integrated, and what has been feeding
+/// it. The text body (the GUI's Glide) and a PDF strip are each one.
+pub const Follow = struct {
+ offset: f32 = 0,
+ speed: f32 = 0,
+ at_ns: u64 = 0,
+ /// The last inputs (down positive), their spacing, the last's time, and
+ /// how many came as one stream.
+ feed: struct { amount: f32 = 0, gap_ns: u64 = 0, last_ns: u64 = 0, count: u16 = 0 } = .{},
+
+ /// How far the picture may lag, in bodies (a flick past that jumps);
+ /// how fast it may move, in bodies a second, so moving text stays
+ /// readable.
+ pub const reach = 2;
+ pub const bodies_per_s: f32 = 15;
+ /// Inputs this close, each the way the last went, are one stream.
+ pub const stream_ns = 150 * std.time.ns_per_ms;
+
+ /// What a view's size and the flavour make of the rules: the spring at
+ /// 1.5x the flavour's pace, the speed cap, the lag cap, and the offset
+ /// under which it is at rest (a tenth of a pixel, in its unit).
+ pub const Pace = struct { omega: f32, cap: f32, limit: f32, rest: f32 };
+
+ pub fn pace(motion: Motion, body: f32, rest: f32) Pace {
+ return .{ .omega = motion.omega * 1.5, .cap = body * bodies_per_s, .limit = body * reach, .rest = rest };
+ }
+
+ pub fn moving(follow: *const Follow) bool {
+ return follow.offset != 0 or follow.speed != 0;
+ }
+
+ /// An input: the core moved `amount` (down positive). The picture now
+ /// lags by as much more, its speed as it was.
+ pub fn kick(follow: *Follow, amount: f32, now_ns: u64, p: Pace) void {
+ follow.advance(now_ns, p);
+ follow.offset = std.math.clamp(follow.offset - amount, -p.limit, p.limit);
+ const f = &follow.feed;
+ const like = f.count > 0 and (amount > 0) == (f.amount > 0) and now_ns -| f.last_ns < stream_ns;
+ if (like) {
+ const gap = now_ns -| f.last_ns;
+ f.gap_ns = if (f.count == 1) gap else (f.gap_ns + gap) / 2;
+ f.amount = (f.amount + amount) / 2;
+ f.count +|= 1;
+ } else f.* = .{ .amount = amount, .count = 1 };
+ f.last_ns = now_ns;
+ }
+
+ /// The system moved on to `now_ns`. Fed by a stream, at the stream's
+ /// rate (an input's amount over their spacing), held a lag `lead`
+ /// behind so a late input never finds it stopped and the stream's end
+ /// never runs it past its mark; else on the spring, critically damped,
+ /// from the speed it has. Never faster than `p.cap`.
+ pub fn advance(follow: *Follow, now_ns: u64, p: Pace) void {
+ const dt: f32 = @floatCast(@as(f64, @floatFromInt(@min(now_ns -| follow.at_ns, 2 * frame_ns))) / std.time.ns_per_s);
+ follow.at_ns = now_ns;
+ if (follow.offset == 0 and follow.speed == 0) return;
+ const omega = p.omega;
+ const toward: f32 = if (follow.offset < 0) 1 else -1;
+ const f = follow.feed;
+ if (f.count >= 2 and f.gap_ns > 0 and now_ns -| f.last_ns < f.gap_ns * 3 / 2) {
+ const gap: f32 = @floatCast(@as(f64, @floatFromInt(f.gap_ns)) / std.time.ns_per_s);
+ const rate = @abs(f.amount) / gap;
+ const lead = @max(@abs(f.amount), rate / omega);
+ // ponytail: a gentle pull to the lead (a tenth an input), so the
+ // per-frame step varies by a tenth; a stream's own jitter beyond
+ // that shows. An estimate of the input clock if it ever matters.
+ // A slow stream's lag runs out before its next input: never
+ // faster than the spring's own pace from there (omega a unit of
+ // lag a second), so it eases onto the mark, not into it at speed.
+ const v = std.math.clamp(rate + 0.1 / gap * (@abs(follow.offset) - lead), 0, @min(p.cap, omega * @abs(follow.offset)));
+ const step = @min(v * dt, @abs(follow.offset));
+ follow.offset += toward * step;
+ follow.speed = if (follow.offset == 0) 0 else toward * v;
+ return;
+ }
+ const x = follow.offset;
+ // Coming to rest from a stream: never faster than the spring's own
+ // pace from here (omega a unit of lag a second), so it eases in and
+ // does not pass its mark.
+ if (follow.speed * x < 0 and @abs(follow.speed) > omega * @abs(x)) follow.speed = std.math.sign(follow.speed) * omega * @abs(x);
+ const lead_v = follow.speed + omega * x;
+ const decay = @exp(-omega * dt);
+ var next = (x + lead_v * dt) * decay;
+ var speed = (follow.speed - omega * dt * lead_v) * decay;
+ if (@abs(next - x) > p.cap * dt) {
+ next = x + std.math.sign(next - x) * p.cap * dt;
+ speed = std.math.sign(next - x) * p.cap;
+ }
+ follow.offset = next;
+ follow.speed = speed;
+ if (@abs(next) < p.rest and @abs(speed) < 10 * p.rest) follow.* = .{ .at_ns = now_ns };
+ }
+};
+
+test "a follow comes to rest without a snap: past its fastest frame each step is shorter, the last under a tenth of a pixel" {
+ // A flick's spin (a stream of notches, then nothing), one page, and
+ // notches slow enough that each lag runs out before the next (its
+ // stream must ease onto each mark, never pass it).
+ const p = Follow.pace(.{}, 600, 0.1);
+ for ([_]struct { n: u16, gap_ms: u64 }{ .{ .n = 12, .gap_ms = 16 }, .{ .n = 1, .gap_ms = 16 }, .{ .n = 6, .gap_ms = 144 } }) |run| {
+ var f: Follow = .{};
+ var now: u64 = std.time.ns_per_s;
+ for (0..run.n) |_| {
+ f.kick(if (run.n == 1) 600 else 25, now, p);
+ var waited: u64 = 0;
+ while (waited < run.gap_ms) : (waited += 8) {
+ now += 8 * std.time.ns_per_ms;
+ const was = f.offset;
+ f.advance(now, p);
+ // Toward its mark only, and onto it from under a pixel.
+ try std.testing.expect(f.offset <= 0 and f.offset >= was);
+ if (f.offset == 0) try std.testing.expect(-was <= 3 * p.rest);
+ }
+ }
+ var last: f32 = std.math.inf(f32);
+ var before = f.offset;
+ var frames: usize = 0;
+ var fastest: f32 = 0;
+ while (f.moving()) : (frames += 1) {
+ now += frame_ns;
+ f.advance(now, p);
+ const step = @abs(f.offset - before);
+ try std.testing.expect(@abs(f.offset) <= @abs(before));
+ if (f.offset == 0) try std.testing.expect(step <= 3 * p.rest);
+ fastest = @max(fastest, step);
+ if (step < fastest) try std.testing.expect(step <= last);
+ last = step;
+ before = f.offset;
+ try std.testing.expect(frames < 120);
+ }
+ try std.testing.expect(last <= 0.1);
+ }
+}
+
/// The focused cursor's quad (docs/render-pipeline.md ยง5.4), in grid cells:
/// four corners, each on its own springs, so a jump smears along its path
/// (the leading corners stiffer than the trailing ones, Motion.follow) and