API REFERENCE · 2.2.0

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.

Source reference: 1 ↗ 2 ↗

ParticleCanvascomponent

Render a React canvas and own a particles simulation plus requestAnimationFrame loop.

ParticleCanvas(props: ParticleCanvasProps): JSX.Element;

Parameters

NameTypeRequiredDefaultMeaning
propsParticleCanvasPropsYes—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

Use key to rebuild when configuration identity changes.
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

NameTypeRequiredDefaultMeaning
countnumberNo8192Integer 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.
forcesForcePresetName | 'random' | ForceMatrixNo'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.
seedstring | numberNo'wgpu-kit'Converted to String(seed), then hashed; initializes positions/species and the random force matrix deterministically.
rMaxnumberNo0.12Maximum interaction radius in world units. Use a finite positive number. Also affects grid resolution.
betanumberNo0.3Piecewise force-curve breakpoint at normalized distance d/rMax. Meaningful values are between 0 and 1; not validated by this API.
forceFactornumberNo10Acceleration scale: summed interactions are multiplied by forceFactor*rMax before integration.
frictionHalfLifenumberNo0.04Damping 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.
dtnumberNo0.02Base simulation time step per tick, multiplied by the tick argument. The animation callback elapsed time is not measured automatically.
pointSizenumberNo0.004Normalized 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.
maxNeighborsnumberNo8100Grid-only candidate budget: ceil(maxNeighbors/9) candidates per each of up to 9 neighboring cells (self may count). Use a finite positive number.
classNamestringNo—CSS class forwarded to the canvas.
styleCSSPropertiesNo—React inline style merged after built-in defaults. This is the React CSSProperties type, not an arbitrary GPU parameter map.
onReady(sim: ParticlesSim) => voidNo—Initial callback after simulation creation and canvas attachment, before the RAF loop starts. Use setForces/setParams for imperative changes; the component owns destruction.
onReady.simParticlesSimYes—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

NameTypeRequiredDefaultMeaning
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.

Search concepts, APIs, and examples