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
- Variables
- func BounceSpring(bounce float64) (zeta, omega float64)
- func CanonicalLinear(pts []LinearPt)
- func CubicBezierAt(x1, y1, x2, y2, t float64) float64
- func EaseInCubic(t float64) float64
- func EaseInOutCubic(t float64) float64
- func EaseInOutQuad(t float64) float64
- func EaseInQuad(t float64) float64
- func EaseOutBack(t float64) float64
- func EaseOutCubic(t float64) float64
- func EaseOutQuad(t float64) float64
- func Instant() bool
- func Linear(t float64) float64
- func PiecewiseAt(pts []LinearPt, t float64) float64
- func SetInstant(on bool) (restore func())
- func SpringEasingAt(zeta, omega, t float64) float64
- func StepsAt(n int, jump StepPos, t float64, before bool) float64
- type Bezier
- type Cancel
- type Easing
- type LinearPt
- type Spring
- type SpringParams
- type Step
- type StepPos
- type Tween
Constants ¶
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 ¶
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.
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 ¶
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 ¶
CubicBezierAt evaluates the CSS cubic-bezier(x1, y1, x2, y2) timing function at t: x is time, y progress, the curve's parameter solved for x (Newton where it converges, bisection where it does not). x1 and x2 clamp to [0, 1], the range CSS admits, so the curve stays a function of time; the y controls are free. Outside (0, 1) the output holds at the end it passed.
func EaseInCubic ¶
EaseInCubic starts slow and accelerates hard.
func EaseInOutCubic ¶
EaseInOutCubic eases both ends of the tween.
func EaseInOutQuad ¶
EaseInOutQuad accelerates through the first half, decelerates through the second.
func EaseOutBack ¶
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 ¶
EaseOutCubic is lys-ui's default curve: fast start, gentle landing.
func Instant ¶
func Instant() bool
Instant reports whether tweens collapse to their end state: the reduced-motion mode, on with LYS_UI_NO_ANIM=1 or SetInstant. Delays keep their durations, so timing survives and only motion goes away.
func PiecewiseAt ¶
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 SetInstant ¶
func SetInstant(on bool) (restore func())
SetInstant turns reduced motion on or off for later launches and returns the restore function.
func SpringEasingAt ¶
SpringEasingAt evaluates SpringEasing(zeta, omega) at t without building the curve.
func StepsAt ¶
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.
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.
type Easing ¶
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 ¶
CubicBezier returns the CSS cubic-bezier(x1, y1, x2, y2) timing function; see CubicBezierAt.
func Piecewise ¶
Piecewise returns the linear(...) easing over a copy of pts made canonical; see PiecewiseAt.
func SpringEasing ¶
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.
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 ¶
Position and Velocity are the spring's current state, per component; the slices are the spring's own.
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 ¶
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 ¶
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.
type SpringParams ¶
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).
type StepPos ¶
type StepPos uint8
StepPos is where a steps() curve jumps: at the start of each interval, the end, both edges, or neither.
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.