| 1 | const std = @import("std"); |
| 2 | |
| 3 | pub const Easing = enum { |
| 4 | linear, |
| 5 | ease_out, |
| 6 | ease_in_out, |
| 7 | |
| 8 | pub fn apply(self: Easing, t: f64) f64 { |
| 9 | return switch (self) { |
| 10 | .linear => t, |
| 11 | .ease_out => 1.0 - std.math.pow(f64, 1.0 - t, 3), |
| 12 | .ease_in_out => if (t < 0.5) |
| 13 | 4.0 * t * t * t |
| 14 | else |
| 15 | 1.0 - std.math.pow(f64, -2.0 * t + 2.0, 3) / 2.0, |
| 16 | }; |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | pub const AnimationConfig = struct { |
| 21 | duration_ms: u64 = 150, |
| 22 | easing: Easing = .ease_out, |
| 23 | }; |
| 24 | |
| 25 | pub const SpringConfig = struct { |
| 26 | stiffness: f64 = 800.0, |
| 27 | damping_ratio: f64 = 1.0, |
| 28 | epsilon: f64 = 0.5, |
| 29 | max_duration_ms: i64 = 2000, |
| 30 | }; |
| 31 | |
| 32 | pub const ScrollAnimation = struct { |
| 33 | start_value: i32 = 0, |
| 34 | end_value: i32 = 0, |
| 35 | start_time: i64 = 0, |
| 36 | duration_ms: u64 = 150, |
| 37 | easing: Easing = .ease_out, |
| 38 | active: bool = false, |
| 39 | spring: bool = false, |
| 40 | velocity: f64 = 0, |
| 41 | spring_config: SpringConfig = .{}, |
| 42 | |
| 43 | pub fn start(self: *ScrollAnimation, io: std.Io, from: i32, to: i32, config: AnimationConfig) void { |
| 44 | if (from == to) { |
| 45 | self.active = false; |
| 46 | return; |
| 47 | } |
| 48 | self.start_value = from; |
| 49 | self.end_value = to; |
| 50 | self.start_time = std.Io.Timestamp.now(io, .awake).toMilliseconds(); |
| 51 | self.duration_ms = config.duration_ms; |
| 52 | self.easing = config.easing; |
| 53 | self.spring = false; |
| 54 | self.active = true; |
| 55 | } |
| 56 | |
| 57 | /// Starts a critically damped spring from `from` to `to` that carries |
| 58 | /// the initial `velocity` (in pixels per second) into the motion. |
| 59 | pub fn startSpring(self: *ScrollAnimation, io: std.Io, from: i32, to: i32, velocity: f64, config: SpringConfig) void { |
| 60 | if (from == to) { |
| 61 | self.active = false; |
| 62 | return; |
| 63 | } |
| 64 | self.start_value = from; |
| 65 | self.end_value = to; |
| 66 | self.start_time = std.Io.Timestamp.now(io, .awake).toMilliseconds(); |
| 67 | self.velocity = velocity; |
| 68 | self.spring_config = config; |
| 69 | self.spring = true; |
| 70 | self.active = true; |
| 71 | } |
| 72 | |
| 73 | fn springPosition(self: *const ScrollAnimation, elapsed_ms: i64) f64 { |
| 74 | const t = @as(f64, @floatFromInt(elapsed_ms)) / 1000.0; |
| 75 | const beta = self.spring_config.damping_ratio * @sqrt(self.spring_config.stiffness); |
| 76 | const to: f64 = @floatFromInt(self.end_value); |
| 77 | const x0 = @as(f64, @floatFromInt(self.start_value)) - to; |
| 78 | return to + @exp(-beta * t) * (x0 + (beta * x0 + self.velocity) * t); |
| 79 | } |
| 80 | |
| 81 | pub fn update(self: *ScrollAnimation, io: std.Io) ?i32 { |
| 82 | if (!self.active) return null; |
| 83 | |
| 84 | const now = std.Io.Timestamp.now(io, .awake).toMilliseconds(); |
| 85 | const elapsed = now - self.start_time; |
| 86 | |
| 87 | if (self.spring) { |
| 88 | const pos = self.springPosition(elapsed); |
| 89 | const settled = @abs(pos - @as(f64, @floatFromInt(self.end_value))) < self.spring_config.epsilon; |
| 90 | if (settled or elapsed > self.spring_config.max_duration_ms) { |
| 91 | self.active = false; |
| 92 | return self.end_value; |
| 93 | } |
| 94 | return @intFromFloat(pos); |
| 95 | } |
| 96 | |
| 97 | if (elapsed >= @as(i64, @intCast(self.duration_ms))) { |
| 98 | self.active = false; |
| 99 | return self.end_value; |
| 100 | } |
| 101 | |
| 102 | const t = @as(f64, @floatFromInt(elapsed)) / @as(f64, @floatFromInt(self.duration_ms)); |
| 103 | const eased = self.easing.apply(t); |
| 104 | const diff = @as(f64, @floatFromInt(self.end_value - self.start_value)); |
| 105 | const current = @as(f64, @floatFromInt(self.start_value)) + (diff * eased); |
| 106 | |
| 107 | return @intFromFloat(current); |
| 108 | } |
| 109 | |
| 110 | pub fn isActive(self: *const ScrollAnimation) bool { |
| 111 | return self.active; |
| 112 | } |
| 113 | |
| 114 | pub fn target(self: *const ScrollAnimation) i32 { |
| 115 | return self.end_value; |
| 116 | } |
| 117 | |
| 118 | pub fn stop(self: *ScrollAnimation) void { |
| 119 | self.active = false; |
| 120 | } |
| 121 | }; |