Wisp GitHub

Embedding examples

Put an animation in your product.

Six ways in, from one HTML tag to a SwiftUI screen. The four web examples run the code they show, right beside it: press things. The iOS and Android examples show their code beside a recording from the simulator and the emulator, in the demo app.

Every web example loads the player from jsDelivr, pinned to version 0.2.5. In a bundler, run npm install wisp-anim and import load from wisp-anim/player.

Web · HTML

One tag

<wisp-player> takes src, pointer, fit, seed, autoplay and rest. It stops drawing when nothing moves.

HTML
<script
  src="https://cdn.jsdelivr.net/npm/wisp-anim@0.2.5/dist/wisp.player.iife.min.js">
</script>

<wisp-player
  src="./examples/ui-kit/like-button.wisp.json"
  pointer="canvas"
  style="width: 200px; height: 200px"></wisp-player>

Running: click the heart.

Web · JavaScript

Drive it from your page, and listen to it

load() returns a player. set changes an input, fire sends a trigger and on tells you what the animation did.

HTML and JavaScript
<canvas id="dash"
  style="width: 100%; aspect-ratio: 2 / 1"></canvas>
<label>Speed
  <input id="speed" type="range" min="0" max="60" value="0">
</label>
<button id="warn">Warning</button>
<output id="log">Move the slider</output>

<script type="module">
  import { load } from
    "https://cdn.jsdelivr.net/npm/wisp-anim@0.2.5/dist/wisp.player.min.js";

  const dash = document.querySelector("#dash");
  const player = await load(
    "./examples/cluster/cluster.wisp.json",
    dash, { pointer: "canvas" });

  // The page drives the animation…
  const speed = document.querySelector("#speed");
  speed.oninput = () =>
    player.set("speed", speed.valueAsNumber);
  const warn = document.querySelector("#warn");
  warn.onclick = () => player.fire("warn");

  // …and hears what it does.
  const log = document.querySelector("#log");
  player.on("input", (e) => {
    log.textContent = `${e.name} = ${e.value}`;
  });
  player.on("statechange", (e) => {
    log.textContent = `${e.layer}: ${e.to}`;
  });
</script>

Running: drag the slider, press Warning, watch the line under it.

Web · React

A React component

Twenty lines around the same player: it loads once per src and pushes your state into the animation's inputs.

WispAnimation.jsx
import { useEffect, useRef, useState } from "react";
import { load } from "wisp-anim/player";

// A Wisp animation as a React component. It loads once
// per `src` and pushes every change in `inputs` into the
// running animation.
export function WispAnimation(props) {
  const { src, inputs = {}, onEvent, style } = props;
  const canvas = useRef(null);
  const [player, setPlayer] = useState(null);

  useEffect(() => {
    let p = null, gone = false;
    const opts = { pointer: "canvas" };
    load(src, canvas.current, opts).then((loaded) => {
      if (gone) loaded.destroy();
      else setPlayer((p = loaded));
    });
    return () => {
      gone = true;
      p?.destroy();
      setPlayer(null);
    };
  }, [src]);

  useEffect(() => {
    for (const [name, value] of Object.entries(inputs)) {
      player?.set(name, value);
    }
  }, [player, inputs]);

  useEffect(() => {
    if (!player || !onEvent) return;
    return player.on("event", onEvent);
  }, [player, onEvent]);

  const size = { width: "100%", ...style };
  return <canvas ref={canvas} style={size} />;
}

// Using it: the switch follows your app's state.
export function LampSwitch() {
  const [on, setOn] = useState(false);
  return (
    <div>
      <WispAnimation
        src="./examples/hello/toggle.wisp.json"
        inputs={{ on }}
        style={{ aspectRatio: "300 / 180" }}
      />
      <button onClick={() => setOn(!on)}>
        {on ? "Turn it off" : "Turn it on"}
      </button>
    </div>
  );
}

Running: this is <LampSwitch /> from the file on the left, rendered by React 19 on this page.

Anywhere · one file

One self-contained HTML file

The export holds the player and the document in a single file with nothing to fetch. Attach it to an email, drop it in a CMS, or put it in an <iframe>.

Terminal
npx wisp-anim export mascot.wisp.json \
  --html embed-kibo.html
HTML
<iframe src="embed-kibo.html" title="Kibo"
  style="width: 100%; height: 600px; border: 0"></iframe>

Running: the file this command wrote when this site was built. Click Kibo, or press its poke button.

iOS and Android

The same file in a native app

Today a phone app builds the library from a clone of this repository with a script (scripts/build-xcframework.sh for iOS, scripts/use-core.sh for Android); prebuilt packages are not published yet. It has run on the iOS simulator and an Android emulator, not yet on a physical phone.

iOS, SwiftUI

OnboardingScreen.swift
import SwiftUI
import WispKit

@MainActor
struct OnboardingScreen: View {
    @State private var player: WispView?

    var body: some View {
        VStack {
            if let player {
                WispPlayerView(player: player)
                Button("Next") {
                    player.setNumber("page", 1)
                }
            } else {
                ProgressView()
            }
        }
        .task {
            guard player == nil else { return }
            player = try? WispView(
                resource: "onboarding.wisp.json")
        }
    }
}

WispKit is a Swift package on the Rust core (setup).

Recorded on the iOS simulator, in the demo app

Android, Jetpack Compose

YearInReviewScreen.kt
import ai.wisp.android.compose.Wisp

@Composable
fun YearInReviewScreen(name: String) {
    Wisp(
        asset = "recap.wisp.json",
        modifier = Modifier.fillMaxSize(),
        onReady = { view -> view.setString("name", name) },
    )
}

A Kotlin library with a Compose wrapper, on the same core (setup).

Recorded on an Android emulator, in the demo app