Rive Web WASM Runtime

repository·master·Indexed 21 days ago

https://github.com/rive-app/rive-wasm

A high-performance JavaScript/TypeScript and WebAssembly runtime for the Rive design tool. It enables embedding interactive, real-time animations into web applications via high-level interaction APIs and low-level custom render loop controls. Includes the @rive-app/canvas helper and @rive-app/canvas-advanced-lite for low-level API access, multi-artboard rendering, and hierarchy manipulation.

Tokens
25.5K
Snippets
90
Records
128
Agent score
76%

What's inside rive-wasm

  1. Overview of @rive-app/canvas

    master

    The @rive-app/canvas package provides an easy-to-use, high-level Rive API that uses a CanvasRenderingContext2D renderer. This approach leverages the browser's native high-level vector graphics renderer instead of WebGL.

    Key Benefits

    • Small Download Size: Extremely lightweight compared to WebGL-based runtimes.
    • High Concurrency: Ideal for displaying many animated canvases simultaneously (e.g., in lists or grids) because it is not subject to the browser's WebGL context limits.
    • Feature Support: Supports simple vector graphics animations, raster images, and mesh deformations.
    • Automatic Dependency Management: Automatically requests the required WebAssembly (WASM) backing dependency for you.
  2. What is @rive-app/canvas-advanced-single?

    master

    The @rive-app/canvas-advanced-single package provides a low-level Rive API using the CanvasRenderingContext2D renderer. It is designed for developers who need more granular control than the standard @rive-app/canvas package provides.

    Key features include:

    • Full control over the update and render loop: Manually manage when Rive updates its state and draws to the canvas.
    • Multi-artboard rendering: The ability to render multiple Rive artboards onto a single canvas element.
    • Hierarchy manipulation: Deeper control and manipulation of individual components within a Rive hierarchy.
    • Inline WASM: The WebAssembly (WASM) binary is bundled directly within the JavaScript package, eliminating the need for separate runtime requests to load the WASM file.
  3. Overview of Rive Web runtime

    master

    Rive Web is a JavaScript/TypeScript and WebAssembly (WASM) runtime library designed to run Rive animations in web applications. It provides two levels of control:

    1. High-level API: Designed for simple interactions and hooking up animations easily.
    2. Low-level API: Allows developers to drive their own render loop, enabling the creation of multiple artboards, animations, and state machines within a single canvas.

    This runtime is used to load interactive motion graphics created in the Rive collaborative editor into websites and apps.

  4. Use @rive-app/canvas-single for high-level Canvas rendering

    master

    The @rive-app/canvas-single package provides an easy-to-use high-level Rive API that uses a CanvasRenderingContext2D renderer. This approach leverages the browser's native high-level vector graphics renderer.

    Key Benefits

    • Small Footprint: Extremely small download size.
    • High Concurrency: Ideal for displaying many animated canvases concurrently (e.g., in lists or grids) because it is not subject to the browser's WebGL context limits.
    • Feature Support: Supports simple vector graphics animations, raster images, and mesh deformations.
    • Zero Runtime WASM Requests: The WebAssembly (WASM) dependency is included directly in the JS bundle, so no additional network requests are required to load it at runtime.
  5. Understand the features of @rive-app/canvas-advanced-lite

    master

    The @rive-app/canvas-advanced-lite package provides a low-level Rive API using the CanvasRenderingContext2D renderer. It includes the same benefits as the standard @rive-app/canvas package but adds the following capabilities:

    • Full control over the update and render loop: You manage when the Rive engine updates and draws.
    • Multi-artboard rendering: Allows rendering multiple Rive artboards onto a single canvas.
    • Hierarchy manipulation: Provides deeper control and manipulation of components within a Rive hierarchy.
    • Manual WASM loading: While Web Assembly (WASM) is included in the NPM bundle, you are responsible for loading the WASM file manually.

    Note on Limitations: Because this is a lite version optimized for small package size, it does not include certain dependencies. Consequently:

    • It will not render Rive Text onto the canvas.
    • It will not play Rive Audio.

    Even if your Rive file contains Text or Audio components, the engine will not error out or stop rendering the rest of the graphic.

  6. Compare @rive-app/canvas-advanced-lite and @rive-app/canvas-advanced

    master

    Choose between the lite and standard advanced versions based on your requirements for package size and feature support:

    Feature@rive-app/canvas-advanced-lite@rive-app/canvas-advanced
    APILow-level CanvasRenderingContext2DLow-level CanvasRenderingContext2D
    Package SizeMinimal (optimized)Larger (includes all dependencies)
    Rive TextNot supportedSupported
    Rive AudioNot supportedSupported
    ControlFull control over loop/hierarchyFull control over loop/hierarchy
  7. Understand when to use @rive-app/canvas-lite

    master

    The @rive-app/canvas-lite package is a high-level Rive API designed for specific use cases where minimizing bundle size and maximizing the number of concurrent animations is a priority.

    Key Benefits:

    • Small Download Size: It has a significantly smaller footprint than the full @rive-app/canvas package because it excludes certain heavy dependencies.
    • High Concurrency: Since it uses CanvasRenderingContext2D instead of WebGL, it is not subject to the browser's WebGL context limits. This makes it ideal for rendering large lists or grids of Rive animations simultaneously.
    • Automatic Dependency Management: It automatically requests the required WebAssembly (WASM) backing dependency for you.

    Supported Features:

    • Simple vector graphics animations
    • Raster graphics
    • Mesh deformations

    Limitations:

    • No Rive Text: It will not render Rive Text components onto the canvas.
    • No Rive Audio: It will not play Rive Audio.
    • Note: Including text or audio in your Rive file will not cause errors or stop the rest of the animation from rendering.
  8. Implement Rive Layouts using the Canvas API

    master

    To use Rive's Layout system (which allows graphics to respond to window resizing or zooming), you must configure the Rive instance with specific fit and alignment settings:

    1. Set the fit property to Fit.layout.
    2. Update the artboard size dynamically as the window or container size changes.
    3. Use the align method and adjust the scaleFactor parameter to account for the device pixel ratio or to implement custom zoom levels.

    This example utilizes the high-level JS/TS API provided by @rive-app/canvas.

    // Conceptual usage based on the layout example
    // 1. Set fit to Fit.layout
    // 2. Update artboard size on resize
    // 3. Adjust scaleFactor in align() for zoom/DPI
  9. Rive Event Integration Patterns in Neostream Events

    master

    The Neostream Events sample demonstrates three primary patterns for integrating Rive events with web application logic:

    • Open URL Events: Triggering browser navigation (opening a URL) when a specific Rive event is fired.
    • HTML/DOM Synchronization: Using Rive events to update the DOM, such as detecting animation completion or changing CSS properties like the mouse cursor when hovering over clickable Rive elements.
    • Audio Synchronization: Triggering audio playback in sync with specific animation events to create immersive experiences.
  10. Migrate to parameterized constructors (v0.7.5 breaking change)

    master

    Starting with version 0.7.5, the Rive and Layout constructors underwent a breaking change. They no longer accept individual parameters. Instead, they require a single parameterized object.

    • Old way: Passing individual arguments to the constructor.
    • New way: Pass a single configuration object.

    Note: Layout.new and Rive.new were deprecated in favor of new Layout(params) and new Rive(params) using the same parameter object structure.

    // v0.7.5+ pattern
    const rive = new Rive({
      src: '...', 
      // other properties
    });
  11. Run the Neostream Events Sample

    master

    The Neostream Events Sample demonstrates how to synchronize Rive's interactive animations with web applications using various event types. To run the development environment locally, follow these steps:

    1. Clone the repository and navigate to the sample directory:
      cd neostream-events
    2. Install dependencies:
      npm install
    3. Start the development server:
      npm run dev
    cd neostream-events
    npm install
    npm run dev
  12. Implement Rive Layouts using Fit.layout

    master

    To achieve responsive layouts in Rive that react to window resizing or zooming, use the following approach:

    1. Set the fit property to Fit.layout.
    2. Update the artboard size dynamically as the window dimensions change.
    3. Use the align method and adjust the scaleFactor parameter to account for the device pixel ratio or to implement custom zoom levels for your graphics.