anim

package
v0.0.0-alpha.13 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 4 Imported by: 2 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.

Documentation

Overview

Package anim animates numbers in a Bubble Tea model: a Value eases or springs toward a target, and a Presence is a value from 0 to 1 for showing and hiding something, such as a dialog.

Animations are model state, advanced by the messages their commands schedule: return the command from Value.To or Presence.Show, and pass every message to Update, which schedules the next frame until the animation is done. Progress follows the clock, not the number of frames, so a slow frame does not slow an animation down.

While the terminal is not focused, animations ask for fewer frames (see UnfocusedFPS). Terminals report focus only to programs that enable it with tea.View ReportFocus. SetMotion turns animation off for the whole program, for terminals that struggle with it or people who would rather not see it.

See the documentation for guides and examples. It is also available for LLMs and agents as llms.txt.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Motion

func Motion() bool

Motion reports whether animation is on. See SetMotion.

func SetMotion

func SetMotion(on bool)

SetMotion turns animation on or off for the whole program. It is on by default. With it off, animations jump to where they are going. A Sequence still waits out its Hold steps when played, as those are about time rather than motion, but a looping one settles where its steps end and stops.

Animations already running follow it from their next frame. A loop stopped by turning motion off stays stopped until Sequence.Loop is called again.

func Update

func Update(msg tea.Msg, anims ...Animation) tea.Cmd

Update passes msg to each of anims, returning their commands as one:

return m, tea.Batch(cmd, anim.Update(msg, &m.dialog, &m.toast, &m.sidebar))

Types

type Animation

type Animation interface {
	Update(msg tea.Msg) tea.Cmd
}

Animation is anything animated by the messages passed to its Update, as Value, Presence, and Sequence are.

type Easing

type Easing func(t float64) float64

Easing maps linear progress, from 0 to 1, to eased progress.

var (
	Linear     Easing = func(t float64) float64 { return t }
	InQuad     Easing = func(t float64) float64 { return t * t }
	OutQuad    Easing = func(t float64) float64 { return 1 - (1-t)*(1-t) }
	InOutQuad  Easing = func(t float64) float64 { return inOut(t, 2) }
	OutCubic   Easing = func(t float64) float64 { return 1 - (1-t)*(1-t)*(1-t) }
	InOutCubic Easing = func(t float64) float64 { return inOut(t, 3) }
	// OutBack overshoots the target a little before settling on it.
	OutBack Easing = func(t float64) float64 {
		const c1, c3 = 1.70158, 2.70158
		u := t - 1
		return 1 + c3*u*u*u + c1*u*u
	}
)

Easings, for Ease.

type FrameMsg

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

FrameMsg advances an animation. Values ignore those that are not theirs.

type Option

type Option func(*config)

Option configures a Value, Presence, or Sequence.

func Clock

func Clock(now func() time.Time) Option

Clock sets the value's source of time, for tests.

func Duration

func Duration(d time.Duration) Option

Duration makes the value ease to each target over d.

func Ease

func Ease(e Easing) Option

Ease sets the easing of a duration-based value.

func FPS

func FPS(n int) Option

FPS sets how many frames per second the value asks for while animating. It need not be more than Bubble Tea renders, 60 by default.

func Spring

func Spring(frequency, damping float64) Option

Spring makes the value move like a damped spring of the given angular frequency, where higher is faster, and damping ratio, where 1 settles without overshooting and less than 1 bounces. Unlike easing, a spring keeps its speed when its target changes mid-flight.

func UnfocusedFPS

func UnfocusedFPS(n int) Option

UnfocusedFPS sets how many frames per second the value asks for while the terminal is not focused, 20 by default, as an animation nobody is looking at need not be smooth. It never asks for more than FPS. The value learns of focus from the tea.FocusMsg and tea.BlurMsg passed to its Update, which Bubble Tea sends only while tea.View ReportFocus is set; until then, a value counts the terminal as focused.

type Presence

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

Presence animates something being shown and hidden, as a value from 0, hidden, to 1, shown. Keep showing it while Presence.Visible reports true, so it is still there to animate out after Presence.Hide, and pass Presence.Progress to a transition.

func NewPresence

func NewPresence(opts ...Option) Presence

NewPresence returns a hidden presence, configured as NewValue would be.

func (*Presence) Advance

