Back

Nov 29, 2022

Nov 29, 2022

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.

decorative cover image with title text that reads 'We love Fabric.js'
decorative cover image with title text that reads 'We love Fabric.js'
decorative cover image with title text that reads 'We love Fabric.js'

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:

screenshot of the official fabric.js website

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:

<html><head><script src="fabric.min.js"></script></head></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:

$ npm install fabric

After this, you can import Fabric.js with this command:

import { fabric } from 'fabric';

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:

<!DOCTYPE html><html><head><!-- Adding the fabric.js library --><script src="https://unpkg.com/fabric@5.2.1/dist/fabric.min.js"></script><style>body{display:flex;flex-direction:column;align-items:center;}</styles></head><body><h2>Learning Fabric</h2><canvas id="canvas"></Canvas></body></html>

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:

const canvas = new fabric.Canvas("canvas", {})

Now, we’ll go ahead and give our canvas different properties, such as width, height, and background color:

const canvas = new fabric.Canvas("canvas", {width:500,height:500,backgroundColor:green})

Canvas API Output

canvas api output showing a green square in the center of the window with a title above it that reads 'learning fabric'

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

new fabric.Rect({object})

Here the object houses the rectangle properties:

const canvas = new fabric.Canvas("canvas", {width:500,height:500,backgroundColor:green});const rectangle = new fabric.Rect({width:200,height:200,/* fill is used to define the background color*/fill:'red'})/*the add method is used to add objects to the canvas element */canvas.add(rectangle)/*the renderAll method used to render the canvas*/canvas.renderAll();

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

gif showing a green square being scaled and rotated in a gray square with a title above it that reads 'learning fabric'

Creating a Circle

In the same way, we’ll create a circle. This time, we’ll use fabric.Circle:

const canvas = new fabric.Canvas("canvas", {width:500,height:500,backgroundColor:"lightgray"});const circle = new fabric.Circle({radius:60,fill:"blue"})canvas.add(circle);canvas.renderALl();

Circle Output

gif showing a blue circle being moved and rotated in a gray square with a title above it that reads 'learning fabric'

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

<canvas id='canvas'></canvas><img src='my_image.png' id='image' style='display:none'/><

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:

const canvas = new fabric.Canvas("canvas", {width:500,height:500,backgroundColor:"White"});fabric.Image.fromURL('my_image.png',(img) => {// the scaleToHeight property is use to set the image heightimg.scaleToHeight(250)// scaleToWidth is use to set the image widthimg.scaleToWidth(250)canvas.add(img) })canvas.renderAll()

Fabric.Image Output

gif showing a blue circle being moved and rotated in a gray square with a title above it that reads 'learning fabric'

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

new fabric.Image.filters// after that you can add any filter of your choice// Example new fabric.Image.filters.Grayscale()fabric.Image.fromURL('my_image.png', (image) => {image.scaleToHeight(300)image.ScaleToWidth(300)// Creating filterconst filter = new fabric.Image.filters.Grayscale()//push the filter instance to filtersimage.filter.push(filter)// apply filterimage.applyFilters()//add image into canvas (it also re-render the canvas)canvas.add(image)},{crossOrigin: 'anonymous'});canvas.renderAll();

Grayscale Filter Output

browser display showing a grayscale filter applied to an image of a woman smiling with a title above that reads 'learning fabric'

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:

fabric.Image.fromURL('my_image.png', (image) => {image.scaleToHeight(300)image.ScaleToWidth(300)// creating filterconst filter = new fabric.Image.filters.Sepia()//add filterimage.filter.push(filter)// apply filterimage.applyFilters()//add image into canvas (it also re-render the canvas)canvas.add(image)},{crossOrigin: 'anonymous'});canvas.renderAll();

Changed Filter Output

browser display showing a sepia filter applied to an image of a woman smiling with a title above that reads 'learning fabric'

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

new fabric.Image.filters.BlendImage({// the image that we want to blendimage:imageA,// blend mode (in this case we are using the 'add' mode)mode:'add',alpha:0.5});fabric.Image.fromURL('my_image.png', (imageA) => {imageA.scaleToWidth(350)imageA.scaleToHeight(350)canvas.add(imageA)// Blending filterfilter = new fabric.Image.filters.BlendImage({image:imageA,mode:'add',alpha:0.5});//Add the second image fabric.Image.fromURL('my_image2.png',(imageB) => {imageB.scaleToHeight(100),imageB.scaleToWidth(100),canvas.add(imageB)}, {crossOrigin:''});imageA.filters.push(filter);imageA.applyFilters(canvas.renderAll.bind(canvas));},{crossOrigin:''})

Image Blending Output

gif of a browser window showing off a yellow hoodie and a sunglasses smiley face. There is a title above the gif that reads 'learning fabric'

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:

const clipPath = new fabric.Circle({radius:150,top:200,left:250,// The originY and originX helps to center the image and the clipPathoriginY:"center",originX:"center"})

After that, we add it to the image that we want to clip using the filter method:

new fabric.Image.fromURL('my_image.png',(img) => {img.scaleToHeight(300)img.scaleToWidth(300)// positioning the imageimg.scale(o.5).set({top:50,left:100})// Clip PathclipPath = new fabric.Circle({radius:150,top:200,left:250,originY:"center",originX:"center"})canvas.add(img)canvas.clipPath = clipPath;});canvas.renderAll();

Image Clipping Output

gif of a browser window that shows a circle cropped version of the image of the woman smiling

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

new fabric.Image.filters.Blur(object)fabric.Image.fromURL('my_image.png', (image) => {image.scaleToHeight(300)image.ScaleToWidth(300)// Creating the blur filterconst filter = new fabric.Image.filters.Blur({blur:0.6})//push the filter instance to filtersimage.filter.push(filter)image.applyFilters()canvas.add(image)},{crossOrigin: 'anonymous'});canvas.renderAll();

Image Blurring Output

browser display showing a blurred filter applied to an image of a woman smiling with a title above that reads 'learning fabric'

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.

Written by

Written by

Goodness Woke

Goodness Woke

SHARE

SHARE

Title

Title

our newsletter

Sign up for The Pieces Post

Check out our monthly newsletter for curated tips & tricks, product updates, industry insights and more.

our newsletter

Sign up for The Pieces Post

Check out our monthly newsletter for curated tips & tricks, product updates, industry insights and more.

our newsletter

Sign up for The Pieces Post

Check out our monthly newsletter for curated tips & tricks, product updates, industry insights and more.