React

The ShaderCanvas component and useShader hook.

1 min read

Terminal
npm i shadermate react react-dom

<ShaderCanvas>#

JSX
import { ShaderCanvas } from 'shadermate/react';

export function Hero({ hover }) {
  return (
    <ShaderCanvas
      shader={frag}
      uniforms={{ u_hover: hover ? 1 : 0 }}
      options={{ maxFps: 60, pointer: { smooth: 0.15 } }}
      style={{ width: '100%', height: '60vh' }}
      onReady={sm => sm.tween('u_in', 1, { duration: 800 })}
    />
  );
}
Prop Meaning
shader or passes What to render. A change rebuilds the instance.
uniforms Applied live on every render, without rebuilding.
options Any other option (everything except canvas, shader, passes, uniforms).
onReady, onFrame, onError Callbacks.
deps Extra values that should trigger a rebuild.
anything else Passed to the <canvas> (className, style, aria-* …).

A forwarded ref receives the instance:

JSX
const ref = useRef(null);
<ShaderCanvas ref={ref} shader={frag} />
// ref.current.pause()

useShader#

When you need your own canvas element:

JSX
import { useShader } from 'shadermate/react';

function Panel() {
  const [canvasRef, sm] = useShader({ shader: frag, uniforms: { u_glow: 1 } }, []);
  return <canvas ref={canvasRef} onPointerEnter={() => sm?.tween('u_glow', 2, { duration: 200 })} />;
}

sm is null until the instance exists. The second argument is a dependency list that triggers a rebuild, like useEffect.

Notes#