anim

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 6 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 declares time-based animations: tweens with easing curves, combined into timelines with Sequence, Parallel and Delay, and launched with Play; and physics springs (Spring), which run until they settle and keep their velocity when retargeted. The application loop advances them once per frame and parks when none run. Callbacks run on the loop goroutine, so a tween may mutate widgets directly.

Index

Constants

View Source
const (
	DurationShort  = 120 * time.Millisecond
	DurationMedium = 220 * time.Millisecond
	DurationLong   = 360 * time.Millisecond
)

The motion durations every surface and control share: short for state changes (hover, press, a toggle), medium for surfaces opening and closing, long for large moves across the screen.

Variables

View Source
var (
	BezierEmphasizedDecelerate = Bezier{0.05, 0.7, 0.1, 1}
	BezierEmphasizedAccelerate = Bezier{0.3, 0, 0.8, 0.15}
	BezierStandard             = Bezier{0.2, 0, 0, 1}

	// EmphasizedDecelerate brings content in fast and settles it long.
	EmphasizedDecelerate = BezierEmphasizedDecelerate.Easing()
	// EmphasizedAccelerate starts slow and leaves fast: exits.
	EmphasizedAccelerate = BezierEmphasizedAccelerate.Easing()
	// Standard eases a change between two resting states.
	Standard = BezierStandard.Easing()
)

The emphasized curves, the Material 3 motion pair, and its standard curve: an entrance decelerates hard into place, an exit accelerates away, and a transform between two resting states eases both ends.

View Source
var (
	// SpringSmooth settles without overshoot: layout and size changes.
	SpringSmooth = SpringParams{Response: 350 * time.Millisecond, Damping: 1}
	// SpringSnappy overshoots a touch and lands quickly: popovers,
	// menus, toggles.
	SpringSnappy = SpringParams{Response: 300 * time.Millisecond, Damping: 0.86}
	// SpringBouncy overshoots visibly before it settles: playful
	// entrances, toasts.
	SpringBouncy = SpringParams{Response: 450 * time.Millisecond, Damping: 0.68}
)

The named springs.

Functions

func BounceSpring

func BounceSpring(bounce float64) (zeta, omega float64)

BounceSpring is the damping ratio and angular frequency of a duration-filling spring with the given bounce (0 none, toward 1 a lot): damping 1 - bounce, and a frequency whose envelope has decayed to a thousandth by the end of the duration, so the curve lands.

func CanonicalLinear

func CanonicalLinear(pts []LinearPt)

CanonicalLinear resolves a linear() point list's positions in place (CSS Easing 2 §2.1.1): the first point defaults to 0 and the last to 1, a position below an earlier one rises to it, and the points left without one spread evenly between their positioned neighbours. Every point has HasPos set afterwards.

func CubicBezierAt

func CubicBezierAt(x1, y1, x2, y2, t float64) float64

CubicBezierAt evaluates the CSS cubic-bezier(x1, y1, x2, y2) timing function at t: see lys-lib's motion.CubicBezier, whose solver this is.

func EaseInCubic

func EaseInCubic(t float64) float64

EaseInCubic starts slow and accelerates hard.

func EaseInOutCubic

func EaseInOutCubic(t float64) float64

EaseInOutCubic eases both ends of the tween.

func EaseInOutQuad

func EaseInOutQuad(t float64) float64

EaseInOutQuad accelerates through the first half, decelerates through the second.

func EaseInQuad

func EaseInQuad(t float64) float64

EaseInQuad starts slow and accelerates.

func EaseOutBack

func EaseOutBack(t float64) float64

EaseOutBack overshoots the target by a fixed fraction and settles back - the classic popover landing. The overshoot peaks around 1.10 near t = 0.7.

func EaseOutCubic

func EaseOutCubic(t float64) float64

EaseOutCubic is lys-ui's default curve: fast start, gentle landing.

func EaseOutQuad

func EaseOutQuad(t float64) float64

EaseOutQuad starts fast and decelerates.

func Instant

func Instant() bool

