
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Disable Selection of Objects in FabricJS Canvas
In this article, we are going to illustrate how you can disable the selection of objects via dragging in FabricJS. In a FabricJS canvas, we can basically click anywhere and select an area and any object in that area will get selected. In this article, we will see how to disallow this behavior
Syntax
new fabric.Canvas(element: HTMLElement|String, {selection: boolean}: Object)
Parameters
element − This parameter is the <canvas> element itself which can be derived using document.getElementById() or the id of the <canvas> element itself. The FabricJS canvas will be initialized on this element
options (optional) − This parameter is an Object which provides additional customizations to our canvas. Using this parameter, properties such as color, cursor, border width, and a lot of other properties can be changed related to the canvas. The selection parameter indicates whether selection should be enabled. The default value of this key is True.
Example 1
Let us first see how a selection via dragging looks like when it is enabled. For this example, we will pass the selection key as True which is also the default value. Let's see how the canvas behaves with selection enabled.
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Disabling the selection of objects on a canvas</h2> <p>Here you can select the object as the selection key is True</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { selection: true }); // Creating an instance of the fabric.Circle class var cir = new fabric.Circle({ radius: 40, fill: "#87a96b", left: 30, top: 20, }); // Adding it to the canvas canvas.add(cir); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Example 2
The selection key specifies whether the selection of objects in the canvas via dragging should be enabled or disabled. If we set that key to False, then we no longer be able to select the object via dragging.
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Disabling the selection of objects on a canvas</h2> <p> Here you cannot select an area around the object as the selection key is set to False.</p> <canvas id="canvas"></canvas> <script> //Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { selection: false }); //creating an instance of the fabric.Circle class var cir = new fabric.Circle({ radius: 40, fill: "#87a96b", left: 30, top: 20, }); //adding it to the canvas canvas.add(cir); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Now that we've set selection to False, we can no longer be able to select a section around the object to drag it. However, we can still manually click and select the object.