Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new InfiniteAnimation #5344

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,53 @@ func (a *Animation) Stop() {
CurrentApp().Driver().StopAnimation(a)
}

// InfiniteAnimation represents an animation that continues infinitely unless stopped. It has no duration
// or curve, and when started, the Tick function will be called every frame until Stop is invoked.
//
// Since: 2.6
type InfiniteAnimation struct {
Tick func()

isSetup bool
animation Animation
}

// NewInfiniteAnimation creates an infinite animation where the callback function will be called
// for every rendered frame once started, until stopped.
//
// Since: 2.6
func NewInfiniteAnimation(fn func()) *InfiniteAnimation {
return &InfiniteAnimation{Tick: fn}
}

// Start registers the animation with the application run-loop and starts its execution.
func (i *InfiniteAnimation) Start() {
i.setupAnimation()
i.animation.Start()
}

// Stop will end this animation and remove it from the run-loop.
func (i *InfiniteAnimation) Stop() {
i.setupAnimation()
i.animation.Stop()
}

func (i *InfiniteAnimation) setupAnimation() {
if i.isSetup {
return
}

i.animation = Animation{
Tick: func(_ float32) {
i.Tick()
},
Curve: AnimationLinear, // any curve will work
Duration: 1 * time.Second, // anything positive here will work
RepeatCount: AnimationRepeatForever,
}
i.isSetup = true
}

func animationEaseIn(val float32) float32 {
return val * val
}
Expand Down
Loading