func (p *Presence) Advance(now time.Time) bool

Advance moves the presence to now without scheduling a command, as Value.Advance does, and reports whether it is still animating.

func (*Presence) Animating

func (p *Presence) Animating() bool

Animating reports whether it is animating in or out.

func (*Presence) Float

func (p *Presence) Float() float64

Float returns how far in it is, without keeping it between 0 and 1, as a spring that overshoots goes past them, such as to bounce something past its place.

func (*Presence) Hide

func (p *Presence) Hide() tea.Cmd

Hide starts animating out.

func (*Presence) HideAt

func (p *Presence) HideAt(now time.Time)

HideAt starts animating out at now without scheduling a command, as Presence.ShowAt starts animating in.

func (*Presence) Progress

func (p *Presence) Progress() float64

Progress returns how far in it is, from 0 to 1.

func (*Presence) Show

func (p *Presence) Show() tea.Cmd

Show starts animating in.

func (*Presence) ShowAt

func (p *Presence) ShowAt(now time.Time)

ShowAt starts animating in at now without scheduling a command, as Value.SetTarget does, cancelling hiding it after a Presence.ShowFor. Use Presence.Advance with the same clock to drive it, such as in a test with a Clock of its own.

func (*Presence) ShowFor

func (p *Presence) ShowFor(d time.Duration) tea.Cmd

ShowFor starts animating in, and out again once d has passed, such as for a notification. Showing or hiding it again in the meantime cancels hiding it after d, and calling ShowFor again restarts d.

func (*Presence) Shown

func (p *Presence) Shown() bool

Shown reports whether it is shown or animating in.

func (*Presence) Toggle

func (p *Presence) Toggle() tea.Cmd

Toggle starts animating in, or out if shown.

func (*Presence) Update

func (p *Presence) Update(msg tea.Msg) tea.Cmd

Update advances the animation, and hides it when the time given to Presence.ShowFor is up; see Value.Update.

func (*Presence) Visible

func (p *Presence) Visible() bool

Visible reports whether any of it is showing: shown, or not yet done animating out. A spring that overshoots past hidden is still animating.

type Sequence

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

Sequence is a number that runs through a list of steps, one after the other: animating, holding, and jumping. It replaces chaining animations with timers, and a step starts when the one before it was due to end rather than when its frame happened to arrive, so a sequence does not drift.

Use it like a Value: return the command from Sequence.Play or Sequence.Loop, and pass every message to Update. The zero Sequence has no steps; make one with NewSequence.

func NewLoop

func NewLoop(period time.Duration, opts ...Option) Sequence

NewLoop returns a sequence that runs from 0 to 1 at an even pace over period, and then from 0 again, such as the phase of a shimmer or of scrolling text. Start it with Sequence.Loop; opts are as for Sequence.With.

func NewSequence

func NewSequence(start float64, steps ...Step) Sequence

NewSequence returns a stopped sequence at start that runs steps. Its steps default to NewValue's options; change them with Sequence.With.

func (*Sequence) Animating

func (s *Sequence) Animating() bool

Animating reports whether the sequence is running, including while it holds still.

func (*Sequence) Float

func (s *Sequence) Float() float64

Float returns the current value.

func (*Sequence) Int

func (s *Sequence) Int() int

Int returns the current value, rounded.

func (*Sequence) Loop

func (s *Sequence) Loop() tea.Cmd

Loop runs the steps over and over, until Sequence.Stop or another call to Play or Loop. A run through the steps that takes no time ends the loop.

func (*Sequence) Play

func (s *Sequence) Play() tea.Cmd

Play runs the steps once, from the first, restarting if already running. It starts from the current value, so begin with Jump to start elsewhere. It returns the command for the first frame, or nil if there is nothing to wait for.

func (*Sequence) Step

func (s *Sequence) Step() int

Step returns the index of the step being run, or -1 when stopped.

func (*Sequence) Stop

func (s *Sequence) Stop()

Stop stops the sequence where it is.

func (*Sequence) Update

func (s *Sequence) Update(msg tea.Msg) tea.Cmd

Update advances the sequence for its own FrameMsg, returning the command for the next one while it is running. It notes focus as Value.Update does.

func (Sequence) With

func (s Sequence) With(opts ...Option) Sequence

With sets the options steps start from, such as Duration and Ease, and FPS, UnfocusedFPS, and Clock for the whole sequence. It returns the changed sequence, to be used before it is played.