Instant reports whether things that move land at once: springs, smooth scrolling, keyframes, spinners. It is true with LYS_UI_NO_ANIM=1 or SetInstant, under which tweens collapse as well (delays keep their durations, so timing survives), and under SetReduced, the desktop's reduced-motion preference, under which tweens still run so that a surface can fade.

func Linear

func Linear(t float64) float64

Linear runs at constant speed.

func NoFades added in v0.2.1

func NoFades() bool

NoFades reports whether even fades collapse: LYS_UI_NO_ANIM=1 or SetInstant.

func PiecewiseAt

func PiecewiseAt(pts []LinearPt, t float64) float64

PiecewiseAt evaluates the linear(...) easing over canonical points at t (CSS Easing 2 §2.1.2): interpolated between the last point at or before t and the next, extended past the outermost ones along their segment, and the later point's value where two share a position. One point is a constant; none is the identity.

func Reduced added in v0.2.1

func Reduced() bool

Reduced reports the desktop's reduced-motion preference: SetReduced.

func SetInstant

func SetInstant(on bool) (restore func())

SetInstant turns reduced motion on or off for later launches and returns the restore function.

func SetReduced added in v0.2.1

func SetReduced(on bool)

SetReduced records the desktop's reduced-motion preference, which app.Application follows. Use widget.SetReducedMotion, which also restyles, from anything that draws.

func SpringEasingAt

func SpringEasingAt(zeta, omega, t float64) float64

SpringEasingAt evaluates SpringEasing(zeta, omega) at t without building the curve.

func StepsAt

func StepsAt(n int, jump StepPos, t float64, before bool) float64

StepsAt evaluates steps(n, jump) at t (CSS Easing 2 §2.3.1): the progress held in n equal intervals, jumping at each interval's start (JumpStart), end (JumpEnd), both edges (JumpBoth, n+1 jumps) or neither (JumpNone, n-1 jumps). before is the before flag: sampled from the before phase, a jump exactly at t is not yet taken. n reads as at least 1, and as at least 2 under JumpNone, the counts CSS admits.

Types

type Bezier

type Bezier [4]float64

Bezier is a cubic-bezier curve's control points (x1, y1, x2, y2), as data: the theme hands the same curves to stylesheets.

func (Bezier) Easing

func (b Bezier) Easing() Easing

Easing is the curve as an easing function.

type Cancel

type Cancel func()

Cancel stops a launched timeline: every piece not yet finished never fires again, and widget state keeps the last value a callback left. Calling it again, or after the timeline finished, does nothing.

func Play

func Play(steps ...Step) Cancel

Play launches steps as one timeline, several steps running in sequence, and returns its Cancel. The schedule is fixed at launch, so ticks land on exact offsets however frames arrive.

func Start

func Start(dur time.Duration, fn func(t float64)) Cancel

Start runs fn once per frame for dur with EaseOutCubic progress from 0 to 1: Play(Animate(dur, fn)).

type Easing

type Easing func(t float64) float64

Easing maps linear progress t in [0, 1] to eased progress. Most curves keep the result inside [0, 1]; springs may overshoot past 1 (or dip below 0) on their way to settling, so consumers must clamp where a value cannot overshoot (alpha, positions in bounds).

func CubicBezier

func CubicBezier(x1, y1, x2, y2 float64) Easing

CubicBezier returns the CSS cubic-bezier(x1, y1, x2, y2) timing function; see CubicBezierAt.

func Piecewise

func Piecewise(pts []LinearPt) Easing

Piecewise returns the linear(...) easing over a copy of pts made canonical; see PiecewiseAt.

func SpringEasing

func SpringEasing(zeta, omega float64) Easing

SpringEasing returns a damped-spring easing: the value leaves 0 with zero velocity and settles at 1, oscillating on the way when underdamped. zeta is the damping ratio - below 1 overshoots (0.3 to 0.6 give a lively wobble, 1 is critically damped with none), above 1 is overdamped and sluggish. omega is the undamped angular frequency in radians across the normalized tween: 2*pi crosses one full oscillation over the duration, so popovers want roughly 6 to 12. It is the fixed-duration curve; Spring is the physics spring that keeps its velocity across retargets.

