API docs for the Rive instance.
canvas
- (required) Canvas element to draw Rive animations onto.src?
- (optional) There are two optional ways to use src
: either via URL to the .riv
file, or a path to the public .riv
asset to use. One of src
or buffer
must be provided.
.riv
on some publicly accessible bucket/CDN (i.e. AWS, GCS, etc.), you can pass in the URL here.
.riv
file as a data URI. Depending on your bundle loader, you may need to use a plugin (i.e url-loader
for Webpack) to properly parse and load in .riv
files as a data URI string. See this project as a basic example on how to set this up.riv
public asset if bundled in your application. Note that this is not a relative path to the asset from wherever the current JS file is in. Treat the .riv
as any other asset bundled in your application, such as an image or font. If your JS is compiled and run at the root of your web application, you must specify the path from the root to the location of the asset. For example, if your asset is in /public/foo.riv
, and your JS is run from the root at /
, you would specify: src: '/public/foo.riv'
in this property.buffer?
- (optional) ArrayBuffer containing the raw bytes from a .riv file. One of src
or buffer
must be provided.riveFile?
- (optional) Lets you reuse a previously loaded Rive runtime file object without needing to fetch it again from the src
URL or reload it from the buffer
. This can improve performance by eliminating redundant network requests and loading times, especially when creating multiple Rive instances from the same source. Unlike the buffer
and src
parameters, which still require parsing under the hood to create a runtime file object, the file parameter uses an already parsed object, including any loaded assets.artboard?
- (optional) Name of the artboard to use.animations?
- (optional) Name or list of names of animations to play.stateMachines
or animations
parameter is provided, however, in a future major version of rive-wasm
, the default will be to play the first state machine it finds.stateMachines?
- (optional) Name or list of names of state machines to load.stateMachines
. Running multiple state machines of the same artboard at the same time may cause unintended consequences.rive-wasm
, stateMachines
will be a singular string you pass in.layout?
- (optional) Layout object to define how animations are displayed on the canvas.autoplay?
- (optional) If true, the animation will automatically start playing when loaded. Defaults to false.useOffscreenRenderer?
- (optional) Boolean flag to determine whether to use a shared offscreen WebGL context rather than create its own WebGL context for this instance of Rive. This is only relevant for the @rive-app/webgl
package. If you are displaying multiple Rive animations, it is highly encouraged to set this flag to true
. Defaults to false
.enableRiveAssetCDN?
- (optional) Allow the runtime to automatically load assets hosted in Rive’s CDN. Enabled by default.shouldDisableRiveListeners?
- (optional) Boolean flag to disable setting up Rive Listeners on the <canvas>
element, thus preventing any event listeners from being set up on the element.
<canvas>
element if there is no playing state machine, or a state machine without any Rive Listeners set up on the state machineisTouchScrollEnabled?
- (optional) For Rive Listeners, allows scrolling behavior to still occur on canvas elements when a touch/drag action is performed on touch-enabled devices. Otherwise, scroll behavior may be prevented on touch/drag actions on the canvas by default.automaticallyHandleEvents?
- (optional) Enable Rive Events to be handled by the runtime. This means any special Rive Event may have a side effect that takes place implicitly. For example, if during the render loop an OpenUrlEvent
is detected, the browser may try to open the specified URL in the payload. This flag is false
by default to prevent any unwanted behaviors from taking place. This means any special Rive Event will have to be handled manually by subscribing to EventType.RiveEvent
onLoad?
- (optional) Callback that gets fired when the .riv file loads.onLoadError?
- (optional) Callback that gets fired when an error occurs loading the .riv file.onPlay?
- (optional) Callback that gets fired when the animation starts playing.onPause?
- (optional) Callback that gets fired when the animation pauses.onStop?
- (optional) Callback that gets fired when the animation stops playing.onLoop?
- (optional) Callback that gets fired when the animation completes a loop.onStateChange?
- (optional) Callback that gets fired when a state change occurs.onAdvance?
- (optional) Callback that gets fired every frame when the Artboard has advanced.assetLoader?
- (optional) Callback that gets invoked for every asset detected in a Rive file (whether included or excluded). The callback is passed a reference to a Rive FileAsset and associated bytes for the file (if the asset is embedded in the file). In this callback, you’ll determine whether or not to load the asset in your app yourself, or have Rive do it for you. For more details and examples, see the Loading Assets page.play(names?: string | string[], autoplay?: true): void
Plays a specified linear timeline animation(s) or state machine via the passed-in name. Useful if you have either programmatically called pause()
or stop()
or set autoplay: false
when instantiating Rive. If no name is passed in, it plays all instantiated timeline animations or state machines (or the default animation if neither is instantiated).
Example:
pause(names?: string | string[]): void
Pauses a specified linear timeline animation(s) or state machine via the passed-in name. Useful if you want to programmatically pause the playing animation and pause the render loop. You may want to use this API too if the associated Rive instance’s <canvas>
element is scrolled offscreen. If no name is passed in, it pauses all instantiated timeline animations or state machines.
Example:
stop(names?: string | string[]): void
Stops a specified linear timeline animation(s) or state machine via the passed-in name. Useful if you want to programmatically stop the playing animation and render loop. You may want to use this API too if the associated Rive instance’s state machine is “done,” or in an exit state. If no name is passed in, it stops all instantiated timeline animations or state machines.
Example:
autoplay
property passed in.
Example:
on(type: EventType, callback: EventCallback): void
Similar to the Web API’s addEventListener
functionality on DOM elements, you can subscribe to specific “events” in a render loop cycle via providing an EventType
enum and a callback for the runtime to invoke with different parameters depending on the event you want to subscribe to.
EventType
has the following enums to subscribe to that may or may not trigger during the lifespan of the Rive animation/state machine:
off(type: EventType, callback: EventCallback): void
Similar to the Web API’s removeEventListener
functionality on DOM elements, you can unsubscribe to specific “events” in a render loop cycle via providing an EventType
enum and the callback reference that was registered via the on()
API.
removeAllEventListeners(type?: EventType): void
This effectively removes all event listening subscriptions for a single particular EventType
that may have been added via the on()
API.
scrub(animationNames?: string | string[], value?: number): void
Scrubs through (a) linear timeline animation(s) by a specified amount of seconds.
animations
property.cleanup(): void
This API is important to call, as it will stop the animation render loop, and clean up all created instances for the Rive file, artboard, linear timeline animation(s), state machine, and the renderer. The reason it’s important to delete these instances is because these entities hold generated C++ references through WASM behind-the-scenes, and therefore will not automatically get garbage collected like normal JS objects, and must be “cleaned up” manually, to prevent memory leaks. Once you are done with the Rive instance (i.e., a state machine has finished running, a user is navigating off the page, etc.), call .cleanup()
to ensure all underlying memory allocated is freed up.
cleanupInstances()
which will only delete the Artboard, animation, and state machine instances (but not the Rive file or renderer object, which you can still reuse).cleanup()
, but will only clean up instances for the Artboard, linear timeline animation(s), and/or state machine, thus allowing you to re-initialize a different Artboard from the same Rive file, different timeline animation(s), or a different state machine. You can do this with reset()
.
src
file). This API also implicitly cleans up existing references to the old animation(s)/state machine/artboard. The backing WASM code that powers the Rive Web (JS) runtime shouldn’t have to be loaded in again, so there should be no other network overhead unless you are loading in a new Rive file over the web.
resizeToCanvas(): void
This sets the layout bounds to the current canvas size. You may want to call this if your canvas is resized.
resizeDrawingSurfaceToCanvas(): void
This resets the <canvas>
width and height properties to render at its current bounding rect size (CSS height
and width
properties) with the browser’s devicePixelRatio
in mind. This prevents blurry output on high-dpi screens (i.e., a <canvas>
that is 500x500 on the page may render with an internal area size of 1000x1000 while still maintaining its original canvas size). This calls resizeToCanvas()
implicitly. It’s recommended to call this in the onLoad
callback.
width
and height
properties you want to set on the canvasstopRendering(): void
Stops the render loop, and can only be resumed with startRendering()
. This is useful for situations when the <canvas>
is not visible. The React runtime utilizes this for that particular situation implicitly.
startRendering()
Starts the render loop if it has been previously stopped. It will have no effect if the render loop is already active.
getTextRunValue(textRunName: string): string | undefined
Returns the text value of the text run component (from the hierarchy of your .riv
file) you want to retrieve from. Returns undefined
if the text run cannot be queried from the active Artboard (you may see console warnings in this case for further guidance).
setTextRunValue(textRunName: string, textValue: string): void
Sets the text value of the text run component (from the hierarchy of your .riv
file) you specify via textRunName
. You may see console warnings if the text run cannot be queried from the active Artboard, and thus the provided textValue
you want to set on the run may not be successful.
resolveAnimationFrame(): void
You only need to use this function if you are using Rive’s low-level APIs to build the render loop manually and are integrating Rive into your existing requestAnimationFrame()
loop
Resolves deferred drawing commands with the renderer. You must call this at the end of your render loop if you decide to use your own requestAnimationFrame()
loop and are adding Rive graphics into it. Otherwise, if you are using Rive’s wrapped rAF
loop (i.e., rive.requestAnimatimationFrame()
), you do not need to call this method, as Rive will worry about resolving any rendering calls related to Rive graphics.
See example usage below:
contents
is just a property on the Rive instance you can log to the console to see an object hierarchy of what was loaded in from the Rive file. This is useful to see what Rive file was loaded in, all the artboards associated with the file, animations and state machines, state machine inputs, and more. It’s also useful if you don’t have access to the file to inspect in the Rive editor directly.
Example:
<div>
in the top-right corner of the page, reading out the FPS. Call this after Rive has initialized in the onLoad
callback, or at another point in time.
disableFPSCounter(): void
Disables the FPS reporting for the runtime.
src
attribute in the Rive instance
true
if any animation is playing
true
if all instanced animations are paused
true
if no instanced animations are playing or paused
Layout
Example: