API Reference
Methods
Core Render Methods
play(onPreStep?)step(options?)draw(options?)clear()clearHistory()pause()rewind()reset()destroy()
Uniform Methods
initializeUniform(name, type, value, options?)updateUniforms(updates, options?)
Texture Methods
initializeTexture(name, source, options?)updateTextures(updates)
Event Methods
on(name, callback)off(name, callback)
Method Details
play(onPreStep?)
play(onPreStep?: (time: number, frame: number) => StepOptions | void): void
play() starts or resumes the animation loop and updates u_time, u_frame, and output history on each frame.
play() Parameters
| Parameter | Type | Default | Notes |
|---|---|---|---|
onPreStep | (time: number, frame: number) => StepOptions | void | undefined | Called on every frame before the animation step runs. time is measured in seconds of ShaderPad playback time, and frame is the current frame index before it is incremented. Returning a StepOptions object affects that frame only. |
shader.play((time, frame) => {
shader.updateUniforms({ u_speed: Math.sin(time) });
if (frame < 5) {
return { skipHistory: true };
}
});
This overlaps with the preStep event, but they are not identical:
- Use the
play()callback for simplicity if your app only callsplay()once. - Use the
play()callback if the logic only belongs to this specificplay()call. - Use
preStepwhen you want a reusable listener that fires for anyplay()orstep()calls. - Only the
play()callback can returnStepOptionsto affect the current frame.
step(options?)
step(options?: StepOptions): void
step() advances exactly one frame, updates u_time and u_frame, renders, and writes to history unless disabled.
draw(options?)
draw(options?: StepOptions): void
draw() renders immediately without advancing time, frame, or history.
clear()
clear(): void
clear() clears ShaderPad’s current render target immediately. Use it for advanced manual render flows; it does not advance time, frame, or history.
clearHistory()
clearHistory(): void
clearHistory() clears all output and texture history buffers without clearing the current output or changing ShaderPad time/frame state. Use it when stale feedback, trails, or delayed texture frames should be discarded while playback continues from the same clock and frame.
pause()
pause(): void
pause() stops the animation loop started by play(). Calling play() again resumes from the same ShaderPad time and frame.
rewind()
rewind(): void
rewind() resets ShaderPad time and frame state to 0 without clearing the current output or history buffers.
reset()
reset(): void
reset() rewinds ShaderPad time and frame state, clears the current output, and resets history buffers. Use clearHistory() instead when you only need to discard history.
destroy()
destroy(): void
destroy() stops playback and releases WebGL resources and event listeners.
Render Step Options Reference
step(options?), draw(options?), and the return value from play(onPreStep) all accept the same object shape:
| Option | Type | Default | Applies to | Notes |
|---|---|---|---|---|
skipClear | boolean | false | step(), draw(), play() callback return | Draws to the render target without clearing it first. Useful for accumulation, trails, and some multi-pass patterns. |
skipHistory | boolean | false | step(), draw(), play() callback return | Prevents output-history writes for that frame. draw() accepts the field for API consistency, but it has no effect there because draw() never advances history. |
initializeUniform(name, type, value, options?)
initializeUniform(
name: string,
type: 'float' | 'int' | 'uint',
value: number | number[] | (number | number[])[],
options?: { arrayLength?: number, allowMissing?: boolean },
): void
Registers a shader uniform and seeds it with an initial value.
initializeUniform() Options
| Option | Type | Default | Notes |
|---|---|---|---|
arrayLength | number | undefined | Declares that the uniform is a fixed-length array. When set, initialization must include exactly that many elements. |
allowMissing | boolean | false | When true, ShaderPad silently skips initialization if the uniform is not present in the compiled shader. Useful for optional internals and plugins. |
updateUniforms(updates, options?)
updateUniforms(
updates: Record<string, number | number[] | (number | number[])[]>,
options?: { startIndex?: number, allowMissing?: boolean },
): void
Updates one or more initialized uniforms.
When updating built-in u_time or u_frame, ShaderPad also updates its internal clock or frame counter so the value persists as the baseline for the next step() or play() frame.
updateUniforms() Options
| Option | Type | Default | Notes |
|---|---|---|---|
startIndex | number | undefined | Only relevant for uniform arrays. Starts the write at startIndex instead of 0. |
allowMissing | boolean | false | When true, ShaderPad silently skips updates for uniforms that are intentionally optional and not present in GLSL. |
initializeTexture(name, source, options?)
initializeTexture(
name: string,
source: TextureSource,
options?: TextureOptions & { history?: number },
): void
initializeTexture() registers a named texture input. If source is another ShaderPad instance and you omit texture options, the destination texture inherits the source instance’s internal render-texture settings.
Texture Options Reference
| Option | Type | Default | Notes |
|---|---|---|---|
history | number | 0 | Number of previous frames to store for this texture. Publicly, history: N gives you access to the current frame plus N previous frames. |
preserveY | boolean | Omitted, which behaves like false for DOM-backed sources | DOM-backed sources are vertically flipped by default to match WebGL coordinates. Set preserveY: true to keep their original orientation. Typed-array sources are never flipped. |
internalFormat | GLInternalFormatString | Derived from type, otherwise 'RGBA8' | GPU storage format for this texture. |
format | GLFormatString | Derived from internalFormat | Defaults to 'RGBA' for normalized and float color formats, and 'RGBA_INTEGER' for integer color formats. |
type | GLTypeString | Derived from internalFormat, otherwise 'UNSIGNED_BYTE' | Source texel data type. |
minFilter | GLFilterString | 'LINEAR' | Minification filter. |
magFilter | GLFilterString | 'LINEAR' | Magnification filter. |
wrapS | GLWrapString | 'CLAMP_TO_EDGE' | Horizontal wrap mode. |
wrapT | GLWrapString | 'CLAMP_TO_EDGE' | Vertical wrap mode. |
updateTextures(updates)
updateTextures(updates: Record<string, UpdateTextureSource>): void
Updates one or more previously initialized textures.