Home > Web Front-end > JS Tutorial > body text

How to enable object selection only when the object is completely contained in the selection area in FabricJS?

WBOY
Release: 2023-08-27 11:25:07
forward
1336 people have browsed it

如何仅当对象完全包含在 FabricJS 中的选择区域中时才启用对象选择?

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.

Syntax

new fabric.Canvas(element: HTMLElement|String, { selectionFullyContained: Boolean }: Object)
Copy after login

Parameters

  • Element - This parameter is The element itself can be derived using Document.getElementById() or the id of the element itself. The FabricJS canvas will be initialized on this element.

  • 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.

Example 1

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>
Copy after login

Example 2

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!