Documentation
¶
Overview ¶
Package anim declares time-based animations: tweens with easing curves, combined into timelines with Sequence, Parallel and Delay, and launched with Play; physics springs (Spring), which run until they settle and keep their velocity when retargeted; and open-ended drivers (Drive). Every launch names its Owner, usually the widget that animates: it runs on the Clock of the application whose window shows that widget, which the application loop advances once per frame and parks on when nothing runs. 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 NoFades() bool
- func PiecewiseAt(pts []LinearPt, t float64) float64
- func Reduced() bool
- func SetInstant(on bool) (restore func())
- func SetReduced(on bool)
- func SpringEasingAt(zeta, omega, t float64) float64
- func StepsAt(n int, jump StepPos, t float64, before bool) float64
- type Bezier
- type Cancel
- type Clock
- type Easing
- type Launch
- type LinearPt
- type Owner
- type Pace
- 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.
const FallbackInterval = time.Second / 4
FallbackInterval is the period a running clock ticks at while a surface waits on a frame callback, or no surface can show it: the callback paces it when the compositor answers, and an occluded window's animations still land.
const FrameInterval = time.Second / 60
FrameInterval is the period the timer paces a running clock at while no surface waits on a frame callback.
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: see lys-lib's motion.CubicBezier, whose solver this is.
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 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 NoFades ¶
func NoFades() bool
NoFades reports whether even fades collapse: LYS_UI_NO_ANIM=1 or SetInstant.
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 Reduced ¶
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 ¶
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 ¶
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.
func Drive ¶
Drive runs fn on o's clock once per frame, with the frame's instant, until it reports false or the returned Cancel runs: an animation with no fixed end, which settles when it settles.
func Play ¶
Play launches steps as one timeline owned by o, several steps running in sequence, and returns its Cancel. A step with no duration at the timeline's start lands at once, inside Play; the rest start on o's clock, at once or once o has one. The schedule is fixed at that start, so ticks land on exact offsets however frames arrive.
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is the schedule of one application's animations: the timelines, springs and drivers launched onto it, the frame deadlines they need and the tick that advances them. An app.Application owns one and its loop alone ticks it; a widget's animations land on the clock of the application whose window shows the widget. A Clock is an Owner of its own: what is scheduled on it starts on it at once. Stop ends it for good. The zero Clock is empty and ready.
func (*Clock) Due ¶
Due reports whether c's deadline at pace has come by now, so a pass that was not woken by a frame callback ticks it; a pass woken by anything else leaves running timelines where they are.
func (*Clock) Next ¶
Next returns the earliest instant the loop must wake to keep c running at pace: one interval past the last tick while a timeline is mid-flight, or a later step's start while a timeline holds in a delay. At PaceTimer a step's end counts too, so its last frame lands on time. False means nothing runs and the loop may park.
func (*Clock) ScheduleAnimation ¶
ScheduleAnimation starts l on c.
func (*Clock) Stop ¶
func (c *Clock) Stop()
Stop drops every timeline and driver on c and refuses every later launch: the clock of an application that closed runs nothing again.
func (*Clock) Tick ¶
Tick advances everything on c to now and drops what finished or was cancelled. Time never runs back: a now older than the last tick, or than the last launch (a compositor timestamp lags the local clock), counts as that instant. Callbacks run after the pass settles with the lock released, so they may launch or cancel freely; a callback cancelling its own timeline still stops that timeline's later steps in the same tick. It reports whether any callback ran.
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 Launch ¶
type Launch struct {
// contains filtered or unexported fields
}
Launch is one animation on its way to a clock: an Owner starts it on the clock that runs it. It starts once at most, and not at all once cancelled.
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 Owner ¶
type Owner interface {
// ScheduleAnimation starts l on the owner's clock, now or once the
// owner has one, or never.
ScheduleAnimation(l *Launch)
}
Owner is what an animation belongs to, and where the clock that runs it comes from. Every widget is one: its animations run on the clock of the application whose window shows it, wait while it is in no window and start once it is, and never run for a window that closed. An app.Application is one, for what belongs to no widget, and so is a Clock.
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 its owner's clock: onFrame sees every frame's position, and must not keep the slice.
func NewSpring ¶
func NewSpring(o Owner, p SpringParams, initial []float64, onFrame func(pos []float64)) *Spring
NewSpring returns a spring owned by o, 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 on its owner's clock until it settles; it counts as running while it waits for that clock. 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.