Animation Playback
Playing and pausing animations.
Rive lets you specify what artboard to use, what animations and state machines to mix and play and control the play/pause state of each animation.
The term animations may collectively refer to both animations and state machines. In this section, we explore how to deal with specific animation playback, rather than state machines.
If you are trying to coordinate multiple animations’ playback at runtime, consider using a state machine instead to do this for you!
Choosing an artboard
When a Rive object is instantiated, the artboard to use can be specified. If no artboard is given, the default artboard is used. Only one artboard can be used at a time.
Choosing starting animations
Starting animations can also be chosen when Rive is instantiated. The first animation on the artboard may play if one is not provided, or a state machine is not set.
Currently, with the React Native runtime, you can set one animation to autoplay at the start. Despite this, see below in the playback sections to see how you can mix multiple animations on playback functions.
By default, RiveViewModel
will automatically play the animation or state machine you’ve given it.
SwiftUI
UIKit
With the Android runtime, specify one animation with the riveAnimation
property.
Or
Controlling playback
Playback of each animation and state machine can be separately controlled. You can play and pause playback using the play
, pause
and stop
methods, either passing in the names of the animations you want to affect or passing in nothing which will affect all instanced animations.
Invoking Playback Controls
With the web runtime, you can provide callback functions to receive notification when certain animation events have occurred:
onLoad
when a rive file has been loaded and initialized; it’s now ready for playbackonPlay
when one or more animations play; provides a list of animationsonPause
when one or more animations pause; provides a list of animationsonStop
when one or more animations are stopped; provides a list of animationsonLoop
when an animation loops; provides the animation name
See the following Codesandbox link to try out the below code: https://codesandbox.io/p/sandbox/adoring-sea-n7m59f
Invoking Playback Controls
With the web runtime, you can provide callback functions to receive notification when certain animation events have occurred:
onLoad
when a rive file has been loaded and initialized; it’s now ready for playbackonPlay
when one or more animations play; provides a list of animationsonPause
when one or more animations pause; provides a list of animationsonStop
when one or more animations are stopped; provides a list of animationsonLoop
when an animation loops; provides the animation name
See the following Codesandbox link to try out the below code: https://codesandbox.io/p/sandbox/adoring-sea-n7m59f
Invoking Playback Controls
Very similarly to Web, you can pass in Rive params and callbacks for certain animation events. See the Web tab for some examples of callbacks you can set. Additionally, you can use the rive
object returned from the useRive
hook to invoke playback controls.
See the example below here: https://codesandbox.io/p/sandbox/adoring-sea-n7m59f
Invoking Playback Controls
To trigger animation playback controls, set a ref
on the Rive component rendered. Once the ref
is populated, you can trigger functions such as play
, pause
, etc. See the ref
method docs for React Native Rive Ref Methods.
Flutter handles things a little differently compared to the other runtimes due to its reactive nature.
Every animation and state machine in Flutter has an underlying controller that manages the state of each animation. When you pass a list of animation names to the RiveAnimation widget, it creates and manages controllers for each.
In order to access controls for animations, you’ll need to instantiate a RiveAnimationController for each animation and pass the controller to the RiveAnimation widget instead of its name. You can mix and match passing in controllers and names, but avoid passing in both for the same animation.
There are a number of controllers provided in the runtime that perform certain tasks. We’ll cover these as they become relevant.
Manually controlling a looping animation
SimpleAnimation is a basic controller that provides simple playback control of a single animation. With this controller, you can play, pause, and reset animations.
In the following example, the isActive
property of SimpleAnimation
is used to play and pause a single animation.
Repeatedly playing a one-shot animation
One-shot animations do not loop. OneShotAnimation is a controller that will automatically stop and reset a one-shot animation when it has played through so it can be repeatedly played as required.
The controller also provides two callbacks: onStart
and onStop
that will fire when the animation starts and stops playing respectively.
The example below demonstrates mixing animation names with controllers. The idle
and curves
animations are managed by the runtime and their looping animations will play continuously. bounce
is a one-shot and is triggered when the button is tapped. The runtime will then play the bounce
animation, mixing it cleanly with the looping animations.
Invoking Playback Controls
After creating a RiveViewModel
, you can invoke animation playback control methods on a reference to this view model.
Very often that will be all that is needed to display your Rive asset. However, we have some convenient controls for when you want more fine-grained control of when it plays and doesn’t.
You can also choose the loop mode of the animation as additional parameters as needed. Along with playing animations, you similarly have the ability to pause, stop, and reset animation(s).
Playing without
play(animationName: String? = nil, loop: Loop = .autoLoop, direction: Direction = .autoDirection)
animationName
- Name of the animation to playloop
- Loop mode to play the animation in. Possible values listed below:oneShot
- plays animation through onceloop
- plays through animation and repeats from the set starting timepingPong
- plays animation from start -> end, then end -> start on repeatautoLoop
(default) - plays through the loop setting set on the animation
direction
- Direction to play the animation inbackwards
- plays through animation timeline backwardforwards
- plays through animation timeline forwardsautoDirection
- plays through direction set on the animation
pause()
stop()
reset()
Play
If you set autoplay to false you can play the active animation or state machine very simply.
If there are multiple animations on the active artboard you can play a specific one.
Pause/Stop/Reset
Based on certain events in your app you may want to adjust the playback further.
Player Delegates
This runtime allows for delegates that can be set on the RiveViewModel
. You can use delegates to define functions that hook into when certain playback events are invoked. See the below class for how you can hook into the following playback events:
- played
- paused
- stopped
- advanced
- animation looped
Invoking Playback Controls
After setting the Rive Resource with your animation view, you can invoke animation playback control methods.
Along with programmatically playing an animation, you can also choose the loop mode and direction of the animation as additional parameters as needed. You can additionally pause or stop an animation as well.
Animation Event Listeners
The Rive Android runtime also allows listener registration. Check out the events section in the rive player for an example of how this works.
Check out this Activity example: LoopModeActivity.kt
Was this page helpful?