Comment Shape Fabric.js The Ultimate Tutorial Guide

Comment Shape Fabric.js: The Ultimate Tutorial Guide

Comment shape is an important feature of Fabric.js. It allows users to interact with textboxes and create comments on canvas. This ultimate tutorial guide aims to provide a comprehensive and easy-to-follow guide on how to create comment shapes using Fabric.js. By the end of this guide, you will be able to create customized comment shapes and add them to your canvas with ease.

What is Fabric.js?

Fabric.js is a powerful Javascript library that provides an object model for HTML5 canvas, making working with it easier for developers. It is an open-source project under MIT license and offers a range of essential tools, including an SVG parser, interactivity layer, and many more.

How does Fabric.js work?

Fabric.js is based on the HTML5 canvas element, which is an essential tool for creating graphics, animations, and games on the web. With Fabric.js, developers can use a range of features like drawing objects, applying filters and transformations, manipulating images, and adding text. The library makes it easier to manage multiple elements on the canvas and move them as a group or individual objects.

What are the benefits of using Fabric.js?

Fabric.js offers a lot of advantages for developers, including:

  • Easy to use and learn
  • Provides an object model for canvas and SVG
  • Supports multiple platforms, including desktop and mobile devices
  • Offers a range of built-in shapes, filters, animations, and effects
  • Allows to create custom elements
  • Provides interactivity layer, including mouse and touch events
  • Enables integration with other libraries and frameworks, including React and AngularJS

What is a comment shape in Fabric.js?

A comment shape in Fabric.js is an element that allows developers to add comments or annotations to the canvas. It is a custom shape that can be created using the `fabric.Polygon` constructor function, which enables the creation of any polygon shape with a customizable number of points. Developers can use this shape to add notes, labels, or any other information to the canvas.

How to create a comment shape in Fabric.js?

To create a comment shape in Fabric.js, developers can use the `fabric.Polygon` constructor function and pass the polygon points array as an argument. They can then customize the properties of the shape, such as fill color, stroke color, and opacity, using the Fabric.js API. Here is an example code snippet for creating a comment shape:

