Documentation
¶
Overview ¶
Package animation provides an enterprise-grade animation engine for gogpu/ui.
The engine drives [state.Signal] values through time-based tweens and physics-based springs, integrating with the reactive signal system for automatic widget invalidation and event-driven rendering.
Architecture ¶
The animation engine follows the Flutter pattern:
- Engine ONLY animates float32 signals
- Tween maps float32 progress to any type T (Color, Point, Size, etc.)
- Controller manages active animations and provides the Tick entry point
Tween Animation ¶
ctrl := animation.NewController()
opacity := state.NewSignal[float32](0)
animation.To(opacity, 1.0).
Duration(300 * time.Millisecond).
Ease(animation.M3Standard).
Start(ctrl)
Spring Animation ¶
position := state.NewSignal[float32](0)
animation.SpringTo(position, 200.0).
Stiffness(animation.StiffnessMedium).
DampingRatio(animation.DampingNoBouncy).
Start(ctrl)
Type Interpolation with Tween[T] ¶
progress := state.NewSignal[float32](0) animation.To(progress, 1.0).Duration(300*time.Millisecond).Start(ctrl) colorTween := animation.NewColorTween(startColor, endColor) currentColor := colorTween.At(progress.Get()) // in Draw
Composition ¶
animation.NewSequence(anim1, anim2).Start(ctrl) animation.NewParallel(anim1, anim2).Start(ctrl)
Frame Integration ¶
// In OnDraw callback:
active := ctrl.Tick(dt) // updates signals
scheduler.Flush() // picks up changes
window.DrawTo(canvas) // renders
if active { RequestRedraw() }
Index ¶
- Constants
- Variables
- func LerpColor(begin, end widget.Color, t float32) widget.Color
- func LerpFloat32(begin, end, t float32) float32
- func LerpPoint(begin, end geometry.Point, t float32) geometry.Point
- func LerpSize(begin, end geometry.Size, t float32) geometry.Size
- type Animation
- type AnimationBuilder
- func FadeIn(signal signalFloat32, duration time.Duration) *AnimationBuilder
- func FadeOut(signal signalFloat32, duration time.Duration) *AnimationBuilder
- func ScaleIn(signal signalFloat32, duration time.Duration) *AnimationBuilder
- func ScaleOut(signal signalFloat32, duration time.Duration) *AnimationBuilder
- func SlideInFromBottom(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
- func SlideInFromLeft(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
- func SlideInFromRight(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
- func SlideInFromTop(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
- func SnackbarEnter(translateY signalFloat32, distance float32) *AnimationBuilder
- func SnackbarExit(opacity signalFloat32) *AnimationBuilder
- func To(signal signalFloat32, target float32) *AnimationBuilder
- func (b *AnimationBuilder) AutoReverse() *AnimationBuilder
- func (b *AnimationBuilder) Build() *Animation
- func (b *AnimationBuilder) Delay(d time.Duration) *AnimationBuilder
- func (b *AnimationBuilder) Duration(d time.Duration) *AnimationBuilder
- func (b *AnimationBuilder) Ease(e Easing) *AnimationBuilder
- func (b *AnimationBuilder) From(value float32) *AnimationBuilder
- func (b *AnimationBuilder) OnDone(fn func()) *AnimationBuilder
- func (b *AnimationBuilder) Repeat(count int) *AnimationBuilder
- func (b *AnimationBuilder) Start(ctrl *Controller) *Animation
- type Controller
- type Easing
- type LerpFunc
- type ParallelBuilder
- func DialogEnter(opacity, scale signalFloat32) *ParallelBuilder
- func DialogExit(opacity, scale signalFloat32) *ParallelBuilder
- func Group(animations ...Startable) *ParallelBuilder
- func MenuEnter(opacity, translateY signalFloat32, distance float32) *ParallelBuilder
- func MenuExit(opacity, translateY signalFloat32, distance float32) *ParallelBuilder
- func NewParallel(items ...Startable) *ParallelBuilder
- func Stagger(delay time.Duration, animations ...Startable) *ParallelBuilder
- type RepeatingBuilder
- type SequenceBuilder
- type Spring
- type SpringBuilder
- func (b *SpringBuilder) Build() *Spring
- func (b *SpringBuilder) DampingRatio(ratio float32) *SpringBuilder
- func (b *SpringBuilder) InitialVelocity(v float32) *SpringBuilder
- func (b *SpringBuilder) Mass(m float32) *SpringBuilder
- func (b *SpringBuilder) OnDone(fn func()) *SpringBuilder
- func (b *SpringBuilder) RestDelta(d float32) *SpringBuilder
- func (b *SpringBuilder) RestSpeed(s float32) *SpringBuilder
- func (b *SpringBuilder) Start(ctrl *Controller) *Spring
- func (b *SpringBuilder) Stiffness(k float32) *SpringBuilder
- type Startable
- type Tween
- func NewColorTween(begin, end widget.Color) *Tween[widget.Color]
- func NewFloat32Tween(begin, end float32) *Tween[float32]
- func NewPointTween(begin, end geometry.Point) *Tween[geometry.Point]
- func NewSizeTween(begin, end geometry.Size) *Tween[geometry.Size]
- func NewTween[T any](begin, end T, lerpFn LerpFunc[T]) *Tween[T]
Constants ¶
const ( // DurationShort1 is for micro-interactions (icon state changes). DurationShort1 = 50 * time.Millisecond // DurationShort2 is for small element fades. DurationShort2 = 100 * time.Millisecond // DurationShort3 is for button press feedback. DurationShort3 = 150 * time.Millisecond // DurationShort4 is for checkbox and switch toggles. DurationShort4 = 200 * time.Millisecond // DurationMedium1 is for card expand and small transitions. DurationMedium1 = 250 * time.Millisecond // DurationMedium2 is for dialog appear and menu open. DurationMedium2 = 300 * time.Millisecond // DurationMedium3 is for medium area transitions. DurationMedium3 = 350 * time.Millisecond // DurationMedium4 is for navigation transitions. DurationMedium4 = 400 * time.Millisecond // DurationLong1 is for large area transitions. DurationLong1 = 450 * time.Millisecond // DurationLong2 is for full-screen transitions. DurationLong2 = 500 * time.Millisecond // DurationLong3 is for complex multi-element transitions. DurationLong3 = 550 * time.Millisecond // DurationLong4 is for large complex transitions. DurationLong4 = 600 * time.Millisecond // DurationExtraLong1 is for extended transitions. DurationExtraLong1 = 700 * time.Millisecond // DurationExtraLong2 is for extended complex transitions. DurationExtraLong2 = 800 * time.Millisecond // DurationExtraLong3 is for very large transitions. DurationExtraLong3 = 900 * time.Millisecond // DurationExtraLong4 is the maximum standard duration. DurationExtraLong4 = 1000 * time.Millisecond )
Material Design 3 duration tokens.
Duration increases with animation area and traversal distance. Values match M3 specification exactly.
const ( // DampingHighBouncy produces very bouncy springs with many oscillations. DampingHighBouncy float32 = 0.2 // DampingMediumBouncy produces medium bounce springs. DampingMediumBouncy float32 = 0.5 // DampingLowBouncy produces springs with slight bounce. DampingLowBouncy float32 = 0.75 // DampingNoBouncy produces critically damped springs with no bounce. DampingNoBouncy float32 = 1.0 )
Spring damping ratio presets (from Jetpack Compose).
const ( // StiffnessHigh produces very fast springs. StiffnessHigh float32 = 10000 // StiffnessMedium is the default balanced stiffness. StiffnessMedium float32 = 1500 // StiffnessLow produces slow, gentle springs. StiffnessLow float32 = 200 // StiffnessVeryLow produces very slow, dramatic springs. StiffnessVeryLow float32 = 50 )
Spring stiffness presets (from Jetpack Compose).
Variables ¶
var ( // M3Standard is the standard M3 easing for utility animations that // begin and end on screen. M3Standard = CubicBezier(0.2, 0.0, 0.0, 1.0) // M3StandardAccelerate is the M3 easing for utility animations // exiting the screen. M3StandardAccelerate = CubicBezier(0.3, 0.0, 1.0, 1.0) // M3StandardDecelerate is the M3 easing for utility animations // entering the screen. M3StandardDecelerate = CubicBezier(0.0, 0.0, 0.0, 1.0) // M3Emphasized is the primary M3 easing for common animations on screen. // This is a two-segment curve (ThreePointCubic) matching Flutter's exact // easeInOutCubicEmphasized definition. M3Emphasized = ThreePointCubic( [2]float32{0.05, 0}, [2]float32{0.133333, 0.06}, [2]float32{0.166666, 0.4}, [2]float32{0.208333, 0.82}, [2]float32{0.25, 1.0}, ) // M3EmphasizedAccelerate is the M3 easing for styled animations // exiting the screen. M3EmphasizedAccelerate = CubicBezier(0.3, 0.0, 0.8, 0.15) // M3EmphasizedDecelerate is the M3 easing for styled animations // entering the screen. M3EmphasizedDecelerate = CubicBezier(0.05, 0.7, 0.1, 1.0) // M3Linear is the M3 linear easing for simple, non-styled motion. M3Linear = Linear )
Material Design 3 easing curves.
These match the exact values from M3 specification, Flutter's Easing class, and Android's MotionTokens.kt.
var ( // EasingStandard is the standard M3 easing for utility animations // that begin and end on screen. Alias for [M3Standard]. EasingStandard = M3Standard // EasingStandardDecelerate is the M3 easing for utility animations // entering the screen. Alias for [M3StandardDecelerate]. EasingStandardDecelerate = M3StandardDecelerate // EasingStandardAccelerate is the M3 easing for utility animations // exiting the screen. Alias for [M3StandardAccelerate]. EasingStandardAccelerate = M3StandardAccelerate // EasingEmphasized is the primary M3 easing for common animations // on screen. Alias for [M3Emphasized]. EasingEmphasized = M3Emphasized // EasingEmphasizedDecelerate is the M3 easing for styled animations // entering the screen. Alias for [M3EmphasizedDecelerate]. EasingEmphasizedDecelerate = M3EmphasizedDecelerate // EasingEmphasizedAccelerate is the M3 easing for styled animations // exiting the screen. Alias for [M3EmphasizedAccelerate]. EasingEmphasizedAccelerate = M3EmphasizedAccelerate )
Preset easing aliases for convenience.
These are shorthand aliases for the M3 easing curves defined in m3.go, using names that match the Material Design 3 motion specification directly.
Functions ¶
func LerpFloat32 ¶
LerpFloat32 linearly interpolates between two float32 values.
Types ¶
type Animation ¶
type Animation struct {
// contains filtered or unexported fields
}
Animation represents a running tween animation that drives a Signal[float32].
Animations are created with the To builder and started with AnimationBuilder.Start. The animation updates its target signal each frame via the Controller's Tick method.
type AnimationBuilder ¶
type AnimationBuilder struct {
// contains filtered or unexported fields
}
AnimationBuilder constructs an Animation using the builder pattern.
Example:
animation.To(opacity, 1.0).
From(0.0).
Duration(300 * time.Millisecond).
Ease(animation.M3Standard).
Start(ctrl)
func FadeIn ¶
func FadeIn(signal signalFloat32, duration time.Duration) *AnimationBuilder
FadeIn creates a tween animation builder that animates opacity from 0 to 1.
Uses M3 standard decelerate easing (entering). The returned builder targets the provided signal and can be further configured before starting.
Example:
opacity := state.NewSignal[float32](0) animation.FadeIn(opacity, animation.DurationMedium2).Start(ctrl)
func FadeOut ¶
func FadeOut(signal signalFloat32, duration time.Duration) *AnimationBuilder
FadeOut creates a tween animation builder that animates opacity from 1 to 0.
Uses M3 standard accelerate easing (exiting). The returned builder targets the provided signal and can be further configured before starting.
Example:
opacity := state.NewSignal[float32](1) animation.FadeOut(opacity, animation.DurationMedium2).Start(ctrl)
func ScaleIn ¶
func ScaleIn(signal signalFloat32, duration time.Duration) *AnimationBuilder
ScaleIn creates a tween animation builder that animates scale from 0.8 to 1.0 (growing into view).
Uses M3 emphasized decelerate easing (entering).
Example:
scale := state.NewSignal[float32](0.8) animation.ScaleIn(scale, animation.DurationMedium2).Start(ctrl)
func ScaleOut ¶
func ScaleOut(signal signalFloat32, duration time.Duration) *AnimationBuilder
ScaleOut creates a tween animation builder that animates scale from 1.0 to 0.8 (shrinking out of view).
Uses M3 emphasized accelerate easing (exiting).
func SlideInFromBottom ¶
func SlideInFromBottom(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
SlideInFromBottom creates a tween animation builder that animates a vertical offset from +distance to 0 (entering from below).
Uses M3 emphasized decelerate easing (entering).
Example:
translateY := state.NewSignal[float32](100) animation.SlideInFromBottom(translateY, 100, animation.DurationMedium2).Start(ctrl)
func SlideInFromLeft ¶
func SlideInFromLeft(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
SlideInFromLeft creates a tween animation builder that animates a horizontal offset from -distance to 0 (entering from the left).
Uses M3 emphasized decelerate easing (entering).
func SlideInFromRight ¶
func SlideInFromRight(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
SlideInFromRight creates a tween animation builder that animates a horizontal offset from +distance to 0 (entering from the right).
Uses M3 emphasized decelerate easing (entering).
func SlideInFromTop ¶
func SlideInFromTop(signal signalFloat32, distance float32, duration time.Duration) *AnimationBuilder
SlideInFromTop creates a tween animation builder that animates a vertical offset from -distance to 0 (entering from above).
Uses M3 emphasized decelerate easing (entering).
func SnackbarEnter ¶
func SnackbarEnter(translateY signalFloat32, distance float32) *AnimationBuilder
SnackbarEnter creates an animation for snackbar enter transitions.
The animation slides the snackbar in from the bottom, matching the Material Design 3 snackbar pattern. Uses M3 emphasized decelerate easing with DurationMedium1 (250ms).
Parameters:
- translateY: signal driven from +distance to 0
- distance: the slide distance in pixels
func SnackbarExit ¶
func SnackbarExit(opacity signalFloat32) *AnimationBuilder
SnackbarExit creates an animation for snackbar exit transitions.
The animation fades the snackbar out, matching the Material Design 3 snackbar pattern. Uses M3 standard accelerate easing with DurationShort4 (200ms).
Parameters:
- opacity: signal driven from 1 to 0
func To ¶
func To(signal signalFloat32, target float32) *AnimationBuilder
To creates a new AnimationBuilder that animates the signal to the target value.
If From is not called, the animation starts from the signal's current value at the time Start is called.
func (*AnimationBuilder) AutoReverse ¶
func (b *AnimationBuilder) AutoReverse() *AnimationBuilder
AutoReverse makes the animation reverse direction after each iteration.
func (*AnimationBuilder) Build ¶
func (b *AnimationBuilder) Build() *Animation
Build returns the configured Animation without starting it.
If From was not called, the from value is set to the signal's current value.
func (*AnimationBuilder) Delay ¶
func (b *AnimationBuilder) Delay(d time.Duration) *AnimationBuilder
Delay sets a delay before the animation starts.
func (*AnimationBuilder) Duration ¶
func (b *AnimationBuilder) Duration(d time.Duration) *AnimationBuilder
Duration sets the animation duration. Default is 300ms (M3 Medium2).
func (*AnimationBuilder) Ease ¶
func (b *AnimationBuilder) Ease(e Easing) *AnimationBuilder
Ease sets the easing function. Default is M3Standard.
func (*AnimationBuilder) From ¶
func (b *AnimationBuilder) From(value float32) *AnimationBuilder
From sets the starting value. If not called, starts from signal.Get().
func (*AnimationBuilder) OnDone ¶
func (b *AnimationBuilder) OnDone(fn func()) *AnimationBuilder
OnDone sets a callback invoked when the animation completes. Not called if the animation is canceled.
func (*AnimationBuilder) Repeat ¶
func (b *AnimationBuilder) Repeat(count int) *AnimationBuilder
Repeat sets the number of additional repetitions.
0 means play once (default). Pass -1 for infinite repetition.
func (*AnimationBuilder) Start ¶
func (b *AnimationBuilder) Start(ctrl *Controller) *Animation
Start builds the animation and registers it with the controller.
If From was not called, the animation starts from the signal's current value. If another animation is already running on the same signal, it is auto-canceled.
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
Controller manages active animations and provides the Tick entry point.
The Controller owns a map of signal -> active animation for auto-cancel. When a new animation targets a signal that already has an active animation, the old animation is automatically canceled.
Controller is designed to be owned by a window and accessed via widget.Context. It is NOT thread-safe; all calls must happen on the UI thread.
func NewController ¶
func NewController() *Controller
NewController creates a new animation controller.
func (*Controller) Cancel ¶
func (c *Controller) Cancel(signal signalFloat32)
Cancel stops the animation targeting the given signal.
The signal parameter should be the same signal passed to To() or SpringTo().
func (*Controller) CancelAll ¶
func (c *Controller) CancelAll()
CancelAll stops all animations immediately without calling OnDone callbacks.
func (*Controller) HasActive ¶
func (c *Controller) HasActive() bool
HasActive reports whether any animations are running.
type Easing ¶
Easing maps normalized time [0,1] to animation progress [0,1].
The input t is clamped to [0,1] by the animation engine before calling. The output may exceed [0,1] for overshoot effects (e.g., back easing).
var ( // Linear returns t unchanged (constant speed). Linear Easing = func(t float32) float32 { return t } // EaseInQuad accelerates from zero velocity (quadratic). EaseInQuad Easing = func(t float32) float32 { return t * t } // EaseOutQuad decelerates to zero velocity (quadratic). EaseOutQuad Easing = func(t float32) float32 { inv := 1 - t return 1 - inv*inv } // EaseInOutQuad accelerates then decelerates (quadratic). EaseInOutQuad Easing = func(t float32) float32 { if t < 0.5 { return 2 * t * t } inv := -2*t + 2 return 1 - inv*inv/2 } // EaseInCubic accelerates from zero velocity (cubic). EaseInCubic Easing = func(t float32) float32 { return t * t * t } // EaseOutCubic decelerates to zero velocity (cubic). EaseOutCubic Easing = func(t float32) float32 { inv := 1 - t return 1 - inv*inv*inv } // EaseInOutCubic accelerates then decelerates (cubic). EaseInOutCubic Easing = func(t float32) float32 { if t < 0.5 { return 4 * t * t * t } inv := -2*t + 2 return 1 - inv*inv*inv/2 } )
Standard polynomial easing functions.
func CubicBezier ¶
CubicBezier creates an Easing function from cubic bezier control points.
The control points define a CSS-style cubic-bezier(x1, y1, x2, y2) curve. The curve starts at (0,0) and ends at (1,1). x1 and x2 should be in [0,1].
Example:
ease := animation.CubicBezier(0.25, 0.1, 0.25, 1.0) // CSS "ease"
func ThreePointCubic ¶
ThreePointCubic creates an Easing function from a two-segment cubic curve.
This is used for Material Design 3's Emphasized easing, which requires two joined cubic bezier segments sharing a midpoint.
Parameters:
- a1, b1: control points for first segment (origin to midpoint)
- mid: shared midpoint
- a2, b2: control points for second segment (midpoint to end)
Example (M3 Emphasized):
emphasized := animation.ThreePointCubic(
[2]float32{0.05, 0},
[2]float32{0.133333, 0.06},
[2]float32{0.166666, 0.4},
[2]float32{0.208333, 0.82},
[2]float32{0.25, 1.0},
)
type LerpFunc ¶
LerpFunc defines a function that linearly interpolates between two values of type T. t=0 returns begin, t=1 returns end.
type ParallelBuilder ¶
type ParallelBuilder struct {
// contains filtered or unexported fields
}
ParallelBuilder constructs a parallel composition.
func DialogEnter ¶
func DialogEnter(opacity, scale signalFloat32) *ParallelBuilder
DialogEnter creates a composite animation for dialog enter transitions.
The animation plays fade in and scale in simultaneously, matching the Material Design 3 dialog container transform pattern. Uses M3 emphasized decelerate easing with DurationMedium2 (300ms).
Parameters:
- opacity: signal driven from 0 to 1
- scale: signal driven from 0.8 to 1.0
func DialogExit ¶
func DialogExit(opacity, scale signalFloat32) *ParallelBuilder
DialogExit creates a composite animation for dialog exit transitions.
The animation plays fade out and scale out simultaneously, matching the Material Design 3 dialog container transform pattern. Uses M3 emphasized accelerate easing with DurationShort4 (200ms).
Parameters:
- opacity: signal driven from 1 to 0
- scale: signal driven from 1.0 to 0.8
func Group ¶
func Group(animations ...Startable) *ParallelBuilder
Group plays all animations in parallel.
Group is a convenience alias for NewParallel with a clearer name for orchestration contexts. All animations start simultaneously and the group completes when the longest animation finishes.
Example:
animation.Group(
animation.FadeIn(opacity, animation.DurationMedium2),
animation.ScaleIn(scale, animation.DurationMedium2),
).Start(ctrl)
func MenuEnter ¶
func MenuEnter(opacity, translateY signalFloat32, distance float32) *ParallelBuilder
MenuEnter creates a composite animation for menu enter transitions.
The animation plays fade in and slide from top simultaneously, matching the Material Design 3 menu reveal pattern. Uses DurationShort4 (200ms).
Parameters:
- opacity: signal driven from 0 to 1
- translateY: signal driven from -distance to 0
- distance: the slide distance in pixels
func MenuExit ¶
func MenuExit(opacity, translateY signalFloat32, distance float32) *ParallelBuilder
MenuExit creates a composite animation for menu exit transitions.
The animation plays fade out and slide to top simultaneously, matching the Material Design 3 menu dismiss pattern. Uses DurationShort3 (150ms).
Parameters:
- opacity: signal driven from 1 to 0
- translateY: signal driven from 0 to -distance
- distance: the slide distance in pixels
func NewParallel ¶
func NewParallel(items ...Startable) *ParallelBuilder
NewParallel creates a composition that plays animations simultaneously.
All animations start at the same time. The parallel composition is complete when the longest animation finishes.
Example:
animation.NewParallel(
animation.To(opacity, 1.0).Duration(200*time.Millisecond),
animation.To(translateY, 0).Duration(300*time.Millisecond),
).Start(ctrl)
func Stagger ¶
func Stagger(delay time.Duration, animations ...Startable) *ParallelBuilder
Stagger creates a composition where each animation starts after a fixed delay from the previous one's start time.
This is implemented as a parallel composition where each successive animation has an additional delay added. All animations run concurrently but with staggered start times.
Example:
// Each item fades in 50ms after the previous one starts
animation.Stagger(50*time.Millisecond,
animation.FadeIn(item1, animation.DurationMedium2),
animation.FadeIn(item2, animation.DurationMedium2),
animation.FadeIn(item3, animation.DurationMedium2),
).Start(ctrl)
func (*ParallelBuilder) OnDone ¶
func (b *ParallelBuilder) OnDone(fn func()) *ParallelBuilder
OnDone sets a callback invoked when all parallel animations complete.
func (*ParallelBuilder) Start ¶
func (b *ParallelBuilder) Start(ctrl *Controller)
Start registers the parallel composition with the controller.
type RepeatingBuilder ¶
type RepeatingBuilder struct {
// contains filtered or unexported fields
}
RepeatingBuilder constructs a repeating animation.
func RepeatForever ¶
func RepeatForever(anim Startable) *RepeatingBuilder
RepeatForever creates an animation that repeats indefinitely.
The animation factory is called fresh for each iteration.
Example:
animation.RepeatForever(
animation.To(pulse, 1.0).From(0.5).Duration(500*time.Millisecond).AutoReverse(),
).Start(ctrl)
func RepeatN ¶
func RepeatN(n int, anim Startable) *RepeatingBuilder
RepeatN creates an animation that repeats the given animation exactly n times.
Pass n=0 for infinite repetition. The animation factory is called fresh for each iteration, ensuring clean state.
Example:
// Pulse opacity 3 times
animation.RepeatN(3,
animation.To(opacity, 1.0).From(0.0).Duration(200*time.Millisecond).Ease(animation.Linear),
).Start(ctrl)
func (*RepeatingBuilder) OnDone ¶
func (b *RepeatingBuilder) OnDone(fn func()) *RepeatingBuilder
OnDone sets a callback invoked when all repetitions complete. Not called for infinite repetitions.
func (*RepeatingBuilder) Start ¶
func (b *RepeatingBuilder) Start(ctrl *Controller)
Start registers the repeating animation with the controller.
type SequenceBuilder ¶
type SequenceBuilder struct {
// contains filtered or unexported fields
}
SequenceBuilder constructs a sequence composition.
func Chain ¶
func Chain(animations ...Startable) *SequenceBuilder
Chain creates a sequential composition that plays animations one after another.
Chain is a convenience alias for NewSequence with a clearer name for orchestration contexts. Each animation starts only after the previous one completes.
Example:
animation.Chain(
animation.FadeOut(oldOpacity, animation.DurationShort4),
animation.FadeIn(newOpacity, animation.DurationMedium2),
).Start(ctrl)
func NewSequence ¶
func NewSequence(items ...Startable) *SequenceBuilder
NewSequence creates a composition that plays animations one after another.
Each animation starts only after the previous one completes. The sequence is complete when all animations have finished.
Example:
animation.NewSequence(
animation.To(opacity, 1.0).Duration(200*time.Millisecond),
animation.To(scale, 1.0).Duration(300*time.Millisecond),
).Start(ctrl)
func (*SequenceBuilder) OnDone ¶
func (b *SequenceBuilder) OnDone(fn func()) *SequenceBuilder
OnDone sets a callback invoked when the sequence completes.
func (*SequenceBuilder) Start ¶
func (b *SequenceBuilder) Start(ctrl *Controller)
Start registers the sequence with the controller.
type Spring ¶
type Spring struct {
// contains filtered or unexported fields
}
Spring represents a running spring animation that drives a Signal[float32].
It uses the damped harmonic oscillator model:
F = -k*x - d*v a = F / m
where k is stiffness, d is damping coefficient, m is mass, x is displacement from target, and v is velocity.
Convergence is detected using the dual-threshold approach: the spring is settled when both |position - target| < restDelta AND |velocity| < restSpeed.
type SpringBuilder ¶
type SpringBuilder struct {
// contains filtered or unexported fields
}
SpringBuilder constructs a Spring using the builder pattern.
Example:
animation.SpringTo(position, 200.0).
Stiffness(animation.StiffnessMedium).
DampingRatio(animation.DampingNoBouncy).
Start(ctrl)
func SpringTo ¶
func SpringTo(signal signalFloat32, target float32) *SpringBuilder
SpringTo creates a new SpringBuilder that animates the signal to the target.
The spring starts from the signal's current value with zero initial velocity. If a previous spring on the same signal is canceled via auto-cancel, velocity is automatically transferred.
func (*SpringBuilder) Build ¶
func (b *SpringBuilder) Build() *Spring
Build returns the configured Spring without starting it.
func (*SpringBuilder) DampingRatio ¶
func (b *SpringBuilder) DampingRatio(ratio float32) *SpringBuilder
DampingRatio sets the damping ratio. Default is DampingNoBouncy (1.0).
Presets: DampingHighBouncy (0.2), DampingMediumBouncy (0.5), DampingLowBouncy (0.75), DampingNoBouncy (1.0).
func (*SpringBuilder) InitialVelocity ¶
func (b *SpringBuilder) InitialVelocity(v float32) *SpringBuilder
InitialVelocity sets the initial velocity. Default is 0.
func (*SpringBuilder) Mass ¶
func (b *SpringBuilder) Mass(m float32) *SpringBuilder
Mass sets the mass. Default is 1.0. Higher mass = slower response.
func (*SpringBuilder) OnDone ¶
func (b *SpringBuilder) OnDone(fn func()) *SpringBuilder
OnDone sets a callback invoked when the spring settles.
func (*SpringBuilder) RestDelta ¶
func (b *SpringBuilder) RestDelta(d float32) *SpringBuilder
RestDelta sets the position convergence threshold. Default is 0.5 (sub-pixel).
func (*SpringBuilder) RestSpeed ¶
func (b *SpringBuilder) RestSpeed(s float32) *SpringBuilder
RestSpeed sets the velocity convergence threshold. Default is 10.0 (px/s).
func (*SpringBuilder) Start ¶
func (b *SpringBuilder) Start(ctrl *Controller) *Spring
Start builds the spring and registers it with the controller.
If another animation is running on the same signal, it is auto-canceled. If the canceled animation was a Spring, its velocity is transferred.
func (*SpringBuilder) Stiffness ¶
func (b *SpringBuilder) Stiffness(k float32) *SpringBuilder
Stiffness sets the spring constant k. Default is StiffnessMedium (1500).
type Startable ¶
type Startable interface {
// contains filtered or unexported methods
}
Startable is the interface for anything that can be built into an animatable.
Both AnimationBuilder and SpringBuilder satisfy this via their Build methods. This interface is used by Sequence and Parallel to accept both types.
func Reverse ¶
Reverse wraps an animation to play with inverted time mapping.
This works by intercepting the easing and inverting the progress value. For tween animations, it swaps the from/to values. For other types, it wraps the animatable.
Example:
// Slide out to bottom (reverse of slide in from bottom)
animation.Reverse(
animation.SlideInFromBottom(translateY, 100, animation.DurationMedium2),
).Start(ctrl)
func WithDelay ¶
WithDelay wraps an animation builder with an initial delay before it starts.
This returns a new AnimationBuilder if the input is an AnimationBuilder, otherwise wraps the animation in a delayed orchestration wrapper.
Example:
animation.WithDelay(200*time.Millisecond,
animation.FadeIn(opacity, animation.DurationMedium2),
).Start(ctrl)
type Tween ¶
type Tween[T any] struct { // contains filtered or unexported fields }
Tween maps a float32 progress value [0,1] to an interpolated value of type T.
Tween is a pure evaluator with no lifecycle or state. It follows the Flutter pattern where AnimationController produces 0..1 and Tween maps that to any type.
Example:
colorTween := animation.NewColorTween(red, blue) mid := colorTween.At(0.5) // 50% between red and blue
func NewColorTween ¶
NewColorTween creates a Tween that interpolates between two colors.
func NewFloat32Tween ¶
NewFloat32Tween creates a Tween that interpolates between two float32 values.
func NewPointTween ¶
NewPointTween creates a Tween that interpolates between two points.
func NewSizeTween ¶
NewSizeTween creates a Tween that interpolates between two sizes.
func NewTween ¶
NewTween creates a Tween with a custom interpolation function.
The lerpFn is called with t in [0,1] to interpolate between begin and end.