We strongly recommend controlling your animation(s) at runtime with a State Machine, rather than controlling them individually.
Rive lets you specify 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!
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.
Copy
Ask AI
// Play the idle animationnew rive.Rive({ src: 'https://cdn.rive.app/animations/vehicles.riv', canvas: document.getElementById('canvas'), animations: 'idle', autoplay: true});
Copy
Ask AI
// Play the idle animationnew rive.Rive({ src: 'https://cdn.rive.app/animations/vehicles.riv', canvas: document.getElementById('canvas'), animations: 'idle', autoplay: true});
Copy
Ask AI
// Play the idle animationexport const Simple = () => ( <Rive src="https://cdn.rive.app/animations/vehicles.riv" animations="idle" />);// With `useRive` Hook:export default function Simple() { const { RiveComponent } = useRive({ src: 'https://cdn.rive.app/animations/vehicles.riv', animations: ['idle'], autoplay: true, }); return <RiveComponent />;}
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.
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.
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.
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 painter that manages the state/painting/advancing.
In order to access controls for animations, you’ll need to create a SingleAnimationPainter or StateMachinePainter and pass it to the RiveArtboardWidget.
Or you can create RiveWidgetController and pass it to the RiveWidget to control playback of state machines with a more feature-rich API (recommended).
This painter concept is extensible, and you can also create your own custom painter, for example, to control multiple animations at once.
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).
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
Copy
Ask AI
class ToggleViewModel: RiveViewModel { private let onAnimation: String = "On" private let offAnimation: String = "Off" private let startAnimation: String = "StartOff" var action: ((Bool) -> Void)? = nil var isOn = false { didSet { stop() play(animationName: isOn ? onAnimation : offAnimation) action?(isOn) } } init() { super.init(fileName: "toggle", animationName: startAnimation, fit: .cover) } func view(_ action: ((Bool) -> Void)? = nil) -> some View { self.action = action return super.view().frame(width: 100, height: 50, alignment: .center) } // When an animation is played override func player(playedWithModel riveModel: RiveModel?) { if let animationName = riveModel?.animation?.name() {...} } // When an animation is paused override func player(pausedWithModel riveModel: RiveModel?) { if let animationName = riveModel?.animation?.name() {...} } // When an animation is stopped override func player(stoppedWithModel riveModel: RiveModel?) { if let animationName = riveModel?.animation?.name() {...} } // When an animation is looped override func player(loopedWithModel riveModel: RiveModel?, type: Int) { if let animationName = riveModel?.animation?.name() {...} } // When an animation is advanced override func player(didAdvanceby seconds: Double, riveModel: RiveModel?) {...}}
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.
Copy
Ask AI
// Play one animationanimationView.play("rollaround")// Set loop mode and directionanimationView.play("rollaround", Loop.ONE_SHOT, Direction.Backwards)animationView.pause()animationView.pause("bouncing")animationView.stop()animationView.stop("bouncing")
The Rive Android runtime also allows listener registration. Check out the events section in the
rive player for an example of how this works.
Copy
Ask AI
val listener = object : Listener { override fun notifyPlay(animation: PlayableInstance) { var text: String? = null if (animation is LinearAnimationInstance) { text = animation.name } } override fun notifyPause(animation: PlayableInstance) { var text: String? = null if (animation is LinearAnimationInstance) { text = animation.name } } override fun notifyStop(animation: PlayableInstance) { var text: String? = null if (animation is LinearAnimationInstance) { text = animation.name } } override fun notifyLoop(animation: PlayableInstance) { var text: String? = null if (animation is LinearAnimationInstance) { text = animation.name } }}animationView.registerListener(listener)