var polyPoints = [
  { x: 0, y: 0 },
  { x: 100, y: 0 },
  { x: 100, y: 50 },
  { x: 50, y: 50 },
  { x: 50, y: 100 },
  { x: 0, y: 100 },

var comment = new fabric.Polygon(polyPoints, {
  fill: 'yellow',
  stroke: 'black',
  strokeWidth: 1,
  opacity: 0.5


Fabric.js is a powerful and versatile library that offers developers a range of tools for creating and manipulating graphics and animations on the web. With its easy-to-learn API, comprehensive documentation, and strong community support, Fabric.js is a popular choice for developers looking to work with HTML5 canvas. Adding comment shapes to the canvas can provide valuable feedback and notes, making it a useful feature for developers and users alike.

Canvas Initializing

Initializing a canvas using Fabric.js is simple and straightforward. First, we need to define a canvas element in our HTML file.

Next, we need to create a new instance of Fabric.js Canvas class and pass our canvas element as a parameter by using the constructor method, as shown in the code snippet below:

var canvas = new fabric.Canvas('canvas');

This will create a new instance of the canvas on which we can draw our shapes and add interactivity. We can also customize our canvas by setting its dimensions and other properties using the `setDimensions` and `setBackgroundColor` methods, respectively.

Now that we have initialized our canvas, we can move on to adding shapes and interacting with them using Fabric.js.

Drawing Shapes with Fabric.js

Drawing Rectangle and Ellipse

Fabric.js allows you to easily create rectangle and ellipse shapes on the canvas. To draw a rectangle shape, use the fabric.Rect function, specifying the left, top, width, and height parameters. To draw an ellipse shape, use the fabric.Ellipse function, specifying the left, top, rx, and ry parameters. Both shapes can be styled with various properties, such as fill color, stroke color, and stroke width.

Drawing Free-hand Shapes

Fabric.js also enables you to draw free-hand shapes directly on the canvas. You can utilize the fabric.Path function to achieve this. First, create a new path object with the fabric.Path constructor. Then utilize the path array property to store points and movements through the canvas with push and lineTo function. Finally, you can call the set() function to fix the fill color, stroke color, and stroke width.

Drawing Line and Dashed Line Shapes

If you need to draw lines or dashed lines in Fabric.js, you can use the fabric.Line function. After creating the line object with the fabric.Line constructor, you can customize the parameters of the line, including the position, stroke color, stroke width, and dash array. To create a dashed line, set the dash array with an array of numbers, which specify the length of each dash and gap pair.

Drawing Polygon Shapes

To create a polygon shape in Fabric.js, you can use the fabric.Polygon constructor. Similar to the free-hand drawing with the path object, the polygon constructor takes a series of points to establish the shape. You can customize the polygon with different properties, such as fill color, stroke color, and stroke width, to fit your design needs.

Drawing Text Shapes

Finally, you can add text to your canvas with the fabric.Text constructor. In order to add text to the canvas, specify the content, font size, and position of the text as parameters. You can also customize the styling properties of the text, such as font family, fill color, stroke color, stroke width, and text alignment.

Manipulating Shapes with Fabric.js

Selecting, Moving, and Scaling Objects

Fabric.js makes manipulating shapes on the canvas a breeze, with features like selecting, moving, and scaling objects with ease. With the help of the canvas.setActiveObject() method, you can select and manipulate objects with the mouse cursor. Once an object is selected, you can move it around by dragging it with the cursor, and resize it using the corner points.

Stop Event Propagation and ID

To stop event propagation on shapes, you can use the e.stopPropagation() method. This allows you to prevent events from triggering on an object, so you can handle them manually without interference. You can also add ID to shapes which will help you keep track of individual objects. The ObjectId feature of Fabric.js makes it easy to manipulate specific objects by ID.

Colors, Thickness, and Eraser

Fabric.js allows you to change the colors and thickness of shapes with ease using the set() method. This makes it easy to customize and create unique designs for your canvas. Additionally, if you need to erase part of your canvas, you can use the eraser tool in Fabric.js by utilizing the globalCompositeOperation property of the canvas context.


What is the Difference Between Fabric.js and SVG?

Fabric.js provides an object model for the HTML5 canvas while SVG is a markup language. Fabric.js allows you to create complex shapes and animate them while SVG images are static. When you require interactivity or animation, Fabric.js is the way to go. For simpler shapes and images, SVG can be a better option.

How Do I Add Images to My Fabric.js Project?

Adding images to your Fabric.js project is easy! You can use the built-in fabric.Image.fromURL() method to add images. Just pass it the URL of the image and the image will be added to your canvas. Alternatively, you can use the fabric.Image.fromElement() method to add an image element from your HTML file.

How Do I Animate Shapes in Fabric.js?

Animating shapes in Fabric.js is simple. You can use the built-in fabric.util.animate() method to animate any property of your shapes. Just pass it the object you want to animate, the properties to change, the new values, and the animation options. You can also use the built-in animation functions like fabric.util.animateColor() and fabric.util.ease.easeInOutBack() to create more complex animations.


Overall, Fabric.js is a powerful drawing library for JavaScript that allows developers to create interactive and visually appealing graphics on

elements. In this article, we focused on the topic of comment shape in Fabric.js, exploring the different ways to create and manipulate shapes, as well as how to add comments to shapes. With the help of Fabric.js, developers can easily add advanced functionalities to their canvas graphics, such as dragging, scaling and rotating objects, grouping and manipulating shapes, and more. Whether you’re a beginner or an experienced developer, Fabric.js is definitely worth checking out for your next canvas project.


Here are some helpful resources for working with comment shapes in Fabric.js:

Being a web developer, writer, and blogger for five years, Jade has a keen interest in writing about programming, coding, and web development.
Posts created 491

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top