In this article, we will learn how to use FabricJS to enable selection of an object only when it is fully contained in the selection area. We can use the SelectionFullyContained property to achieve this.
new fabric.Canvas(element: HTMLElement|String, { selectionFullyContained: Boolean }: Object)
Element - This parameter is
Options (optional) - This parameter is an object that provides additional customization of our canvas. Using this parameter, you can change the color, cursor, border width and many other properties related to the canvas, of which selectionFullyContained is a property. It accepts a Boolean value that determines whether the object should be selected only if it is completely contained in the selection area. The default value is False.
Passing a SelectionFullyContained key with a value of False
Let’s look at a code example of the default behavior in FabricJS , that is, the object is still selected even though it is not completely contained in the selection area. In this example, we pass the value of SelectionFullyContained as False.
<!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>Enabling selection of an object only when it is fully contained in a selection area</h2> <p>Select a partial area around the object. The entire object would be selected even if you select a partial area containing the object.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { selectionFullyContained: false }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 215, top: 100, radius: 50, fill: "orange", }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Passing the selectionFullyContained key to a class with a value of True
Let’s look at a code example where the value of the selectionFullyContained attribute has Already set to True. As we can see, an object is only selected if it is completely contained in the selection area.
<!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>Enabling selection of an object only when it is fully contained in a selection area</h2> <p>Here you cannot select the object by selecting a partial area around the object. The object must be fully contained inside the selection area.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { selectionFullyContained: true }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 215, top: 100, radius: 50, fill: "orange" }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
The above is the detailed content of How to enable object selection only when the object is completely contained in the selection area in FabricJS?. For more information, please follow other related articles on the PHP Chinese website!