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 ¶
- func Motion() bool
- func SetMotion(on bool)
- func Update(msg tea.Msg, anims ...Animation) tea.Cmd
- type Animation
- type Easing
- type FrameMsg
- type Option
- type Presence
- func (p *Presence) Advance(now time.Time) bool
- func (p *Presence) Animating() bool
- func (p *Presence) Float() float64
- func (p *Presence) Hide() tea.Cmd
- func (p *Presence) HideAt(now time.Time)
- func (p *Presence) Progress() float64
- func (p *Presence) Show() tea.Cmd
- func (p *Presence) ShowAt(now time.Time)
- func (p *Presence) ShowFor(d time.Duration) tea.Cmd
- func (p *Presence) Shown() bool
- func (p *Presence) Toggle() tea.Cmd
- func (p *Presence) Update(msg tea.Msg) tea.Cmd
- func (p *Presence) Visible() bool
- type Sequence
- func (s *Sequence) Animating() bool
- func (s *Sequence) Float() float64
- func (s *Sequence) Int() int
- func (s *Sequence) Loop() tea.Cmd
- func (s *Sequence) Play() tea.Cmd
- func (s *Sequence) Step() int
- func (s *Sequence) Stop()
- func (s *Sequence) Update(msg tea.Msg) tea.Cmd
- func (s Sequence) With(opts ...Option) Sequence
- type Step
- type Ticker
- type Value
- func (v *Value) Advance(now time.Time) bool
- func (v *Value) Animating() bool
- func (v *Value) Float() float64
- func (v *Value) Int() int
- func (v *Value) Play(from, to float64) tea.Cmd
- func (v *Value) Set(x float64)
- func (v *Value) SetTarget(target float64, now time.Time)
- func (v *Value) Target() float64
- func (v *Value) To(target float64) tea.Cmd
- func (v *Value) Update(msg tea.Msg) tea.Cmd
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
Types ¶
type Animation ¶
Animation is anything animated by the messages passed to its Update, as Value, Presence, and Sequence are.
type Easing ¶
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 FPS ¶
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 ¶
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 ¶
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 ¶
NewPresence returns a hidden presence, configured as NewValue would be.
func (*Presence) Advance ¶
Advance moves the presence to now without scheduling a command, as Value.Advance does, and reports whether it is still animating.
func (*Presence) Float ¶
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) HideAt ¶
HideAt starts animating out at now without scheduling a command, as Presence.ShowAt starts animating in.
func (*Presence) ShowAt ¶
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 ¶
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) Update ¶
Update advances the animation, and hides it when the time given to Presence.ShowFor is up; see Value.Update.
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 ¶
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 ¶
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 ¶
Animating reports whether the sequence is running, including while it holds still.
func (*Sequence) Loop ¶
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 ¶
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) Update ¶
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.
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.
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 ¶
NewTicker returns a stopped ticker at zero. Of opts, only FPS, UnfocusedFPS, and Clock apply.
func (*Ticker) Reset ¶
func (k *Ticker) Reset()
Reset puts the ticker back at zero, leaving it running or stopped.
func (*Ticker) Start ¶
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) Update ¶
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 (*Value) Advance ¶
Advance moves the value to now without scheduling a command and reports whether it is still animating.
func (*Value) Play ¶
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) SetTarget ¶
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) To ¶
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 ¶
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.