How to create a custom filter to process your images
Fabric has a boiler plate file to start your new filter.
I decided to call this filter SwapColor, so i start from this file boilerplate duplicate it, and first thing you can do is replace all the instances of MyFilter
with SwapColor
.
Then start coding your filter code.
The idea behind this filter is that would start simple, take a color in the picture and swap with another, if the match exactly. We need to write this code both for the JS part and for the WEBGL part.
Prerequisites
The standard one parameter filter is able to save/restore itself using the mainParameter
argument, but this one needs 2 of them.
So we delete any reference to mainParameter
and we add a couple of things.
The filters needs to handle the source color and the destination color, so we add some basic information to our empty boilerplate.
On top of that the filter needs to be able to store and restore itself.
And now we should make the filter work in 2DCanvas and WebGL.
2DCanvas routine for pixel swapping
Inside the filter class, applyTo2d
is the function you have to look at for the standard non WebGL support.
This code is rarely needed if you run from browser, useful if you run also in node, but anyway fabricJs is now supporting both modes,
so if you want to write a filter, better you handle both of them.
The logic is simple, for each pixel, if the color match, swap it with the destination one.
To determine if a color match, we use fabric.Color
to parse a css color string and we compare the first 3 components.
That’s all for the 2D canvas part.
The WebGL part
If you are comfortable with WebGL this part will look super easy, if not there are some concept to grasp. Disclaimer: I’m not great at WebGL and this code reflect how fabricJS is wrapping the basic functionalities in reusable pieces.
First of all you have to setup the variables injecton in the webgl shader, this is done with the couple getUniformLocations
and sendUniformData
.
The getUniformLocations
will define how those variables are called in the shader, while sendUniformData
will be responsible to bind data to them. Writing sendUniformData
is definetely the hardest part since you have to know which type of variable you are using. In our case we are changing color strings in array of 4 number between 1 and 0, and to do so we need to use the gl.uniform4fv
function.
And then of course the webgl shader source is needed.