Back
Fabric.js Image Manipulation with Canvas
Fabric.js was designed to tackle the difficulties of using the Canvas API. Learn how to integrate it into your projects with this in-depth explainer.
Using Fabric.js image manipulation with Canvas allows us to create some amazing graphics on the web these days. However, the API it provides is disappointingly low-level. It's one thing if we want to draw a few basic shapes in Canvas and forget about them; using Canvas won't feel cumbersome. But, when any form of interaction is required with Canvas, such as drawing complex graphics or changing pictures in specific situations, it becomes challenging to use the Canvas API.
What is Fabric.js?
Fabric.js is a JavaScript library that makes working with HTML Canvas easy. It provides a unique object model and interactive platform to work with Canvas. Fabric.js was designed to tackle the difficulties of using the Canvas API. With Fabric.js, you can work with both images and animations. For example, Fabric.js allows you to drag, scale, and rotate images. You can also group and manipulate shapes and objects together. Fabric.js even provides the necessary functionality to serialize a Canvas element to SVG or JSON and enables you to reuse it as needed.
Fabric.js Objects
Fabric.js provides a powerful object model that takes care of Canvas state and rendering. Fabric.js operates on objects by instantiating them, changing their property, and adding them to Canvas. These Fabric.js objects include the following:
One of the unique features of Fabric is interactivity on top of all object models. The object model gives you programmatic access and the ability to manipulate objects on a canvas. As soon as the canvas is initialized, you can select objects, drag them around, scale them, rotate them, and group them in order to manipulate them into one piece.
Setting Up Fabric.js
Just like every JavaScript library, setting up Fabric.js is quite easy and can be done in one of two ways. The first way is to download the source file from the official website, Fabricjs.com:
A second way to set up Fabric.js is to copy the CDN link HERE and include it in your project. Then, you can create an HTML file, for example, index.html:
The Fabric.js Install Process
On the other hand, if you want to integrate Fabric.js into React or a similar project, you have to install Fabric.js by using the following:
After this, you can import Fabric.js with this command:
Canvas API
The canvas element is used to draw 2-dimensional graphics on websites. It has an API called Canvas API, which is used to illustrate basic geometrical shapes like rectangles, triangles, and other objects. However, adding interactions to them or drawing complex shapes becomes very difficult. Fabric.js tends to fix this problem.
Next, we’ll be creating some Canvas objects like rectangles and circles. Because Fabric.js works with the Canvas API, we must make the <canvas>
element tag in the HTML and give it an id. Further, we must pass that id to the Fabric.js API to initialize the Fabric.js Canvas instance on the <canvas>
tag with a bit of CSS to center it:
Next, we initiate a Canvas instance in our script tag:
Canvas API Syntax
new fabric.Canvas(element: HTMLElement|String, options: Object)
This allows Fabric.js to create this class instance so that we can use the provided methods of Fabric.js:
Now, we’ll go ahead and give our canvas different properties, such as width, height, and background color:
Canvas API Output
Creating a Rectangle with Fabric.js
Now that we’ve initialized a canvas and given it various properties, we’re going to create a rectangle using the Fabric.js object fabric.Rect:
Rectangle Syntax
Here the object houses the rectangle properties:
The add()
method is used to add objects to the Canvas element. In this case, it adds the rectangle we created to the canvas:
Rectangle Output
Creating a Circle
In the same way, we’ll create a circle. This time, we’ll use fabric.Circle:
Circle Output
With the above steps, we created a rectangle and a circle using Fabric.js. Note how interactive the objects are! Next, we’ll discuss images in Fabric.js.
Images with Fabric
The Fabric.Image is a Fabric.js object used to render images on a canvas. With Fabric.js, we can interact with the image. Images can be added to our canvas in two ways:
Fabric.Image
Fabric.Image.fromURL
Using the Fabric.Image constructor requires us to create an image element in our HTML and then insert it into the constructor:
Fabric.Image Syntax
fabric.Image(image_element)
HTML
After passing the URL, the callback function after the image is loaded. The first default parameter of the callback function is the fabric.Image object. Now, we can modify the image properties and then add it to the canvas:
Fabric.Image Output
Editing Images with Fabric.js
Now that we know how to insert an image into a canvas, we’ll look at other image objects like image filters.
Fabric.js Image Filters
Fabric.js provides us with built-in filters:
BaseFilter
Brightness
convolute
GradientTransparency
Grayscale
Invert
Mask
Noise
Pixelate
RemoveWhite
Sepia
Sepia2
Tint
Every instance of fabric.Image
has the "filters" property, which is a simple array of filters. We can use the array method to perform any required operation, including removing filters (pop, slice, shift), adding filters (push, unshift, slice), and even combining multiple filters. When we call applyFilters
, any filter in the “filters” array will be applied one by one. Let’s use some of these filters:
Image Filters Syntax
Grayscale Filter Output
Above, we used the Grayscale filter; we can also apply other filters. All we have to do is simply change the filter, as shown in this example:
Changed Filter Output
Image Blending
Blending is mixing two or more images to create a new image. Fabric.js gives us this ability. Here, we’re going to use the image blending filter for our image:
Image Blending Syntax
Image Blending Output
Using Fabric.js to Clip an Existing Image to ClipPath
A very popular effect used today is image clipping, or rather image cropping, which is done to remove parts of an image that we don’t want. With Fabric.js, we can achieve this by using the clipPath
method.
First, we have to create our own clipPath
using any Fabric.js object. Then, we’ll assign it to the clipPath
property of the object we want to clip. Here, we’ll use a circle object:
After that, we add it to the image that we want to clip using the filter method:
Image Clipping Output
Image Blurring
Like other image filters, the blur filter accepts objects because they can be adjusted. A practical example is changing the brightness of an image. You can apply other image filters without any additional configurations (like grayscale, invert, or sepia). Other image filters like brightness, pixelate, tint, and noise all operate in this way, too. However, the difference is that their values are not the same. The brightness filter's actual brightness level ranges from-1 (full black) to 1 (full white). The noise filter's value is between 0 and 1000. For the blur filter, the value ranges from 0 to 10.
Image Blurring Syntax
Image Blurring Output
Conclusion
In conclusion, Fabric.js image manipulation gives us an awesome experience when working with Canvas. It also provides a lot of features, some of which we covered in this article. For full details on some of the Fabric.js objects, you can review its documentation to learn more.
Download the Pieces for Developers IntelliJ plugin or VS Code extension to easily use the code snippets above in your projects.