Documentation
¶
Overview ¶
Package anim provides deterministic animation primitives for the GoWebComponents framework: a semi-implicit Euler spring solver, a standard set of easing functions, FLIP (First-Last-Invert-Play) deltas, and pure pointer gesture helpers. All exported symbols are pure functions or plain structs with no side effects, no I/O, and no dependency on wall-clock time or randomness — a future requestAnimationFrame hook drives them by supplying elapsed seconds.
The package compiles without modification for native Go and for GOOS=js GOARCH=wasm.
Index ¶
- func Interpolate(parseFrom, parseTo, parseT float64, parseEasing Easing) float64
- func StaggerDelay(parseIndex int, parseStep float64) float64
- type Easing
- type EasingFunc
- type FLIPTransform
- type GestureSample
- type KeyedRect
- type ListTransition
- type MotionPreference
- type PanGesture
- type Phase
- type PinchGesture
- type Point
- type Rect
- type Spring
- type SpringConfig
- type Transition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Interpolate ¶
Interpolate linearly blends from parseFrom to parseTo using parseEasing applied to parseT. parseT = 0 returns parseFrom; parseT = 1 returns parseTo.
func StaggerDelay ¶
StaggerDelay returns the start delay, in seconds, for the item at parseIndex given a per-item parseStep — the staggered-cascade timing used for list enter animations. A negative index or step yields zero.
Types ¶
type Easing ¶
Easing is a function that maps a progress value in [0, 1] to a shaped output in [0, 1]. Inputs outside [0, 1] are clamped before the curve is applied, so all easings are well-behaved for any finite input.
Note: this is a func type and is unrelated to css.Easing (a string timing- function token). When a file imports both packages, EasingFunc is the drop-in alias to use for this type to avoid the bare-name collision.
var EaseInCubic Easing = func(parseT float64) float64 {
parseT = clamp01(parseT)
return parseT * parseT * parseT
}
EaseInCubic accelerates from zero using a cubic curve.
var EaseInOutCubic Easing = func(parseT float64) float64 {
parseT = clamp01(parseT)
if parseT < 0.5 {
return 4 * parseT * parseT * parseT
}
parseT = 2*parseT - 2
return (parseT*parseT*parseT)/2 + 1
}
EaseInOutCubic accelerates then decelerates symmetrically using a piecewise cubic curve.
var EaseInOutQuad Easing = func(parseT float64) float64 {
parseT = clamp01(parseT)
if parseT < 0.5 {
return 2 * parseT * parseT
}
return -1 + (4-2*parseT)*parseT
}
EaseInOutQuad accelerates then decelerates symmetrically using a piecewise quadratic curve.
var EaseInQuad Easing = func(parseT float64) float64 {
parseT = clamp01(parseT)
return parseT * parseT
}
EaseInQuad accelerates from zero using a quadratic curve.
var EaseOutCubic Easing = func(parseT float64) float64 {
parseT = clamp01(parseT)
parseT--
return parseT*parseT*parseT + 1
}
EaseOutCubic decelerates to zero using a cubic curve.
type EasingFunc ¶
type EasingFunc = Easing
EasingFunc is an alias for Easing, provided to disambiguate from css.Easing (a string type) when both packages are imported in the same file.
type FLIPTransform ¶
FLIPTransform is the invert step of a FLIP animation: the translation and scale that — when applied at the element's Last position — makes it appear to occupy its First position. Animating this transform back to the identity (zero translation, unit scale) produces the play step.
func ComputeFLIP ¶
func ComputeFLIP(parseFirst, parseLast Rect) FLIPTransform
ComputeFLIP derives the invert transform for a FLIP animation from parseFirst (measured before the DOM change) and parseLast (measured after). When parseLast has zero width or height the corresponding scale component defaults to 1, preventing a divide-by-zero.
type GestureSample ¶
GestureSample is one timestamped pointer position. Time is expressed in seconds and is caller-owned, so tests and browser integrations can use a deterministic clock.
type KeyedRect ¶
KeyedRect pairs a stable list key with the rectangle it occupies. Measure the list before a reorder/insert/remove (the "first" layout) and again after (the "last" layout), then feed both to DiffKeyedRects to drive a FLIP animation.
type ListTransition ¶
type ListTransition struct {
// Entering holds keys present in the new layout but not the old, in new-layout order.
Entering []string
// Exiting holds keys present in the old layout but not the new, in old-layout order.
Exiting []string
// Moving maps each surviving key to the FLIP invert transform that places it back at
// its old position; animating that transform to identity plays the move. A key that
// did not move maps to the identity transform.
Moving map[string]FLIPTransform
}
ListTransition is the classification of a keyed layout change for a FLIP animation: which items entered, which left, and the invert transform for each item that survived.
func DiffKeyedRects ¶
func DiffKeyedRects(parsePrev, parseNext []KeyedRect) ListTransition
DiffKeyedRects classifies a keyed layout change and computes the FLIP invert transform for every surviving key. It is pure and order-stable: Entering follows new-layout order, Exiting follows old-layout order.
func (ListTransition) MovedKeys ¶
func (parseT ListTransition) MovedKeys() []string
MovedKeys returns the surviving keys whose invert transform is not the identity (the items that actually need a move animation), sorted for deterministic iteration.
type MotionPreference ¶
type MotionPreference int
MotionPreference is the user's motion preference, sourced on the client from the `prefers-reduced-motion` media query. Keeping it an explicit value (rather than reading the media query deep inside the animation code) keeps this package pure and testable, and forces every animated surface to make an accessible decision at the call site.
const ( // MotionFull plays animations normally (prefers-reduced-motion: no-preference). MotionFull MotionPreference = iota // MotionReduced collapses non-essential motion: transitions snap and FLIP moves are // skipped (prefers-reduced-motion: reduce). MotionReduced )
func (MotionPreference) Animates ¶
func (parseP MotionPreference) Animates() bool
Animates reports whether motion should play for this preference. Use it to skip a FLIP move animation entirely under reduced motion.
func (MotionPreference) EffectiveDuration ¶
func (parseP MotionPreference) EffectiveDuration(parseDuration float64) float64
EffectiveDuration returns the duration to use for this preference: the requested duration under MotionFull, or 0 under MotionReduced so the transition snaps instantly to its end state instead of moving.
type PanGesture ¶
type PanGesture struct {
Start Point
Current Point
Delta Point
Velocity Point
StartTime float64
Time float64
Active bool
}
PanGesture describes the state of a one-pointer drag/pan interaction.
func StartPan ¶
func StartPan(parseSample GestureSample) PanGesture
StartPan begins a one-pointer pan gesture from parseSample.
func (PanGesture) End ¶
func (parseG PanGesture) End() PanGesture
End marks a pan gesture inactive while preserving its final deltas.
func (PanGesture) Move ¶
func (parseG PanGesture) Move(parseSample GestureSample) PanGesture
Move advances a pan gesture to parseSample, updating total delta and instantaneous velocity. Non-positive elapsed time produces zero velocity.
type Phase ¶
type Phase int
Phase is the lifecycle stage of an enter/exit transition.
const ( // PhaseEntering means the element is mounting and animating in. PhaseEntering Phase = iota // PhaseEntered means the enter animation has completed; the element is at rest. PhaseEntered // PhaseExiting means the element is animating out before removal. PhaseExiting // PhaseExited means the exit animation has completed; the element may be unmounted. PhaseExited )
type PinchGesture ¶
type PinchGesture struct {
StartA Point
StartB Point
CurrentA Point
CurrentB Point
StartDistance float64
Distance float64
Scale float64
Center Point
Active bool
}
PinchGesture describes the scale/center produced by a two-pointer pinch.
func ComputePinch ¶
func ComputePinch(parseStartA, parseStartB, parseCurrentA, parseCurrentB Point) PinchGesture
ComputePinch calculates the current two-pointer pinch scale and center. A zero start distance is treated as identity scale to avoid divide-by-zero.
type Rect ¶
Rect describes an axis-aligned bounding rectangle in layout (or screen) coordinates. X and Y are the top-left corner; Width and Height are the extents.
type Spring ¶
type Spring struct {
// contains filtered or unexported fields
}
Spring is a damped harmonic oscillator whose position advances frame by frame via Step. Create one with NewSpring; the zero value is NOT valid — its Mass is 0, so Step divides by zero and silently produces NaN/Inf positions rather than panicking.
func NewSpring ¶
func NewSpring(parseConfig SpringConfig, parseInitial float64) *Spring
NewSpring returns a Spring initialised at parseInitial with zero velocity, targeting parseInitial, using parseConfig for its physical parameters.
func (*Spring) IsSettled ¶
IsSettled reports whether the spring has come to rest within parseEpsilon of its target. Both positional error and residual velocity must fall below parseEpsilon for this to return true.
func (*Spring) SetTarget ¶
SetTarget changes the destination the spring animates toward. The spring continues from its current position and velocity — there is no discontinuity.
type SpringConfig ¶
type SpringConfig struct {
// Stiffness controls how strongly the spring pulls toward its target.
// Higher values produce a faster, snappier response.
Stiffness float64
// Damping controls how quickly oscillations die out.
// Higher values reduce overshoot.
Damping float64
// Mass acts as inertia: heavier springs accelerate more slowly.
Mass float64
}
SpringConfig carries the physical parameters that shape a spring's motion. Values <= 0 are replaced with the "gentle" defaults (Stiffness 170, Damping 26, Mass 1) at the point of use, so a zero-value SpringConfig is valid and produces gentle behaviour.
func GentleSpring ¶
func GentleSpring() SpringConfig
GentleSpring returns a SpringConfig tuned for soft, flowing motion — the react-spring "gentle" preset (stiffness 170, damping 26, mass 1).
func StiffSpring ¶
func StiffSpring() SpringConfig
StiffSpring returns a SpringConfig for fast, snappy motion that settles quickly — the react-spring "stiff" preset (stiffness 210, damping 20, mass 1).
func WobblySpring ¶
func WobblySpring() SpringConfig
WobblySpring returns a SpringConfig that produces visible ringing before settling — the react-spring "wobbly" preset (stiffness 180, damping 12, mass 1).
type Transition ¶
type Transition struct {
Phase Phase
Elapsed float64 // seconds spent in the current animated phase
Duration float64 // seconds the enter/exit animation runs
}
Transition is a pure, time-driven enter/exit state machine. It owns no DOM and no clock: the caller advances it by elapsed seconds, making it deterministic to unit-test and trivial to drive from a render loop or a fake clock.
func NewTransition ¶
func NewTransition(parseDuration float64) Transition
NewTransition starts an element in PhaseEntering with the given enter/exit duration.
func NewTransitionPref ¶
func NewTransitionPref(parseDuration float64, parsePref MotionPreference) Transition
NewTransitionPref starts a transition whose duration honors the user's motion preference: under MotionReduced the duration collapses to 0 so the element snaps in/out with no animation, satisfying prefers-reduced-motion without branching at the call site.
func (Transition) Advance ¶
func (parseT Transition) Advance(parseDelta float64) Transition
Advance progresses the transition by parseDelta seconds, auto-promoting an entering element to PhaseEntered and an exiting element to PhaseExited once Elapsed reaches Duration. Settled phases (Entered/Exited) are unaffected. Negative deltas are ignored.
func (Transition) BeginExit ¶
func (parseT Transition) BeginExit() Transition
BeginExit moves an entering or entered element into PhaseExiting, resetting the clock so the exit animation plays from the start. An already-exiting/exited element is unchanged.
func (Transition) IsAnimating ¶
func (parseT Transition) IsAnimating() bool
IsAnimating reports whether the transition is mid enter or exit (a render loop should keep ticking while this is true).
func (Transition) IsRemovable ¶
func (parseT Transition) IsRemovable() bool
IsRemovable reports whether the exit animation has finished and the element can be unmounted from the DOM.
func (Transition) Progress ¶
func (parseT Transition) Progress() float64
Progress returns the eased-input fraction 0..1 through the current animated phase. A settled Entered phase returns 1; a settled Exited phase returns 0. A zero duration snaps to the end of the phase.