webdevpuneet.com

Rive Interactive Icon — Free Rive Runtime Snippet With SVG Fallback

Rive Interactive Icon · Animations · Plain HTML, CSS & JS · Live preview

CategoryAnimations

Copy & paste

Source Code

Requires
<div class="ri-wrap">
  <div class="ri-stage">
    <canvas id="riCanvas" class="ri-canvas" width="220" height="220" hidden></canvas>
    <svg id="riFallback" class="ri-fallback" viewBox="0 0 100 100" width="220" height="220">
      <circle class="ri-ring" cx="50" cy="50" r="34" />
      <path id="riCheck" class="ri-check" d="M32 52 L45 65 L70 35" />
    </svg>
  </div>
  <p class="ri-status" id="riStatus">Loading Rive animation…</p>
  <button class="ri-btn" id="riReplay">Replay</button>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Real Rive runtime API
new rive.Rive with src, canvas, stateMachines.
Guaranteed fallback
An animated SVG always has something to show.
Error-safe loading
try/catch plus onLoadError cover both failure modes.
State-machine ready
Structured for stateMachineInputs-driven logic.
Never-blank preview
Exactly one of Rive/fallback is visible at all times.
Replay control
One button resets whichever animation is active.
Status messaging
Text label reports load state for transparency.
Small dependency footprint
Only the Rive canvas runtime is required.

About this UI Snippet

Rive Interactive Icon — The Rive Runtime Pattern, With a Fallback That Never Blanks

Screenshot of the Rive Interactive Icon snippet rendered live

Rive is a design tool and runtime for small, state-machine-driven vector animations — icons that react to hover, loading states that blend between phases, character animations with real logic behind them — exported as a compact .riv binary and played back in the browser with new rive.Rive({ src, canvas, stateMachines }). This snippet demonstrates that exact loading pattern, but since a live sandbox has no server to host a custom .riv file on, it points src at Rive's own public example asset and — critically — wraps the whole load in error handling with an always-available animated SVG checkmark as a fallback, so the preview is never blank even if that external asset is unreachable.

The real Rive API surface

new rive.Rive({ src, canvas, autoplay, stateMachines, onLoad, onLoadError }) is exactly how you'd load Rive in production. stateMachines names the state machine (built visually in the Rive editor) that drives the animation's logic — timelines, blend states, inputs — rather than a single fixed timeline. onLoad fires once the .riv file has been fetched and parsed; onLoadError fires if it can't be. Both are used here for real, not simulated.

Why the fallback matters

Embedding a third-party asset URL in a public snippet is fragile — the demo CDN could move, rate-limit, or go offline, and a broken Rive load would otherwise leave a blank canvas with no explanation. This snippet wraps instantiation in try/catch and always renders a small self-contained SVG (a circle with an animated checkmark stroke, using stroke-dasharray/stroke-dashoffset) underneath, toggling visibility so exactly one of "real Rive" or "fallback icon" is showing at any time — never neither.

A pattern worth reusing beyond Rive

Any snippet that depends on a hosted binary asset — a .riv file, a .lottie file, a font, a texture — benefits from the same shape: attempt the real load, catch both synchronous construction errors and the library's own async error callback, and always have a pure-CSS/SVG stand-in ready to show. See Lottie hover icon button for the same guarantee applied to an inline Lottie animation instead of a fallback SVG.

Where Rive fits vs. Lottie

Lottie plays back an After Effects timeline; Rive is closer to a tiny game engine for vector graphics, with state machines and runtime inputs (useful for things like a play/pause icon that blends between states, or a like-button burst with actual physics). Pick Rive when interaction state needs to drive the animation's logic, not just trigger a linear play.

Customizing it

Swap RIVE_SRC for your own exported .riv file's URL, point stateMachines at the state machine name from your file, and use riveInstance.stateMachineInputs(...) to wire booleans/triggers to hover or click events. Redesign the SVG fallback to visually match your real Rive asset so the transition between "still loading" and "loaded" feels seamless.

Step by step

How to Use

  1. 1
    Add the Rive runtime CDNInclude @rive-app/canvas from unpkg.
  2. 2
    Paste HTML, CSS, and JSA canvas and an SVG fallback both render, one hidden.
  3. 3
    Watch it loadThe Rive asset plays on the canvas if it loads.
  4. 4
    If the asset failsThe animated SVG checkmark shows instead, never blank.
  5. 5
    Click "Replay"Resets and replays whichever is currently active.
  6. 6
    Swap in your own .rivChange RIVE_SRC to your exported file's URL.

Real-world uses

Common Use Cases

Reactive interface icons
Build icons that respond to hover, loading or click using a state machine, loaded through the real new rive.Rive runtime.
Loading indicators with phases
Blend between loading phases with a state machine instead of looping a single animation, so progress feels responsive to real events.
Lightweight onboarding illustrations
Deliver small vector animations in a compact .riv file, with a guaranteed animated SVG shown if the file fails to load.
Like and reaction buttons
Pair with a confetti button so a reaction icon animates first and then bursts when the user clicks it.
Fallback pattern reuse
Reuse the error-safe loading approach alongside a Lottie hover icon button, with try/catch and onLoadError covering both failure paths.

Got questions?

Frequently Asked Questions

A live code sandbox has no server to host a custom exported .riv binary on, so this snippet points at one of Rive's own publicly hosted example files purely to demonstrate the real loading API working end to end. In your own project, replace RIVE_SRC with the URL of a .riv file you've exported from the Rive editor and uploaded to your own hosting or CDN.

The rive.Rive constructor call is wrapped in try/catch to guard against synchronous errors, and the onLoadError callback handles asynchronous failures like a network error or the CDN being unreachable. Either path calls the same playFallback() function, which hides the canvas, shows the SVG checkmark icon, and restarts its CSS keyframe animation — so the preview always has something animating, never a blank box.

Rive files can define state machines visually in the Rive editor — named states, transitions, and inputs (booleans, triggers, numbers) that drive which part of the animation plays based on logic rather than a single fixed timeline. Passing stateMachines: "State Machine 1" tells the runtime to drive playback through that state machine; in a real icon you'd typically also call riveInstance.stateMachineInputs() to read or set those inputs from hover/click handlers.

Lottie and GIF both play a fixed, pre-rendered timeline. Rive's state machines let runtime code drive the animation's logic — blending between states, responding to numeric inputs, branching based on booleans — which is closer to a tiny animated state machine than a video loop. That flexibility is the tradeoff for a slightly heavier runtime and the need to design the state machine in the Rive editor first.

Yes. Set autoplay: false in the constructor, then in a mouseenter listener call riveInstance.play() (or set a boolean state machine input to true), and in mouseleave call riveInstance.pause() or reset the input to false. The onLoad callback is the right place to attach those listeners, since riveInstance isn't guaranteed usable until the file has finished loading.