@fungi.computer/sprite

中文文档
0.2.2发布于 2026/11/09MITTypeScriptES Module近 30 天下载 1.2万

Typed renderer-bounded presentation for Fungi agents

原始使用文档
文档目录9 节
  1. Install or set up
  2. Quick start
  3. Core concepts
  4. Saved avatars
  5. Decorative drawings
  6. API reference
  7. Examples and checks
  8. Limits and errors
  9. Contribute and license

Sprite (@fungi.computer/sprite)

Sprite presents a character document through a host-installed renderer, so a game or app can show an animated avatar without writing its own rendering layer. The host chooses local characters, renderer adapters, network origins, and asset limits. Each mounted player owns its current source and presentation lifetime.

Install or set up

npm install @fungi.computer/sprite

The package ships ESM only and depends on Effect, Zod, and Cairn at runtime. React is an optional peer dependency, needed only for the ./react entrypoint. The core entrypoint, @fungi.computer/sprite, exports the renderer-neutral player API. @fungi.computer/sprite/react exports the React provider and component. @fungi.computer/sprite/video exports the optional video renderer. ./avatar supplies avatar descriptors and catalog-aware validation. ./ink and ./mycelium mount decorative drawings independently of the character player. Zdog support ships as the separate Sprite Zdog package.

Quick start

This illustrative React example configures the host's asset policy and lazy video renderer, then presents a character document:

import { createSprite } from "@fungi.computer/sprite";
import { Sprite, SpriteProvider } from "@fungi.computer/sprite/react";

const service = createSprite({
  assets: {
    baseURL: window.location.href,
    allowedOrigins: [window.location.origin],
    maxBytes: 2_000_000,
    maxTotalBytes: 4_000_000,
    maxRequests: 4,
  },
  renderers: {
    video: () => import("@fungi.computer/sprite/video"),
  },
});

function Character() {
  return (
    <SpriteProvider service={service}>
      <Sprite
        src="/characters/my-avatar.json"
        activity="idle"
        role="img"
        aria-label="My avatar"
      />
    </SpriteProvider>
  );
}

The package tests exercise player updates, React lifetime, and asset admission. See src/player.test.ts, src/react.test.tsx, and src/assets.test.ts.

Core concepts

A character document has version: 1, a name, a renderer name, optional controls and poster, and renderer-specific data. The shared schema validates that envelope. The selected adapter validates its own data. A source is either a document URL or { local: "key" } for a character installed by the host.

createSprite creates a stable host service. The host supplies local characters, renderer loaders, and asset policy. A player selects one source and receives activity, expression, appearance, and render-style updates without replacing its host element. The React Sprite owns one player and disposes it when it unmounts. The imperative player exposes set, pause, subscribe, and asynchronous dispose.

A presentation may carry performance, renderer data for that presentation such as a scene. Sprite passes it unchanged to the native renderer and compares it by identity: an equal value in a new object applies again. A host must keep the reference stable (for example with useMemo), or the renderer's set runs on every render. A renderer reports its own progress through reportProgress on its context. The ready state carries the latest value as progress, and the report is ignored once the selection closes. Sprite never reads either value. The selected adapter owns both schemas, as it owns document.data.

renderStyle applies one of the built-in SVG filters: plain, pencil, manga, riso, halftone, blueprint, crt, or watercolor. Each player owns its filter instance. Animated styles pause when the player is paused, is offscreen, the page is hidden, or reduced motion is requested.

Remote documents and nested assets pass through one reader. The host sets allowed origins, the maximum bytes per response, total bytes per selection, and request count. Local characters need no remote asset policy. Video uses the configured video renderer and reads clip assets through that same path.

The React Sprite takes src, optional activity, expression, appearance, renderStyle and performance, plus ordinary attributes for its host div. Pass paused to suspend it and onState to observe player states. A direct service prop overrides SpriteProvider; one of them is required.

For frame-driven playback, wrap an App in <AnimationTimeProvider timeMs={frame / fps * 1000}> from the same React entry. Every nested Sprite and Parakeet AgentHead inherits that clock, including memoized children. An explicit timeMs prop overrides it (zero included). Controlled players stay paused between seeks; omitting the provider time restores their ordinary live or paused behavior. No provider means no clock override.

Saved avatars

@fungi.computer/sprite/avatar defines the v1 descriptor: v, character, appearance, expression and renderStyle. avatarEnvelopeSchema checks only that envelope. Use createAvatarSchema(catalog) or parseAvatar(data, catalog) to also validate the selected character's controls and expressions. Sprite Zdog supplies a validator bound to its built-in catalog.

Decorative drawings

mountInkDrawing(host, { art, palette, ...options }) from ./ink paints traced art with Canvas 2D. The palette is a callback; call the returned redraw() after a theme change and destroy() when the host leaves.

mountMycelium(host, { pixi, palette, ...options }) from ./mycelium returns a Promise for a growing field with pulse() and destroy(). The host supplies Pixi 8 and the palette. Both drawings respect reduced motion and release their canvas and observers through destroy().

API reference

See the generated Sprite API reference. Import the renderer-neutral player API from @fungi.computer/sprite, the React provider and component from @fungi.computer/sprite/react, and the video adapter from @fungi.computer/sprite/video. The reference also covers ./avatar; the decorative ./ink and ./mycelium contracts are described above.

Examples and checks

The video workbench story presents a character with multiple clips and changes the activity to select a clip. Start the local Ladle workbench from the repository root with:

pnpm --filter @fungi.computer/sprite exec ladle serve

Run the package tests with:

pnpm --filter @fungi.computer/sprite test

Limits and errors

Sprite validates the shared document envelope, not renderer-specific data. The host must configure the renderer named by a served document and review its asset policy. Without an asset policy, remote origins are denied. Video codec support depends on the browser and the encoded file.

The built-in render styles draw through an SVG filter. Reduced-motion preferences, pause state, offscreen status, and page visibility suspend styles that animate. A still frame remains. poster supplies a loading image, not a replacement for host asset limits.

Contribute and license

Sprite is MIT licensed. See LICENSE and THIRD_PARTY_NOTICES.

版本与发布记录

@fungi.computer/sprite 共发布 6 个版本,首次发布于 2026/10/15;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.2.2latest2026/11/097 天前
0.2.12026/11/087 天前
0.2.02026/10/2323 天前
0.1.02026/10/1729 天前
0.0.12026/10/151 个月前
0.0.0-stage2026/10/151 个月前

关于 @fungi.computer/sprite 的常见问题

@fungi.computer/sprite 是什么?

Typed renderer-bounded presentation for Fungi agents

如何安装 @fungi.computer/sprite?

在项目目录运行 npm install @fungi.computer/sprite,或使用 pnpm add / yarn add / bun add。

@fungi.computer/sprite 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fungi.computer/sprite 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fungi.computer/sprite 可以商用吗?

@fungi.computer/sprite 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fungi.computer/sprite 的下载量有多少?

根据 npm 官方下载统计,@fungi.computer/sprite 在 2026/10/16 – 2026/11/14 这 30 天内被下载 11,900 次,日均约 397 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fungi.computer/sprite 还在维护吗?

最新版本 0.2.2 发布于 2026/11/09(7 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。