In this article, we will learn how to disable canvas interactivity in FabricJS. The interaction layer on top of each object is one of the unique features of FabricJS. Once we initialize the canvas, we can select objects, drag them, or manipulate group selections. However, all of this can be undone by specifying the interaction property as False.
new fabric.Canvas(element: HTMLElement|String, { interactive : Boolean }: Object)
Element - This parameter is
Options (optional) - This parameter is an object that provides additional customization of our canvas. Using this parameter, it is possible to change properties related to the canvas such as color, cursor, border width, and many other properties, among which Interactive is the property by which we can decide whether we want an interactive canvas or not. The default value of this property is True.
When interactive properties are enabled
After enabling interactivity, we can freely drag objects and select them and operate them as needed. We can see in the code example below -
<!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 interactivity of canvas</h2> <p>Here you can drag the object and manipulate them freely as we have set the <b>interactive</b> property to True. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { interactive: true, }); // Creating an instance of the fabric.Rect class var obj1 = new fabric.Rect({ left: 170, top: 90, width: 60, height: 80, fill: "#966fd6", angle: 90, }); var obj2 = new fabric.Rect({ left: 200, top: 120, width: 60, height: 80, fill: "#ffa343", angle: 56, }); // Adding it to the canvas canvas.add(obj1); canvas.add(obj2); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Passing interaction keys to class
Let’s see a code example , learn how to disable canvas interactivity. We can assign a False value to the interaction property, which will eliminate the interaction layer on top of the object in the canvas.
<!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 interactivity of canvas</h2> <p>Here you cannot select an area around the objects and manipulate them freely, as we have set the <b>interactive</b> property as False. </b> </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { interactive: false, }); // Creating an instance of the fabric.Rect class var obj1 = new fabric.Rect({ left: 170, top: 90, width: 60, height: 80, fill: "#966fd6", angle: 90, }); var obj2 = new fabric.Rect({ left: 200, top: 120, width: 60, height: 80, fill: "#ffa343", angle: 56, }); // Adding it to the canvas canvas.add(obj1); canvas.add(obj2); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
The above is the detailed content of How to disable interactivity of canvas using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!