API Reference

React

The shaderpad/react entry exports:

import { ShaderPad, type ShaderPadProps } from 'shaderpad/react';

If you want the common usage patterns first, start with the React guide.

Exports

ExportTypeMeaning
ShaderPadcomponentReact wrapper that renders one managed <canvas>.
ShaderPadPropstypeProp type for the component.

Props

Required

PropTypeMeaning
shaderstringFragment shader source passed into the core ShaderPad constructor.

Core Configuration

PropTypeDefaultMeaning
pluginsPlugin[][]Additional plugins to install after wrapper-owned autosize.
optionsOmit<Options, 'canvas' | 'plugins' | 'cursorTarget'>{}Core constructor options other than wrapper-owned canvas, plugins, and cursor target.
autosizeboolean | AutosizeOptionstrueEnables wrapper-owned autosize() or passes custom autosize options.
cursorTargetWindow | Element | RefObject<Element | null>canvas element when applicableTarget for built-in pointer uniforms. Ref targets delay initialization until resolved.

Playback

PropTypeDefaultMeaning
autoPlaybooleantrueStarts playback automatically after initialization.
pauseWhenOffscreenbooleantruePauses when offscreen or document-hidden, then resumes when visible again.

Callbacks

PropTypeMeaning
onInit(shader, canvas) => voidRuns after the instance is created and subscriptions are installed, before auto-play starts.
onBeforeStep(shader, time, frame) => StepOptions | voidRuns before each managed play() frame and can return step options.
onError(error) => voidReceives initialization errors instead of throwing them through React.
onOnscreenChange(isOnscreen) => voidRuns when the effective onscreen state changes.
eventsRecord<string, (...args: any[]) => void>Subscribes to core ShaderPad events and namespaced plugin events.

Canvas Props

ShaderPadProps extends normal React canvas props except children and the DOM onError.

  • className
  • style
  • id
  • role
  • tabIndex
  • aria-label
  • other standard canvas DOM props

Default inline style values:

  • display: 'block'
  • width: '100%'
  • height: '100%'

Your style prop is merged on top of those defaults.

Events

The events prop subscribes to:

  • core ShaderPad events such as updateUniforms, beforeStep, and updateResolution
  • namespaced plugin events such as autosize:resize

Core event signatures are documented on the main Events page.

Ref API

The component uses forwardRef. The public way to type the ref is:

const ref = useRef<React.ElementRef<typeof ShaderPad>>(null);

The ref exposes:

MemberType / Behavior
shaderUnderlying core ShaderPad instance or null.
canvasManaged HTMLCanvasElement or null.
play()Starts playback using the latest onBeforeStep callback.
pause()Pauses playback.
step(options?)Advances one frame manually.
draw(options?)Draws without advancing time or frame.
clear()Clears the current output.
resetFrame()Resets frame counting.
reset()Resets time, frame state, and history.
destroy()Destroys the underlying instance.

Runtime Behavior

The wrapper keeps callback props and event handlers live without recreating the instance.

When pauseWhenOffscreen is enabled, onscreen state is computed from:

  • IntersectionObserver
  • document.visibilityState
  • canvas.isConnected
  • checkVisibility() when available, with a layout/style fallback

Behavior by prop combination:

autoPlaypauseWhenOffscreenBehavior
truetruePlay when visible, pause when not visible.
truefalseStart once after init and keep running.
falseeitherNever auto-start, but still report onOnscreenChange.

Rules

  • Use this component only from client components in frameworks such as Next.js.
  • Do not pass your own autosize() plugin in plugins unless you also set autosize={false}.
  • The wrapper is intentionally thin. Declarative uniform and texture props are not part of this API; use onInit, onBeforeStep, events, or the ref for that work.
Previous
Plugins