@alpha2-sh/react

中文文档
0.5.3发布于 2026/11/05MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2729

A React canvas for the alpha2 browser player.

原始使用文档
文档目录4 节
  1. Install
  2. Alpha2Canvas
  3. bindCanvas
  4. License

@alpha2-sh/react

A React canvas for the alpha2 browser player.

Install

npm install @alpha2-sh/react alpha2

alpha2 (at the same version as this package) and React 18 or later are peer dependencies.

Alpha2Canvas

import { useCallback } from "react";
import { Alpha2Canvas } from "@alpha2-sh/react";
import { Alpha2Player } from "alpha2/player";
import { WorkerRenderBackend } from "alpha2/worker";

function Preview({ workerUrl, refill, time }) {
  // A new function makes a new player: keep it stable.
  const createPlayer = useCallback(
    (replaceSurface) =>
      new Alpha2Player(new WorkerRenderBackend(new Worker(workerUrl, { type: "module" })), {
        onRefill: refill,
        replaceSurface,
      }),
    [workerUrl, refill],
  );
  return <Alpha2Canvas createPlayer={createPlayer} timeTicks={time} width={1280} height={720} />;
}

createPlayer makes an Alpha2Player from alpha2/player, here over a render worker (alpha2/worker), and refill decodes and uploads the frames a presentation lacks. replaceSurface lets the player recover from a lost GPU onto a new canvas (a lost WebGL2 context, or the CPU renderer after the browser withdrew GPU access): the component swaps a new canvas element in for the old one, and the same player draws on it. The component makes a canvas of width x height, the project's size, and hands it to a new player; it presents timeTicks whenever it changes, and disposes the player when the canvas goes. A player belongs to one canvas: React may set the component up more than once (<StrictMode>, <Activity>), and each time gets a new canvas and a new player.

The element keeps the project's aspect ratio and fills its container's width. While the page is hidden it presents nothing, and it shows the latest time when the page is visible again. Failures go to onError, or to the page's reportError without it; onReady and onPresented report the player's start and each presentation.

bindCanvas

bindCanvas(player, canvas, options) is the same wiring without React: it transfers the canvas to the player and presents the times it is shown (binding.show(ticks)). It neither makes nor disposes the player. Set the canvas's width and height to the project's size before binding it. With options.replaceCanvas (put a new canvas of the same size where the old one is, and resolve with it), binding.replaceSurface() is what the player's replaceSurface option needs.

License

MIT, copyright Alpha2 contributors.

版本与发布记录

@alpha2-sh/react 共发布 19 个版本,首次发布于 2026/10/20;过去 12 个月发布了 19 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.3latest2026/11/052 天前
0.5.22026/11/043 天前
0.5.12026/11/034 天前
0.5.02026/11/016 天前
0.4.62026/10/309 天前
0.4.52026/10/2810 天前

关于 @alpha2-sh/react 的常见问题

@alpha2-sh/react 是什么?

A React canvas for the alpha2 browser player.

如何安装 @alpha2-sh/react?

在项目目录运行 npm install @alpha2-sh/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、alpha2(0.5.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alpha2-sh/react 支持 TypeScript 吗?

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

@alpha2-sh/react 支持 ESM 还是 CommonJS?

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

@alpha2-sh/react 可以商用吗?

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

@alpha2-sh/react 的下载量有多少?

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

@alpha2-sh/react 还在维护吗?

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