API Reference
Methods
Core Render Methods
play(onBeforeStep?)step(options?)draw(options?)pause()resetFrame()reset()destroy()
Uniform Methods
initializeUniform(name, type, value, options?)updateUniforms(updates, options?)
Texture Methods
initializeTexture(name, source, options?)updateTextures(updates, options?)
Event Methods
on(name, callback)off(name, callback)
Method Details
play(onBeforeStep?)
play(onBeforeStep?: (time: number, frame: number) => StepOptions | void): void
play() starts the animation loop and updates u_time, u_frame, and output history on each frame.
play() Parameters
| Parameter | Type | Default | Notes |
|---|---|---|---|
onBeforeStep | (time: number, frame: number) => StepOptions | void | undefined | Called on every animation frame before _step() runs. time is measured in seconds from the current start 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 { skipHistoryWrite: true }
}
})
This overlaps with the beforeStep event, but they are not identical:
- Use
onBeforeStepwhen the logic belongs to this specificplay()call. - Use
beforeStepwhen you want a reusable listener that also fires for manualstep()calls. - Only
onBeforeStepcan 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. It accepts the same options shape, but only skipClear affects draw() directly.
Render Step Options Reference
step(options?), draw(options?), and the return value from play(onBeforeStep) all accept the same object shape:
| Option | Type | Default | Applies to | Notes |
|---|---|---|---|---|
skipClear | boolean | false | step(), draw(), play() callback return | Rebinds the intermediate render target without clearing it first. Useful for accumulation, trails, and some multi-pass patterns. |
skipHistoryWrite | 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 },
): void
Registers a shader uniform once, then 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, and ShaderPad will look up name[0] if needed. |
updateUniforms(updates, options?)
updateUniforms(
updates: Record<string, number | number[] | (number | number[])[]>,
options?: { startIndex?: number },
): void
Updates one or more initialized uniforms.
updateUniforms() Options
| Option | Type | Default | Notes |
|---|---|---|---|
startIndex | number | undefined | Only relevant for uniform arrays. Starts the write at name[startIndex] instead of name[0]. Passing an invalid index throws. |
initializeTexture(name, source, options?)
initializeTexture(
name: string,
source: TextureSource,
options?: TextureOptions & { history?: number },
): void
initializeTexture() registers a named texture input. The same options also control per-texture history when history is set on that texture.
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 retain for this texture. Publicly, history: N means current frame plus N previous frames; ShaderPad allocates N + 1 internal layers to make framesAgo access work naturally. |
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. Float color formats require EXT_color_buffer_float when used as renderable textures. |
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 for the texture or texture history. |
magFilter | GLFilterString | 'LINEAR' | Magnification filter for the texture or texture history. |
wrapS | GLWrapString | 'CLAMP_TO_EDGE' | Horizontal wrap mode. |
wrapT | GLWrapString | 'CLAMP_TO_EDGE' | Vertical wrap mode. |
updateTextures(updates, options?)
updateTextures(
updates: Record<string, UpdateTextureSource>,
options?: { skipHistoryWrite?: boolean },
): void
Updates one or more previously initialized textures.
updateTextures() Options
| Option | Type | Default | Notes |
|---|---|---|---|
skipHistoryWrite | boolean | false | Only relevant for textures that were initialized with per-texture history. When true, the texture data is updated without advancing that texture's history layers. |