How to use image filters in p5.js
Image filters allow users to manipulate images. The p5.js library has a built-in function that allows such interactions to occur.
Images
To use images, we will primarily use the two functions loadImage() and image(). The loadImage() function has one nuance; it must be called in the preload() function rather than setup()loadImage() is an setup() might cause the application to start without successfully loading the image.
Image filters
p5.js has a function called filter() for applying filters to images. This function can take up to three parameters. Let’s examine the function’s signatures.
filter(type, [param], [useWebGL])
The
typeparameter represents the type of filter to apply. This can be eitherTHRESHOLD,GRAY,OPAQUE,INVERT,POSTERIZE,BLUR,ERODE, orDILATE.The
[param]is an optional parameter that represents a number. This is unique to each filter, usually indicating its strength.The
[useWebGL]is also an optional parameter that represents a boolean value. This controls whether to use fast WebGL filters (GPU) or original image filters (CPU); defaults totrue.
filter(filterType, [useWebGL])
This signature is the same as the first one, with one notable difference: the omission of [param]. This syntax is usually used when some filter cannot be controlled by a number value, e.g. INVERT.
filter(customFilter)
In this syntax customFilter is a p5.Shaderp5.Shader class, which exceeds the scope of this explanation focused on the built-in image filters provided by the filter() function.
Example: Image filter manipulation
Let's see an example use case of this function. In this example, we can see the effect of different filters. Press the keys 0 to 9 to see the results. Press any other key to go back to the original image. Feel free to play around with the code below:
Code explanation
Lines 1–11: The
imgvariable stores the image, which will be ap5.Imageobject. TheimgUrlstores the path to the image. Then, thepreload()function loads the image asynchronously before the main program starts.Lines 14–17: In the
setup()function, create a canvas 500 pixels wide and 500 pixels high.Lines 19–68: In the
draw()function, continuously redraw the background and the image at the top-left corner of the canvas with dimensionswidthbyheight. If a key is pressed, apply different image filters based on the pressed key using the p5.js library. Display labels using thelabel()function indicating the applied filter or show "ORIGINAL" if no specific key is pressed.Lines 71–93: Define a utility function
label()to display labels. Set the fill color to black, draw a centered rectangle above the bottom edge of the canvas, set text alignment to center, set the textfill()color to white, set thetextSize()to 16 pixels, and display the input stringlabat the center of the rectangle, above the bottom edge.
Conclusion
The p5.js allows the user to manipulate images in a simple way. There are also other functions like tint()p5.Shader filters.
Unlock your potential: p5.js fundamentals series, all in one place!
To deepen your understanding of p5.js, explore our series of Answers below:
What is p5.js?
p5.js is an open-source JavaScript library for creative coding, designed for visualizations, games, and interactive art, with a beginner-friendly approach.How to color shapes in p5.js
Learn how to apply colors to shapes usingfill(),stroke(), and transparency to create stunning visual effects.How to use images in p5.js
Discover how to load, display, and manipulate images in p5.js to enhance dynamic and interactive sketches.How to use image filters in p5.js
Enhance your images with built-in filters like grayscale, blur, and threshold to achieve unique visual effects.How to create advanced 2D shapes in p5.js
Master the creation of complex 2D shapes using curves, vertices, and transformations for custom designs.How to add keyboard interaction in p5.js
Learn how to detect and respond to keyboard input to add interactivity to your sketches.How to use vectors in p5.js
Explore the power of vectors for motion, physics simulations, and dynamic interactions in creative coding.How to add mouse interaction in p5.js
Implement mouse-based interactions such as clicks, drags, and movement tracking to enhance user engagement.
Free Resources