Skip to content
Kite

Animation

Pass two sparse styles to el.Animate(from, to, animation.Options{Duration: time.Second}). Only the animated properties are merged into the current author style on each frame. Matching dimensions, colors, spacing and grid tracks interpolate; discrete properties and mismatched units switch halfway through eased progress.

For an entrance animation, register panel.OnConnect(replay) before mounting. The panel can call Animate from that callback without an engine reference, and the same callback can power a Replay button.

from := style.S().Width(style.Cells(8))
to := style.S().Width(style.Cells(32))
panel := element.Box("Loading").Style(
from.
Background(style.Hex("#0f766e")).
Foreground(style.Hex("#f8fafc")).
Bold(true),
)
var play *animation.Playback
replay := func() {
play.Cancel()
opts := animation.Options{
Duration: 1200 * time.Millisecond,
}
play = panel.Animate(from, to, opts)
}
panel.OnConnect(replay)
button := element.Button("Replay").
Style(demo.Default.PrimaryButton).
OnPress(replay)

Easing defaults to animation.EaseOutQuad. Set Options.Easing to change the curve or Options.OnComplete to run code after the final update. Keep the returned playback and call Cancel() before replaying. Cancellation leaves the last applied style in place and skips completion. A disconnected element stops updating on its next tick.

c := demo.Default.Colors
label := element.Text("Ready")
badge := element.Box(label).Style(
style.S().
Background(c.AccentSoft).
Padding(0, 1),
)
from := style.S().Background(c.AccentSoft)
to := style.S().Background(style.Hex("#0f766e"))
var play *animation.Playback
replay := element.Button("Replay").Style(demo.Default.Button)
replay.OnPress(func() {
play.Cancel()
label.SetData("Deploying...")
opts := animation.Options{
Duration: 1200 * time.Millisecond,
Easing: animation.EaseInOutCubic,
OnComplete: func() {
label.SetData("Deployment complete")
},
}
play = badge.Animate(from, to, opts)
})
from := []style.GridTrackSize{style.Cells(7), style.Fr(1)}
to := []style.GridTrackSize{style.Cells(18), style.Fr(1)}
sidebar := element.Box("Sidebar").Style(
style.S().Background(demo.Default.Colors.AccentSoft),
)
tracks := style.S().
Width(style.Cells(30)).
Display(style.DisplayGrid).
GridTemplateColumns(from...)
grid := element.Box(sidebar, element.Box("Content")).Style(tracks)
toggle := element.Button("Toggle").Style(demo.Default.Button)
var play *animation.Playback
toggle.OnPress(func() {
play.Cancel()
current := grid.RawStyle().GridTemplateColumnsOpt().Value()
opts := animation.Options{
Duration: time.Second,
}
play = grid.Animate(
style.S().GridTemplateColumns(current...),
style.S().GridTemplateColumns(to...),
opts,
)
from, to = to, from
})

Drive a custom value or schedule a single frame

Section titled “Drive a custom value or schedule a single frame”

For a counter or scroll position, pass a tween to animation.Play. Its first tick has zero elapsed time; subsequent ticks use the window’s frame timestamps. A custom animation.Animator can return true from Tick to finish.

win := doc.Terminal().Window()
tween := animation.Int(0, 100, time.Second, func(y int) {
panel.ScrollTo(0, y)
})
playback := animation.Play(win, tween)

For a one-shot callback, use cancel := win.RequestAnimationFrame(func(now time.Time) { ... }). Call cancel() before execution to skip it. All callbacks in a frame receive the same timestamp; requesting another frame inside a callback schedules it for the next frame.

Symbol Kind Behavior
animation.Linear EasingFunction Constant speed (t)
animation.EaseInQuad EasingFunction Accelerates from zero velocity (t * t)
animation.EaseOutQuad EasingFunction Decelerates to zero velocity; default for Int, Float, Color
animation.EaseInOutCubic EasingFunction Softens both acceleration and deceleration
animation.IntInterpolator Interpolator[int] Linearly interpolates int values (shorthand: animation.Int)
animation.FloatInterpolator Interpolator[float64] Linearly interpolates float64 values (shorthand: animation.Float)
animation.ColorInterpolator Interpolator[color.Color] Interpolates alpha-premultiplied RGBA64 channels (shorthand: animation.Color)
animation.InterpolateGridTracks Interpolator[[]style.GridTrackSize] Interpolates matching numeric grid tracks; snaps mismatched kinds or lengths at 0.5