Mastering Object Deletion in Fabric.js 5: Effortlessly Remove Selected Elements Programmatically

Dinesh Rawat
5 min readMay 30, 2023
Photo by Sigmund on Unsplash

The Fabric.js 5 JavaScript library enables developers to create stunning graphical objects and manipulate them effortlessly within the HTML5 canvas. One of the most frequently requested canvas capabilities is the ability to delete selected items.

In this comprehensive guide, we will delve into the implementation of programmatically deleting selected objects in Fabric.js 5. You will gain a profound understanding of leveraging the remove method to selectively remove polygons and circles from your canvas.

Understanding the Fabric.js 5 Polygon Object and Delete Operation

The fabric.Polygon Object in Fabric.js 5

The fabric.Polygon object in Fabric.js 5 is a crucial element for constructing closed shapes made up of connected straight-line segments. Let’s explore an example that illustrates the creation of a fabric.Polygon object:

<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library from Cloudflare CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/5.3.1/fabric.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
// Initialize a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Define the coordinates for the first polygon
var points1 = [
{ x: 30, y: 50 },
{ x: 70, y: 50 },
{ x: 0, y: 0 },
{ x: 70, y: 0 }
];

// Create the first fabric.Polygon object
var polygon1 = new fabric.Polygon(points1, {
left: 100,
top: 40,
fill: "#1e90ff",
strokeWidth: 4,
stroke: "black",
scaleX: 1.2,
scaleY: 1.2
});

// Define the coordinates for the second polygon
var points2 = [
{ x: 60, y: 90 },
{ x: 45, y: 30 },
{ x: 20, y: 48 }
];

// Create the second fabric.Polygon object
var polygon2 = new fabric.Polygon(points2, {
left: 30,
top: 30,
fill: "#080808",
strokeWidth: 4,
stroke: "red",
scaleX: 2,
scaleY: 2
});

// Add both polygon objects to the canvas
canvas.add(polygon1, polygon2);
</script>
</body>
</html>

We define the coordinates for the two polygons using variables points1 and points2.

These variables represent the vertices of the polygons, outlining their shape and structure. The fabric.Polygon constructor is used to create the polygons, and each polygon is customized with specific properties, including position, fill color, stroke width, stroke color, and scaling.

These properties are assigned individually to each polygon, allowing for distinct visual characteristics and appearance.

Finally, we add both polygon objects to the canvas using the canvas.add() method, ensuring their visibility on the canvas. This code sets up a canvas with two polygons rendered and ready for further manipulation or interaction.

Introducing the Remove Method for Deleting Objects

The remove method in Fabric.js 5 is a pivotal feature that allows the deletion of objects from the canvas. Let’s examine its usage through a concise example:

// Removing an object from the canvas using the remove method
canvas.remove(polygon);

By comprehending the fabric.Polygon object and the remove method, you will possess the essential knowledge to implement the delete operation selectively for objects in your Fabric.js 5 applications.

Implementing Delete Operation for Selected Polygon Objects Programmatically

Creating the deletePolygonHandler Function for Removing Selected Polygons

To enable the delete operation for selected polygon objects in Fabric.js 5, we can implement a handy function called deletePolygonHandler. This function will be triggered when a mouse:down event occurs, allowing identification and removal of the selected object from the canvas.

Example code:

// Function to handle deletion of polygons
var deletePolygonHandler = function(obj) {
var selectedObject = obj.target;
canvas.remove(selectedObject); // Remove the selected polygon from the canvas
canvas.renderAll(); // Render the updated canvas
};

// Binding the deletePolygonHandler function to the mouse:down event
canvas.on("mouse:down", deletePolygonHandler);

By binding the deletePolygonHandler function to the mouse:down event, you can effortlessly delete selected polygons within your Fabric.js 5 application. In the subsequent sections, we will further enhance this functionality and explore additional examples.

Enabling Selective Deletion of Circle Objects Programmatically

In this section, we will demonstrate how to enable the selective deletion of circle objects in Fabric.js 5. By following a similar approach to the one used for polygons, we will introduce the deleteCircleHandler function. This function will be responsible for removing the selected circle object from the canvas when a mouse:down event is triggered.

Let’s examine the updated code snippet:

<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library from Cloudflare CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/5.3.1/fabric.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
// Initialize a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);

// Define the properties for the first circle
var circle1 = new fabric.Circle({
radius: 30,
left: 100,
top: 80,
fill: "#1e90ff",
strokeWidth: 4,
stroke: "black",
scaleX: 1.2,
scaleY: 1.2
});

// Define the properties for the second circle
var circle2 = new fabric.Circle({
radius: 15,
left: 30,
top: 30,
fill: "#080808",
strokeWidth: 4,
stroke: "red",
scaleX: 2,
scaleY: 2
});

// Add both circle objects to the canvas
canvas.add(circle1, circle2);

// Function to handle deletion of circles
var deleteCircleHandler = function(obj) {
var selectedObject = obj.target;
canvas.remove(selectedObject); // Remove the selected circle from the canvas
canvas.renderAll(); // Render the updated canvas
};

// Bind the deleteCircleHandler function to the mouse:down event
canvas.on("mouse:down", deleteCircleHandler);
</script>
</body>
</html>

Your Fabric.js 5 application can now effortlessly delete selected circle objects thanks to the deleteCircleHandler function.

Conclusion

In this comprehensive guide, we have explored the process of programmatically deleting selected objects in Fabric.js 5. By mastering the remove method and implementing delete handlers for both polygons and circles, you can empower your canvas-based applications with efficient and intuitive delete functionality.

Deleting selected objects is a crucial feature when working with graphical elements on the HTML5 canvas. Fabric.js 5 simplifies this process by providing the remove method, allowing you to remove specific objects with ease. By leveraging the power of Fabric.js 5, you can create visually stunning applications and seamlessly manage object deletion.

Throughout this guide, we covered the creation and customization of fabric.Polygon objects, as well as the steps to remove them from the canvas programmatically. We also demonstrated how to enable selective deletion of circle objects using similar techniques. By applying these concepts, you can tailor the delete functionality to suit your specific application requirements.

Now equipped with the knowledge gained from this guide, you can confidently implement powerful object deletion capabilities in your own projects. Whether you are building interactive graphics, data visualizations, or any canvas-based application, Fabric.js 5 provides the tools you need to manage and manipulate objects effortlessly.

So go ahead and dive into the world of Fabric.js 5, explore its vast capabilities, and unlock the potential to create captivating visual experiences while maintaining control over object deletion.

Happy coding!

For more topics related to Fabric.js, you can refer to https://prodeasy.com/the-product-mindset for many other topics related to fabric js.

--

--

Dinesh Rawat

Seasoned software engineer, Content creator, Helping teams achieve their goals. https://www.linkedin.com/in/dinesh-rawat/