animation
Garnet Animation
garnet-animation provides renderer-independent, explicit-time animation over garnet-signals. Tracks are immutable functions of time; binding a track to a time signal creates a lazy computed value.
require "garnet-animation"
time = Garnet::Signals::State.new(0.seconds)
track = Garnet::Animation::Track(Float32).new([
Garnet::Animation::Keyframe.new(
0.seconds,
0_f32,
Garnet::Animation::CubicBezier.new(0.42, 0.0, 0.58, 1.0)
),
Garnet::Animation::Keyframe.new(1.second, 100_f32),
]) { |from, to, amount| from + (to - from) * amount }
x = track.bind(time)
time.set(500.milliseconds)
puts x.get
Tracks clamp outside their keyframe range and support linear, hold, and cubic Bezier timing. Callers provide interpolation, keeping the core useful for any value type without depending on graphics or math shards. Tracks.float32 and Tracks.float64 provide the common scalar interpolation forms.
Tween(T) is an owner-driven automation that writes sampled values into a Signals::State(T). It owns elapsed time but no clock or update loop; game code, an ECS system, or a future timeline advances it explicitly. Seeking and resetting produce the same result for the same time.
Timeline owns an explicit Signals::State(Time::Span) and supports once, loop, and ping-pong playback. It advances only when its owner calls advance; seeking is deterministic and does not depend on wall-clock time. Construct a timeline with an Automation, or call attach, to publish imperative target updates in the same signal batch as its time value.
Sequence, Parallel, and finite Repeat compose any Automation, including tweens and other groups. Seeking a group updates all affected states inside one signal batch, so observers never see a partially evaluated pose.
Use EventTrack(T) for typed markers such as sound cues or gameplay events. timeline.advance(delta, markers) updates the timeline and returns crossed markers in playback order, including wraps and ping-pong turns. The interval excludes its starting endpoint and includes its ending endpoint. seek and reset change the playhead without dispatching markers. Systems with their own playhead can call EventTrack#crossed with unwrapped time directly.
Easing and interpolation helpers
Ease names the standard curves: Linear, Step, the In/Out/InOut variants of Sine, Quad, Cubic, Quart, Quint, Expo, Circ, Back, Elastic and Bounce, plus Smoothstep and Smootherstep. Symbols autocast, so APIs accept ease: :out_cubic. Ease#apply(amount) evaluates a curve, and Ease#easing returns a shared Easing for keyframes:
Garnet::Animation::Ease::OutCubic.apply(0.5) # => 0.875
Garnet::Animation::Keyframe.new(0.seconds, 0_f32, Garnet::Animation::Ease::InOutSine.easing)
Animation.lerp(from, to, amount) interpolates floats, integers (rounded), types with slerp (quaternions), types with lerp(other, Float32) (such as Garnet::Color) and types with +, - and * Float32 (math vectors). Tracks.lerp(keyframes) builds a track for any of them. inverse_lerp, remap, smoothstep and smootherstep replace hand-written helpers:
Garnet::Animation.smoothstep(0, 10, 5) # => 0.5
Garnet::Animation.remap(5, 0, 10, 100, 200) # => 150.0
ValueTween(T) is a Tween that passes each sample to a block instead of a Signals::State, so a Timeline, Sequence or Parallel can drive plain objects. For ECS components, use the tweens in garnet-ecs/gameplay.
animation
- 0
- 0
- 0
- 4
- 1
- 1 day ago
- November 30, 2026
MIT License
Wed, 02 Dec 2026 02:09:20 GMT