type Step

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

Step is one part of a Sequence: moving to a target, holding still, or jumping. Build one with To, Hold, or Jump.

func Hold

func Hold(d time.Duration) Step

Hold is a step that leaves the value as it is for d.

func Jump

func Jump(target float64) Step

Jump is a step that sets the value to target without animating, as to restart a looping sequence from its start.

func To

func To(target float64, opts ...Option) Step

To is a step that animates to target, from wherever the value is, ending when it gets there. Options such as Duration, Ease, and Spring override the sequence's for this step only.

type Ticker

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

Ticker is a clock that runs for as long as it is started, for things that animate without an end, such as a backdrop of fx.Ripple or fx.Drift, whose Time field takes Ticker.Elapsed. Unlike a looping Sequence, it never wraps, so what follows it need not repeat.

Use it like a Value: return the command from Ticker.Start, and pass every message to Update. It asks for frames at FPS, and at UnfocusedFPS while the terminal is not focused. While motion is off, see SetMotion, it stands still. The zero Ticker does not tick; make one with NewTicker.

func NewTicker

func NewTicker(opts ...Option) Ticker

NewTicker returns a stopped ticker at zero. Of opts, only FPS, UnfocusedFPS, and Clock apply.

func (*Ticker) Animating

func (k *Ticker) Animating() bool

Animating reports whether the ticker is running.

func (*Ticker) Elapsed

func (k *Ticker) Elapsed() time.Duration

Elapsed returns how long the ticker has run.

func (*Ticker) Reset

func (k *Ticker) Reset()

Reset puts the ticker back at zero, leaving it running or stopped.

func (*Ticker) Seconds

func (k *Ticker) Seconds() float64

Seconds returns how long the ticker has run, in seconds.

func (*Ticker) Start

func (k *Ticker) Start() tea.Cmd

Start runs the ticker from where it stopped, returning the command for its first frame, or nil when it is already running or motion is off.

func (*Ticker) Stop

func (k *Ticker) Stop()

Stop stops the ticker where it is. Its frames are ignored until Start asks for new ones.

func (*Ticker) Toggle

func (k *Ticker) Toggle() tea.Cmd

Toggle stops a running ticker, and starts a stopped one.

func (*Ticker) Update

func (k *Ticker) Update(msg tea.Msg) tea.Cmd

Update advances the ticker for its own FrameMsg, returning the command for the next one while it is running. It notes tea.FocusMsg and tea.BlurMsg for UnfocusedFPS.

type Value

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

Value is a number that animates toward a target. The zero Value does not animate; make one with NewValue.

func NewValue

func NewValue(v float64, opts ...Option) Value

NewValue returns a value at v, which by default eases to each target over 250ms.

func (*Value) Advance

func (v *Value) Advance(now time.Time) bool

Advance moves the value to now without scheduling a command and reports whether it is still animating.

func (*Value) Animating

func (v *Value) Animating() bool

Animating reports whether the value is moving.

func (*Value) Float

func (v *Value) Float() float64

Float returns the current value.

func (*Value) Int

func (v *Value) Int() int

Int returns the current value, rounded.

func (*Value) Play

func (v *Value) Play(from, to float64) tea.Cmd

Play jumps to from and starts animating to to, for something that runs from the start each time it happens, such as a shake or a burst. It returns the command for the first frame.

func (*Value) Set

func (v *Value) Set(x float64)

Set jumps to x without animating.

func (*Value) SetTarget

func (v *Value) SetTarget(target float64, now time.Time)

SetTarget starts moving toward target at now without scheduling a command. It preserves spring velocity when retargeted. Use Value.Advance with the same clock to drive it from an external frame loop.

func (*Value) Target

func (v *Value) Target() float64

Target returns the value being animated toward.

func (*Value) To

func (v *Value) To(target float64) tea.Cmd

To starts animating toward target, from wherever the value is now. It returns the command for the first frame, or nil when an animation already running will carry on toward the new target.

func (*Value) Update

func (v *Value) Update(msg tea.Msg) tea.Cmd

Update advances the value for its own FrameMsg, returning the command for the next frame while it is animating. It notes tea.FocusMsg and tea.BlurMsg for UnfocusedFPS.

Jump to

Keyboard shortcuts

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