This API is experimental
Its behavior may change in a future version of p5.js.
Loads a new shader from a file that can be applied to the contents of the canvas with filter(). Pass the resulting shader into filter() to apply it.
Since this function loads data from another file, it returns a Promise. Use it in an async function setup, and await its result.
async function setup() {
createCanvas(50, 50, WEBGL);
let img = await loadImage('/assets/bricks.jpg');
let myFilter = await loadFilterShader('myFilter.js');
image(img, -50, -50);
filter(myFilter);
describe('Bricks tinted red');
}Inside your shader file, you can use p5.strands hooks to change parts of the shader. For a filter shader, use filterColor to change each pixel on the canvas.
// myFilter.js
filterColor.begin();
let result = getTexture(
filterColor.canvasContent,
filterColor.texCoord
);
// Zero out the green and blue channels, leaving red
result.g = 0;
result.b = 0;
filterColor.set(result);
filterColor.end();Read the reference for buildFilterShader, the version of loadFilterShader that takes in a function instead of a separate file, for more examples.
The second parameter, successCallback, is optional. If a function is passed, as in loadFilterShader('myShader.js', onLoaded), then the onLoaded() function will be called once the shader loads. The shader will be passed to onLoaded() as its only argument. The return value of handleData(), if present, will be used as the final return value of loadFilterShader('myShader.js', onLoaded).
Examples
Syntax
loadFilterShader(filename, [successCallback], [failureCallback])