func Steps

func Steps(n int, jump StepPos) Easing

Steps returns the steps(n, jump) timing function; see StepsAt.

type LinearPt

type LinearPt struct {
	Value float64
	Pos   float64
	// HasPos marks an explicit percentage stop; the rest spread
	// evenly between their bracketing explicit stops.
	HasPos bool
}

LinearPt is one point of a linear() easing: the output value and, when HasPos, its input position as a fraction of the timeline.

type Spring

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

Spring moves any number of components (a rect's x, y, w, h; a radius; a color's channels) toward their targets with a damped harmonic spring, one per component. It carries position and velocity, so retargeting it mid-flight continues from where and how fast it is moving instead of restarting from rest - an interrupted open turning into a close flows rather than jerks. Steps are the closed-form solution, exact for any frame interval. It runs on the application loop: onFrame sees every frame's position, and must not keep the slice.

func NewSpring

func NewSpring(p SpringParams, initial []float64, onFrame func(pos []float64)) *Spring

NewSpring returns a spring at rest at initial, which reports its position to onFrame on every frame it moves.

func (*Spring) Position

func (s *Spring) Position() []float64

Position and Velocity are the spring's current state, per component; the slices are the spring's own.

func (*Spring) Running

func (s *Spring) Running() bool

Running reports whether the spring is in motion.

func (*Spring) SetParams

func (s *Spring) SetParams(p SpringParams)

SetParams reshapes the spring; the motion in flight continues under the new shape from its current position and velocity.

func (*Spring) SetPrecision

func (s *Spring) SetPrecision(p float64)

SetPrecision sets how close, in the components' units, counts as arrived: the default hundredth suits pixels; a 0-to-1 fraction wants a thousandth, or a small overshoot would read as settled.

func (*Spring) SetTarget

func (s *Spring) SetTarget(target ...float64)

SetTarget retargets the spring, keeping its position and velocity, and runs it until it settles. Under reduced motion (Instant) it lands on the target at once, with one onFrame. target has one value per component; a different count is a caller bug and panics.

func (*Spring) Snap

func (s *Spring) Snap(v ...float64)

Snap puts the spring at rest at v, stopping any motion, with one onFrame.

func (*Spring) Stop

func (s *Spring) Stop()

Stop halts the spring where it is.

func (*Spring) Velocity

func (s *Spring) Velocity() []float64

Velocity is the spring's current velocity in units per second.

type SpringParams

type SpringParams struct {
	Response time.Duration
	Damping  float64
}

SpringParams shapes a physics spring the way SwiftUI's springs are shaped: Response is the period of the undamped oscillation, how long one swing takes; Damping is the damping fraction - 1 settles without overshoot, below 1 bounces (smaller bounces more), above 1 creeps.

type Step

type Step interface {
	// contains filtered or unexported methods
}

Step is one timed piece of a timeline: a tween (Animate), a hold (Delay), or a composition (Sequence, Parallel).

func Delay

func Delay(d time.Duration) Step

Delay holds the timeline for d before its next step.

func Parallel

func Parallel(steps ...Step) Step

Parallel runs steps together and ends when the last of them ends.

func Sequence

func Sequence(steps ...Step) Step

Sequence runs steps back to back, each starting when the previous one ends.

type StepPos

type StepPos uint8

StepPos is where a steps() curve jumps: at the start of each interval, the end, both edges, or neither.

const (
	JumpEnd StepPos = iota
	JumpStart
	JumpNone
	JumpBoth
)

The steps() jump positions. JumpEnd is the CSS default (and the legacy `end` keyword); JumpStart is the legacy `start`.

type Tween

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

Tween animates one value: fn runs once per frame with eased progress over the tween's duration.

func Animate

func Animate(dur time.Duration, fn func(t float64)) *Tween

Animate declares a tween over dur with the EaseOutCubic curve. A zero or negative dur runs fn once with 1 when it is reached.

func (*Tween) Easing

func (t *Tween) Easing(e Easing) *Tween

Easing replaces the tween's curve and returns the tween.

Jump to

Keyboard shortcuts

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