The filter() function is used to apply filters to the canvas. p5.js has several presets that can be used with different levels of intensity to get the desired effect.
filter( filterType, filterParam)
Parameters: This function accept two parameters as mentioned above and described below:
- filterType: It is constant which defines the preset to be used as the filter. It can have the following values:
- THRESHOLD: This mode is used to convert the image to black and white depending on the threshold defined in the optional level parameter. A level value of 0.0 means that the image would be completely black and a value of 1.0 means that the image would be white. In case no value is specified, 0.5 is used.
- GRAY: This mode converts all colors in the image to grayscale equivalents. The optional parameter is not used for this preset.
- OPAQUE: This mode sets the alpha channel of the image to be entirely opaque. The optional parameter is not used for this preset.
- INVERT: This mode inverts each of the pixels in the image. The optional parameter is not used for this preset.
- POSTERIZE: This mode limits each channel of the image to the number of colors as specified in the value. The optional parameter can be set in the range of 2 to 255, with the most noticeable effects at the lower color ranges.
- BLUR: This mode applies a gaussian blur to the image. The optional parameter is used to specify the strength of the blur. If no parameter is specified, then a gaussian blur with a radius of 1 metre is applied.
- ERODE: This mode reduces the light areas. The optional parameter is not used for this preset.
- DILATE: This mode increases the light areas. The optional parameter is not used for this preset.
- filterParam: It is a number which is unique to each filter and affects the functionality of the filter. It is an optional parameter.
Below examples illustrate the filter() function in p5.js:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/