@clayzo/webgl-player

中文文档
0.3.21发布于 2026/11/05SEE LICENSE IN LICENSE.mdTypeScriptES Module近 30 天下载 1.7万

The lightweight Clayzo animation player. WebGL2, no Skia, no setup.

原始使用文档

@clayzo/webgl-player

The lightweight Clayzo WebGL2 player. It embeds the engine core and built-in effects, so playback does not need an authentication request, a staged WASM file, or a Clayzo account.

For iOS and macOS apps, the same documents play natively through the Swift package at Clayzo/clayzo-swift.

npm install @clayzo/webgl-player
import { createPlayer, checkCoverage } from "@clayzo/webgl-player";

// `loaders` is optional: a bundle's, or your own font and image loaders.
const coverage = await checkCoverage(document, loaders);
if (!coverage.supported) throw new Error(coverage.reasons.join("; "));

const player = await createPlayer({ canvas, document, loaders, interactive: true });
player.play();

The stable public surface is createPlayer(), checkCoverage(), registerFont(), CreatePlayerOptions, and PlayerHandle. Engine handles, shader sources, renderer classes, the WASM ABI, and context-pool internals are intentionally private.

React and Next clients can use the optional client entrypoint:

import { ClayzoPlayer } from "@clayzo/webgl-player/react";

export function Hero() {
  return (
    <ClayzoPlayer
      document={document}
      poster="/hero.poster.webp"
      posterTick={240}
      label="A cube folding into the logo"
    />
  );
}

The component shows poster at once and loads the player only as it comes near the viewport, then fades it in on posterTick and plays. clayzo package hero.json --poster 240 writes a matching hero.poster.webp. When the reader asks for reduced motion it holds posterTick; reducedMotion="pause" stops where it is instead, and "ignore" plays anyway. autoplay={false} keeps it still, decorative hides it from screen readers instead of label, and fallback is shown if the player cannot be created. document can be null while it loads.

Pages without React can use the <clayzo-player> element, with the same behaviour:

<script type="module">import "@clayzo/webgl-player/element";</script>
<clayzo-player src="/hero.clayzo" poster="/hero.poster.webp" label="A cube folding into the logo"></clayzo-player>

src is an engine JSON document or a .clayzo bundle. The attributes are poster, poster-tick (defaulting to the tick the bundle's poster was rendered at), autoplay, loop, interactive, reduced-motion, label and decorative; set autoplay="false" to turn one off. The element has play(), pause(), seek(), setInput() and fire(), and fires ready and error. Content inside it is shown if the player cannot be created.

States

A document with a state machine plays one state at a time, such as idle, hover, pressed, loading and done. Pointer events, inputs and setState move it between states, and the player calls back after the frame that shows each change.

const player = await createPlayer({
  canvas,
  document,
  onStateChange: ({ from, to }) => console.log(from.name, "→", to.name),
});
player.play();
player.setInput("loading", true);
player.setState("done", { instant: true });
player.currentState; // { id, name }

initialState starts in another state, onComplete reports a state that plays once reaching its end, and onPointer reports pointer events on the canvas and on hit areas. setReducedMotion(true) shows each state at its last frame instead of animating it, which the components do when the reader asks for reduced motion. stateMachine: false plays the plain timeline.

Many players on one page

Players share GPU contexts, but each still needs memory for its pixels, and a phone closes a page that uses too much. The player keeps memory bounded by the players near the screen, not by how many are on the page:

  • Off-screen players stop drawing (pauseWhenOffscreen, default on).
  • Players more than a viewport away free their GPU memory and their canvas's pixels (releaseWhenOffscreen, default on). The canvas keeps its layout box. Within half a viewport of the screen the player draws the same frame again before it can be seen. The clock doesn't run while it's away, so tick and playing state are exactly as it left them.
  • resolution: "display" caps the device pixel ratio at maxPixelRatio (default 2), which on a 3x phone holds less than half the pixels.
  • A lost GPU context keeps the last frame on the canvas while the player moves to a new context. Failures go to onError (or onPlayerError in React); nothing is thrown.
// A player that must reappear instantly, and a crisper one on 3x screens.
await createPlayer({ canvas, document, releaseWhenOffscreen: false, maxPixelRatio: 3 });

Provide font or image loaders when document asset URLs are not directly fetchable. Use a .clayzo bundle when you want assets packaged with the document. Choose @clayzo/canvaskit-player when checkCoverage() reports an unsupported construct or full reference-renderer fidelity matters more than transfer size.

版本与发布记录

@clayzo/webgl-player 共发布 21 个版本,首次发布于 2026/08/25;过去 12 个月发布了 21 个版本。

版本发布日期距今
0.3.21latest2026/11/055 天前
0.3.202026/10/2812 天前
0.3.192026/10/2416 天前
0.3.182026/10/2219 天前
0.3.172026/10/1724 天前
0.3.162026/10/081 个月前

关于 @clayzo/webgl-player 的常见问题

@clayzo/webgl-player 是什么?

The lightweight Clayzo animation player. WebGL2, no Skia, no setup.

如何安装 @clayzo/webgl-player?

在项目目录运行 npm install @clayzo/webgl-player,或使用 pnpm add / yarn add / bun add。

@clayzo/webgl-player 支持 TypeScript 吗?

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

@clayzo/webgl-player 支持 ESM 还是 CommonJS?

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

@clayzo/webgl-player 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.13。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@clayzo/webgl-player 可以商用吗?

@clayzo/webgl-player 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@clayzo/webgl-player 的下载量有多少?

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

@clayzo/webgl-player 还在维护吗?

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