transition

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package transition provides widget enter/exit animations.

Transition wraps a widget and plays an animation when the widget appears (enters) or disappears (exits). Supported effect types include fade, slide, and scale.

Quick Start

wrapped := transition.Wrap(myWidget,
    transition.EnterEffect(transition.FadeIn()),
    transition.ExitEffect(transition.FadeOut()),
    transition.Duration(300 * time.Millisecond),
)

wrapped.Show()  // plays enter animation, then shows widget
wrapped.Hide()  // plays exit animation, then hides widget

Effects

Three built-in effect types are provided:

  • Fade: animate opacity from 0 to 1 (enter) or 1 to 0 (exit)
  • Slide: translate the widget from/to a given direction
  • Scale: scale the widget from/to a smaller size with fade

Effects can be combined by composing multiple property animations within a single Effect value.

Canvas Requirements

Fade effects require the canvas to implement OpacityPusher. If the canvas does not support opacity, fade effects are silently skipped (graceful degradation). Slide effects use widget.Canvas.PushTransform. Scale effects adjust the child widget bounds around the center point.

Retained Mode

During animation, the transition widget calls widget.WidgetBase.SetNeedsRedraw to request continuous redraws until the animation completes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direction

type Direction uint8

Direction specifies the origin or target edge for slide effects.

const (
	FromTop    Direction = iota // Slide in from top edge.
	FromBottom                  // Slide in from bottom edge.
	FromLeft                    // Slide in from left edge.
	FromRight                   // Slide in from right edge.
	ToTop                       // Slide out toward top edge.
	ToBottom                    // Slide out toward bottom edge.
	ToLeft                      // Slide out toward left edge.
	ToRight                     // Slide out toward right edge.
)

Slide directions for enter effects (From*) and exit effects (To*).

type Effect

type Effect struct {
	// OpacityStart is the starting opacity. Negative means opacity is not animated.
	OpacityStart float64
	// OpacityEnd is the ending opacity.
	OpacityEnd float64

	// TranslateXFraction is the starting X offset as a fraction of widget width.
	// For example, -1.0 means start one full width to the left.
	TranslateXFraction float64
	// TranslateYFraction is the starting Y offset as a fraction of widget height.
	TranslateYFraction float64

	// ScaleStart is the starting scale factor. Negative means scale is not animated.
	ScaleStart float64
	// ScaleEnd is the ending scale factor.
	ScaleEnd float64
}

Effect describes the visual transformation applied during a transition.

Each field pair (Start/End) defines the range of animation for that property. A negative Start value means the property is not animated. Effect is a value type; create instances via the provided constructors.

func FadeIn

func FadeIn() Effect

FadeIn returns an effect that fades opacity from 0 to 1.

func FadeOut

func FadeOut() Effect

FadeOut returns an effect that fades opacity from 1 to 0.

func None

func None() Effect

None returns an effect that performs no animation (instant transition).

func ScaleIn

func ScaleIn() Effect

ScaleIn returns an effect that scales from 0.8 to 1.0 with a fade in.

func ScaleOut

func ScaleOut() Effect

ScaleOut returns an effect that scales from 1.0 to 0.8 with a fade out.

func SlideIn

func SlideIn(dir Direction) Effect

SlideIn returns a slide-in effect from the given direction.

The widget slides from off-screen (one full dimension away) to its final position. Only From* directions are meaningful for enter effects; To* directions are treated as their From* equivalents.

func SlideOut

func SlideOut(dir Direction) Effect

SlideOut returns a slide-out effect toward the given direction.

The widget slides from its current position to off-screen. Only To* directions are meaningful for exit effects; From* directions are treated as their To* equivalents.

func (Effect) IsNone

func (e Effect) IsNone() bool

IsNone reports whether the effect performs no animation.

type OpacityPusher

type OpacityPusher interface {
	// PushOpacity pushes an opacity multiplier onto the draw state stack.
	// Opacity is in the range [0, 1]. Nested calls multiply.
	PushOpacity(opacity float64)

	// PopOpacity removes the most recently pushed opacity.
	PopOpacity()
}

OpacityPusher is an optional Canvas capability for opacity effects.

If the Canvas passed to Draw implements this interface, fade effects will use PushOpacity/PopOpacity. Otherwise, fade effects are silently skipped (graceful degradation).

type Option

type Option func(*transitionConfig)

Option configures a Transition widget.

func Duration

func Duration(d time.Duration) Option

Duration sets the animation duration for both enter and exit effects.

func Easing

func Easing(e animation.Easing) Option

Easing sets the easing function for both enter and exit effects. Defaults to animation.EaseOutCubic if not set.

func EnterEffect

func EnterEffect(e Effect) Option

EnterEffect sets the effect played when the widget appears.

func ExitEffect

func ExitEffect(e Effect) Option

ExitEffect sets the effect played when the widget disappears.

type Transition

type Transition struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Transition wraps a child widget with enter/exit animation effects.

When Show is called, the enter effect is played and the child becomes visible. When Hide is called, the exit effect is played and the child is hidden after the animation completes.

Transition implements widget.Widget.

func Wrap

func Wrap(child widget.Widget, opts ...Option) *Transition

Wrap creates a new Transition that wraps the given child widget.

By default the widget starts visible with no animation effects. Use EnterEffect, ExitEffect, Duration, and Easing options to configure the transition behavior.

wrapped := transition.Wrap(myWidget,
    transition.EnterEffect(transition.FadeIn()),
    transition.ExitEffect(transition.FadeOut()),
    transition.Duration(300 * time.Millisecond),
)

func (*Transition) Child

func (t *Transition) Child() widget.Widget

Child returns the wrapped child widget.

func (*Transition) Children

func (t *Transition) Children() []widget.Widget

Children returns the wrapped child as a single-element slice.

func (*Transition) Draw

func (t *Transition) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the child widget with transition effects applied.

func (*Transition) Event

func (t *Transition) Event(ctx widget.Context, e event.Event) bool

Event dispatches events to the child widget if visible.

func (*Transition) Hide

func (t *Transition) Hide()

Hide hides the widget, playing the exit effect if configured.

If an enter animation is currently playing, it is replaced by the exit animation starting from the beginning.

func (*Transition) IsAnimating

func (t *Transition) IsAnimating() bool

IsAnimating reports whether a transition animation is currently in progress.

func (*Transition) IsShown

func (t *Transition) IsShown() bool

IsShown reports whether the widget is logically visible (enter complete or in progress). Returns false if hidden or exit animation is complete.

func (*Transition) Layout

func (t *Transition) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the transition widget size by delegating to the child.

func (*Transition) Show

func (t *Transition) Show()

Show makes the widget visible, playing the enter effect if configured.

If an exit animation is currently playing, it is replaced by the enter animation starting from the beginning.

Jump to

Keyboard shortcuts

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