animation

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ¶

Package animation

Package animation provides a generic, imperative framework for property interpolation and tweening within the Kite framework. It allows for smooth transitions of numeric values, colors, and other types over time using customizable easing functions.

grid_animation

🚀 Key Concepts

Animator

The core interface for any time-based animation.

type Animator interface {
    // Tick updates the animation state by the given delta time.
    // Returns true if the animation has finished.
    Tick(dt time.Duration) bool
}
Tween

A Tween[T] is a concrete implementation of Animator that interpolates a value of type T from a Start value to an End value over a specific Duration.

Easing Functions

Easing functions control the rate of change of a parameter over time. The package includes several common easing functions:

  • Linear
  • EaseInQuad
  • EaseOutQuad
  • EaseInOutCubic
Interpolators

Interpolators define how to calculate a value between two points given a progress value (0.0 to 1.0).

  • FloatInterpolator: For float64 values.
  • IntInterpolator: For int values.
  • ColorInterpolator: For image/color.Color (handles alpha-premultiplied RGBA).

🛠 Usage Example

For style changes, animate only the properties you want to update:

panel.Animate(
    style.S().Width(style.Cells(8)),
    style.S().Width(style.Cells(32)),
    animation.Options{
        Duration: time.Second,
    },
)

For other values, schedule an Animator on the document window:

win := doc.Terminal().Window()
playback := animation.Play(win, animation.Int(0, 100, time.Second, updateCount))

Playback requests one frame at a time. Cancel() prevents further ticks, and IsDone() reports completion or cancellation. Use win.RequestAnimationFrame(fn) directly for a single frame timestamp.

Documentation ¶

Overview ¶

Package animation interpolates styles and custom values on the host frame clock. Elements use Options and InterpolateStyle for sparse style transitions. For non-style values, pass a Tween or another Animator to Play and use the returned Playback to cancel future ticks.

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func ColorInterpolator ¶

func ColorInterpolator(start, end color.Color, progress float64) color.Color

ColorInterpolator interpolates the alpha-premultiplied RGBA channels of image/color.Color.

func EaseInOutCubic ¶

func EaseInOutCubic(t float64) float64

EaseInOutCubic is a cubic ease-in-ease-out.

func EaseInQuad ¶

func EaseInQuad(t float64) float64

EaseInQuad is a quadratic ease-in.

func EaseOutQuad ¶

func EaseOutQuad(t float64) float64

EaseOutQuad is a quadratic ease-out.

func FloatInterpolator ¶

func FloatInterpolator(start, end float64, progress float64) float64

FloatInterpolator interpolates between two float64 values.

func IntInterpolator ¶

func IntInterpolator(start, end int, progress float64) int

IntInterpolator interpolates between two int values.

func InterpolateGridTracks ¶

func InterpolateGridTracks(start, end []style.GridTrackSize, progress float64) []style.GridTrackSize

InterpolateGridTracks interpolates between two slices of GridTrackSize. It iterates through the tracks matching by index. If start and end tracks are of the same Kind, their values are interpolated based on progress. If they are of different kinds, or if the slice lengths differ, it snaps to start (if progress < 0.5) or end (if progress >= 0.5).

func InterpolateStyle ¶

func InterpolateStyle(start, end style.Style, p float64) style.Style

InterpolateStyle blends two sparse styles at normalized progress p in [0, 1]. Numeric dimensions, colors, spacing, flex, and grid tracks interpolate when both sides set compatible values; discrete properties switch at p == 0.5.

func Linear ¶

func Linear(t float64) float64

Linear is a straight linear easing.

Types ¶

type Animator ¶

type Animator interface {
	// Tick updates the animation state by the given delta time.
	// Returns true if the animation has finished.
	Tick(dt time.Duration) bool
}

Animator represents an animation that can be ticked over time.

type EasingFunction ¶

type EasingFunction func(t float64) float64

EasingFunction maps progress in the range [0.0, 1.0] to an eased progress value in [0.0, 1.0].

type FrameScheduler ¶

type FrameScheduler interface {
	RequestAnimationFrame(fn func(now time.Time)) (cancel func())
}

FrameScheduler is the minimal host frame-clock contract (satisfied by terminal.Window).

type Interpolator ¶

type Interpolator[T any] func(start, end T, progress float64) T

Interpolator defines the function signature for interpolating between two values of type T.

type Options ¶

type Options struct {
	Duration   time.Duration
	Easing     EasingFunction
	OnComplete func()
}

Options configures an element or style animation.

type Playback ¶

type Playback struct {
	// contains filtered or unexported fields
}

Playback controls an active animation scheduled on a FrameScheduler.

func Play ¶

func Play(win FrameScheduler, anim Animator) *Playback

Play schedules anim to tick once per frame on win until it finishes or is cancelled.

func (*Playback) Cancel ¶

func (p *Playback) Cancel()

Cancel stops the animation and prevents any pending completion callback from firing.

func (*Playback) IsDone ¶

func (p *Playback) IsDone() bool

IsDone reports whether the animation has finished or been cancelled.

type Tween ¶

type Tween[T any] struct {
	Start    T
	End      T
	Duration time.Duration
	Easing   EasingFunction
	Interp   Interpolator[T]
	OnUpdate func(current T)
	// contains filtered or unexported fields
}

Tween implements Animator, performing interpolation between a start and end value over a duration.

func Color ¶

func Color(start, end color.Color, duration time.Duration, onUpdate func(color.Color)) *Tween[color.Color]

Color creates a Tween interpolating between two color.Color values using EaseOutQuad by default.

func Float ¶

func Float(start, end float64, duration time.Duration, onUpdate func(float64)) *Tween[float64]

Float creates a Tween interpolating between two float64 values using EaseOutQuad by default.

func Int ¶

func Int(start, end int, duration time.Duration, onUpdate func(int)) *Tween[int]

Int creates a Tween interpolating between two int values using EaseOutQuad by default.

func NewTween ¶

func NewTween[T any](start, end T, duration time.Duration, easing EasingFunction, interp Interpolator[T], onUpdate func(current T)) *Tween[T]

NewTween creates a new Tween animator.

func (*Tween[T]) Ease ¶

func (t *Tween[T]) Ease(easing EasingFunction) *Tween[T]

Ease sets the easing function for the tween and returns the tween for chaining.

func (*Tween[T]) OnComplete ¶

func (t *Tween[T]) OnComplete(fn func()) *Tween[T]

OnComplete registers a callback invoked once when the tween finishes.

func (*Tween[T]) Tick ¶

func (t *Tween[T]) Tick(dt time.Duration) bool

Tick ticks the animation by dt. Returns true if the animation is finished.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL