wgpu-kit/react
React canvas component with mount-scoped simulation ownership.
Auxiliary inputs, dispatch counts, managed batches and typed kernels are released in 2.1.0. The original API retains the published npm 2.0.0 declarations and implementation as its historical audit baseline; Additions in 2.1.0 and 2.2.0 are identified on affected APIs and cite src files, without claiming a fresh audit of the entire API. Only actual public exports of this entry point are listed.
ParticleCanvascomponent
Render a React canvas and own a particles simulation plus requestAnimationFrame loop.
ParticleCanvas(props: ParticleCanvasProps): JSX.Element;Parameters
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
props | ParticleCanvasProps | Yes | — | All ParticlesConfig fields plus className, style, and onReady; see the complete props entry. |
Returns
JSX.Element A canvas element whose initialization runs after mounting.
Behavior & constraints
- Requires React >=18 (optional peer dependency) and browser WebGPU. Component creates its simulation once per mount. Config and onReady changes are ignored by its empty-dependency effect; use a new React key to rebuild.
- The component calls attach(), then onReady(), then ticks on every animation frame. Unmount cancels the scheduled frame and destroys its simulation. Do not run a second tick loop or retain the handle after unmount.
- No onError prop, error UI, pause prop, resize observer, or visibility throttling is provided. Async initialization failures are not caught internally; use the lower-level API when explicit failure control is required.
- Canvas style defaults to width:100%, height:100%, display:block, background:#05070c; supplied style overrides these. Give its container a usable height.
Examples
import { ParticleCanvas } from 'wgpu-kit/react';
export function Simulation({ seed }: { seed: string }) {
return (
<div style={{ height: 400 }}>
<ParticleCanvas
key={seed}
seed={seed}
count={4096}
mode="grid"
style={{ background: '#000' }}
onReady={(sim) => sim.setParams({ forceFactor: 8 })}
/>
</div>
);
}ParticleCanvasPropsinterface
Complete component props, inheriting the same implementation defaults and constraints as particles().
interface ParticleCanvasProps extends ParticlesConfig {
className?: string;
style?: CSSProperties;
onReady?: (sim: ParticlesSim) => void;
}Parameters
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
count | number | No | 8192 | Integer particle count in 1..1_000_000. n2 mode rejects counts above 32_000 (its error text recommends at most 20_000). Device memory limits may be lower. |
forces | ForcePresetName | 'random' | ForceMatrix | No | 'cells' | One of cells/snakes/orbitals/viruses, seeded random, or a readonly 16-number tuple. Four species use matrix[mySpecies*4+otherSpecies]; custom arrays are not length/range-validated here. |
mode | 'n2' | 'tiled' | 'grid' | No | 'grid' | Neighbor algorithm: all-pairs, workgroup-tiled all-pairs, or spatial grid. The declaration comment saying tiled is the default is stale; implementation defaults to grid. |
color | 'species' | 'velocity' | No | 'species' | Canvas coloring by four species or speed magnitude. Used when the renderer is attached. |
bounds | 'wrap' | 'clamp' | No | 'wrap' | How updated positions remain in the square world: wrap or clamp. Interaction distances/neighborhood searches do not wrap across opposite edges. |
seed | string | number | No | 'wgpu-kit' | Converted to String(seed), then hashed; initializes positions/species and the random force matrix deterministically. |
rMax | number | No | 0.12 | Maximum interaction radius in world units. Use a finite positive number. Also affects grid resolution. |
beta | number | No | 0.3 | Piecewise force-curve breakpoint at normalized distance d/rMax. Meaningful values are between 0 and 1; not validated by this API. |
forceFactor | number | No | 10 | Acceleration scale: summed interactions are multiplied by forceFactor*rMax before integration. |
frictionHalfLife | number | No | 0.04 | Damping time parameter used as exp(-effectiveDt/frictionHalfLife). Despite the name, implementation does not include ln(2), so this is an exponential decay time constant rather than a mathematical half-life. Use a positive finite value. |
dt | number | No | 0.02 | Base simulation time step per tick, multiplied by the tick argument. The animation callback elapsed time is not measured automatically. |
pointSize | number | No | 0.004 | Normalized point-quad half-size used by the attached renderer, approximately in clip coordinates after world scaling. It is not a literal pixel size; shader generation rounds the scaled value to four decimals. |
maxNeighbors | number | No | 8100 | Grid-only candidate budget: ceil(maxNeighbors/9) candidates per each of up to 9 neighboring cells (self may count). Use a finite positive number. |
className | string | No | — | CSS class forwarded to the canvas. |
style | CSSProperties | No | — | React inline style merged after built-in defaults. This is the React CSSProperties type, not an arbitrary GPU parameter map. |
onReady | (sim: ParticlesSim) => void | No | — | Initial callback after simulation creation and canvas attachment, before the RAF loop starts. Use setForces/setParams for imperative changes; the component owns destruction. |
onReady.sim | ParticlesSim | Yes | — | Borrowed simulation handle. Valid only while the component remains mounted. |
Behavior & constraints
- All particle fields retain their documented semantics, including actual mode default grid and the maxNeighbors=Infinity limitation. React adds no extra validation.
Fields & related types
| Name | Type | Required | Default | Meaning |
|---|---|---|---|---|
CSSProperties (React supporting type) | import('react').CSSProperties | — | — | Standard React CSS style property type; its full browser/React property catalog is outside wgpu-kit. |
React key (framework attribute, not ParticleCanvasProps) | React.Key | — | — | Use a changed key to remount/recreate the simulation. key is handled by React and is not passed as a normal config field. |
按挂载生命周期拥有模拟的 React 画布组件。
辅助输入、dispatch count、托管批次与类型化内核已随 2.1.0 发布。原 API 仍以 npm 2.0.0 声明与实现为历史核对基线;2.1.0 和 2.2.0 新增能力在对应 API 中注明并引用 src 文件,不代表整份 API 重新审计。只列出此入口实际导出的公开 API。
ParticleCanvascomponent
渲染 React canvas,管理 particles 模拟与 requestAnimationFrame 循环。
ParticleCanvas(props: ParticleCanvasProps): JSX.Element;参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
props | ParticleCanvasProps | 是 | — | 所有 ParticlesConfig 字段,加上 className、style 和 onReady;详见完整 props 条目。 |
返回值
JSX.Element 一个 canvas 元素,挂载后异步初始化模拟。
行为与约束
- 需要 React >=18(可选 peer dependency)和浏览器 WebGPU。每次挂载只创建一次模拟。空依赖 effect 忽略后续 config 和 onReady 变化;使用新的 React key 重建。
- 组件调用 attach(),随后 onReady(),然后在每个动画帧 tick。卸载时取消已调度帧并销毁模拟。不要另开 tick 循环,也不要在卸载后继续使用句柄。
- 不提供 onError、错误界面、pause、resize observer 或可见性节流。内部不捕获异步初始化错误;需要精确失败控制时使用底层 API。
- canvas 样式默认为 width:100%、height:100%、display:block、background:#05070c;传入 style 可覆盖。容器应有可用高度。
示例
import { ParticleCanvas } from 'wgpu-kit/react';
export function Simulation({ seed }: { seed: string }) {
return (
<div style={{ height: 400 }}>
<ParticleCanvas
key={seed}
seed={seed}
count={4096}
mode="grid"
style={{ background: '#000' }}
onReady={(sim) => sim.setParams({ forceFactor: 8 })}
/>
</div>
);
}ParticleCanvasPropsinterface
完整组件 props,继承与 particles() 相同的实现默认值和限制。
interface ParticleCanvasProps extends ParticlesConfig {
className?: string;
style?: CSSProperties;
onReady?: (sim: ParticlesSim) => void;
}参数
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
count | number | 否 | 8192 | 粒子数量,整数范围 1..1_000_000。n2 模式拒绝超过 32_000 的数量(其错误文案建议不超过 20_000)。设备内存限制可能更低。 |
forces | ForcePresetName | 'random' | ForceMatrix | 否 | 'cells' | 可选 cells/snakes/orbitals/viruses、带种子的 random 或只读 16 数值元组。四个物种按 matrix[mySpecies*4+otherSpecies] 取值;此处不校验自定义数组长度/范围。 |
mode | 'n2' | 'tiled' | 'grid' | 否 | 'grid' | 邻域算法:全量两两计算、workgroup 分块两两计算或空间网格。声明中默认 tiled 的注释已过时;实现默认 grid。 |
color | 'species' | 'velocity' | 否 | 'species' | 画布按四种物种或速度大小着色,attach 渲染器时生效。 |
bounds | 'wrap' | 'clamp' | 否 | 'wrap' | 更新后位置如何保持在方形世界内:环绕或钳位。交互距离/邻域搜索不会跨相对边界环绕。 |
seed | string | number | 否 | 'wgpu-kit' | 转换为 String(seed) 后散列,确定性地初始化位置、物种和 random 力矩阵。 |
rMax | number | 否 | 0.12 | 世界单位下的最大交互半径,应使用有限正数,也影响网格分辨率。 |
beta | number | 否 | 0.3 | 归一化距离 d/rMax 下的分段力曲线分界点。合理值在 0 与 1 之间;API 不验证。 |
forceFactor | number | 否 | 10 | 加速度缩放;交互总和在积分前乘以 forceFactor*rMax。 |
frictionHalfLife | number | 否 | 0.04 | 阻尼时间参数,实际公式为 exp(-effectiveDt/frictionHalfLife)。虽然名称含 HalfLife,实现没有 ln(2),数学上是指数衰减时间常数,不是真正半衰期。应使用有限正数。 |
dt | number | 否 | 0.02 | 每次 tick 的基础模拟时间步,乘以 tick 参数;不会自动测量动画回调经过的时间。 |
pointSize | number | 否 | 0.004 | attach 渲染器使用的归一化点四边形半尺寸,经世界缩放后近似为裁剪空间坐标尺寸。它不是直接像素值;shader 生成时会把缩放后的值保留四位小数。 |
maxNeighbors | number | 否 | 8100 | 仅 grid 使用的候选预算:最多 9 个相邻格子,每格检查 ceil(maxNeighbors/9) 个候选(自身也可能计入)。应使用有限正数。 |
className | string | 否 | — | 传给 canvas 的 CSS 类名。 |
style | CSSProperties | 否 | — | 合并在默认样式之后的 React 内联样式。类型是 React CSSProperties,不是任意 GPU 参数映射。 |
onReady | (sim: ParticlesSim) => void | 否 | — | 模拟创建并绑定画布之后、RAF 循环之前调用的初始回调。可用 setForces/setParams 做命令式更新;销毁由组件负责。 |
onReady.sim | ParticlesSim | 是 | — | 借用的模拟句柄,仅在组件仍挂载期间有效。 |
行为与约束
- 所有粒子字段保留原有语义,包括 mode 实际默认 grid 和 maxNeighbors=Infinity 的限制。React 层不增加额外验证。
字段与关联类型
| 名称 | 类型 | 必填 | 默认值 | 含义 |
|---|---|---|---|---|
CSSProperties (React supporting type) | import('react').CSSProperties | — | — | 标准 React CSS 样式属性类型,完整浏览器/React 样式目录不属于 wgpu-kit。 |
React key (framework attribute, not ParticleCanvasProps) | React.Key | — | — | 改变 key 可重新挂载/重建模拟。key 由 React 处理,不作为普通配置字段传入。 |