Cogent Core Docs

0%

Loading...

Static preview:

Animate

You can animate any widget by specifying an animation function, which is typically run at the refresh rate of the monitor.

The most commonly animated widget is a canvas:

Pause

If you want to temporarily pause an animation, you can simply return early from your animation function:

Pause

Also, animations associated with widgets that are currently not visible will automatically be paused.

Stop

You can permanently stop an animation by setting the core.Animation.Done field to true:

Also, animations associated with deleted widgets will automatically be permanently stopped.

Other widgets

Any type of widget can be animated:

0

Note: in that example, we use core.WidgetBase.UpdateRender instead of core.WidgetBase.Update to optimize performance. See update for more information. The min setting is necessary to give the text enough space without redoing the layout every frame.

Details

Note that, unlike for goroutines, the animation function is run in the main event loop and thus does not require any async protection. Using the animation API is better than using a goroutine since it automatically lines up with the app rendering timing, and it adapts to the screen refresh rate across platforms.

Using the core.Animation.Dt field of the animation allows the animation to run at the same speed across refresh rates; faster refresh rates will lead to a smoother animation, but the overall speed will be the same.

If the animation is too intensive for the system to keep up, the animation rate will be automatically reduced, so it is not guaranteed to be exactly the refresh rate of the monitor. As such, unlike a goroutine, an animation should not cause any app hanging, even on web.