npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zplata/rive-react

v1.0.0

Published

TEST - Don't use this. React wrapper around the rive-js library

Downloads

4

Readme

Build Status Discord badge Twitter handle

Rive React

React Runtime for Rive.

A wrapper around Rive.js, providing full control over the js runtime while making it super simple to use in React applications.

Detailed runtime documentation can be found in Rive's help center.

Create and ship interactive animations to any platform

Rive is a real-time interactive design and animation tool. Use our collaborative editor to create motion graphics that respond to different states and user inputs. Then load your animations into apps, games, and websites with our lightweight open-source runtimes.

Installation

npm i --save rive-react

Note: This library is using React hooks so the minimum version required for both react and react-dom is 16.8.0.

Usage

Component

Rive React provides a basic component as it's default import for displaying simple animations.

import Rive from 'rive-react';

function Example() {
  return <Rive src="loader.riv" />;
}

export default Example;

Props

  • src: File path or URL to the .riv file to display.
  • artboard: (optional) Name to display.
  • animations: (optional) Name or list of names of animtions to play.
  • layout: (optional) Layout object to define how animations are displayed on the canvas. See Rive.js for more details.
  • All attributes and eventHandlers that can be passed to a div element can also be passed to the Rive component and used in the same manner.

useRive Hook

For more advanced usage, the useRive hook is provided. The hook will return a component and a Rive.js Rive object which gives you control of the current rive file.

import { useRive } from 'rive-react';

function Example() {
  const params = {
    src: 'loader.riv',
    autoplay: false,
  };
  const { RiveComponent, rive } = useRive(params);

  return (
    <RiveComponent
      onMouseEnter={() => rive && rive.play()}
      onMouseLeave={() => rive && rive.pause()}
    />
  );
}

export default Example;

Parameters

  • riveParams: Set of parameters that are passed to the Rive.js Rive class constructor. null and undefined can be passed to conditionally display the .riv file.
  • opts: Rive React specific options.

Return Values

  • RiveComponent: A Component that can be used to display your .riv file. This component accepts the same attributes and event handlers as a div element.
  • rive: A Rive.js Rive object. This will return as null until the .riv file has fully loaded.
  • canvas: HTMLCanvasElement object, on which the .riv file is rendering.
  • setCanvasRef: A callback ref that can be passed to your own canvas element, if you wish to have control over the rendering of the Canvas element.
  • setContainerRef: A callback ref that can be passed to a container element that wraps the canvas element, if you which to have control over the rendering of the container element. For the vast majority of use cases, you can just the returned RiveComponent and don't need to worry about setCanvasRef and setContainerRef.

riveParams

  • src?: (optional) File path or URL to the .riv file to use. One of src or buffer must be provided.
  • buffer?: (optional) ArrayBuffer containing the raw bytes from a .riv file. One of src or buffer must be provided.
  • artboard?: (optional) Name of the artboard to use.
  • animations?: (optional) Name or list of names of animations to play.
  • stateMachines?: (optional) Name of list of names of state machines to load.
  • layout?: (optional) Layout object to define how animations are displayed on the canvas. See Rive.js for more details.
  • autoplay?: (optional) If true, the animation will automatically start playing when loaded. Defaults to false.
  • onLoad?: (optional) Callback that get's fired when the .rive file loads .
  • onLoadError?: (optional) Callback that get's fired when an error occurs loading the .riv file.
  • onPlay?: (optional) Callback that get's fired when the animation starts playing.
  • onPause?: (optional) Callback that get's fired when the animation pauses.
  • onStop?: (optional) Callback that get's fired when the animation stops playing.
  • onLoop?: (optional) Callback that get's fired when the animation completes a loop.
  • onStateChange?: (optional) Callback that get's fired when a state change occurs.

opts

  • useDevicePixelRatio: (optional) If true, the hook will scale the resolution of the animation based the devicePixelRatio. Defaults to true. NOTE: Requires the setContainerRef ref callback to be passed to a element wrapping a canvas element. If you use the RiveComponent, then this will happen automatically.
  • fitCanvasToArtboardHeight: (optional) If true, then the canvas will resize based on the height of the artboard. Defaults to false.

useStateMachineInput Hook

The useStateMachineInput hook is provided to make it easier to interact with state machine inputs on a rive file.

import { useRive, useStateMachineInput } from 'rive-react';

function Example() {
  const STATE_MACHINE_NAME = 'button';
  const INPUT_NAME = 'onClick';

  const { RiveComponent, rive } = useRive({
    src: 'button.riv',
    stateMachines: STATE_MACHINE_NAME,
    autoplay: true,
  });

  const onClickInput = useStateMachineInput(
    rive,
    STATE_MACHINE_NAME,
    INPUT_NAME
  );

  // This example is using a state machine with a trigger input.
  return <RiveComponent onClick={() => onClickInput.fire()} />;
}

export default Example;

See our examples folder for working examples of Boolean and Number inputs.

Parameters

  • rive: A Rive object. This is returned by the useRive hook.
  • stateMachineName: Name of the state machine.
  • inputName: Name of the state machine input.

Return Value

A Rive.js stateMachineInput object.

Examples

The examples shows a number of different ways to use Rive React. See the instructions for each example to run locally.