如何在使用FabricJS分组后,仅取消选定的分组多段线的分组

如何在使用FabricJS分组后,仅取消选定的分组多段线的分组

我们可以通过创建 fabric.Polyline 的实例来创建多段线对象。多段线对象可以由一组连接的直线段组成。由于它是FabricJS的基本元素之一,我们还可以通过应用诸如角度、不透明度等属性来轻松自定义它。为了取消分组多段线对象,我们可以使用 toActiveSelection() 方法。

语法

toActiveSelection(): fabric.ActiveSelection

示例1:一键将所有折线分组

在看如何取消分组之前,让我们看一下如何对对象进行分组。在这个示例中,我们会有一个按钮,点击该按钮将所有的折线分组成一个单一的对象。因此,移动该对象将移动所有的折线,并且在调整大小或倾斜时它将作为一个单一的对象行为。

我们将创建一个函数,获取画布中的所有对象并将它们分组成一个单一的对象。

<!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>Grouping all the Polyline objects using one click</h2>
   Click on the `Group` Button to group all the Polyline objects in the canvas 
   <canvas id="canvas"></canvas>
   <button type="button" onclick="group()">Group</button>
   <script>

      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a Polyline object
      var polyLine1 = new fabric.Polyline([
         { x: 500, y: 200 },
         { x: 550, y: 60 },
         { x: 350, y: 100 },
      ], {
         stroke: "green",
         fill: "white",
         strokeWidth: 5,
      });

      // Initiate another Polyline object
      var polyLine2 = new fabric.Polyline([
         { x: 300, y: 200 },
         { x: 150, y: 60 },
         { x: 250, y: 100 },
      ], {
         stroke: "green",
         fill: "white",
         strokeWidth: 5,
      });

      // Add them to the canvas instance
      canvas.add(polyLine1);
      canvas.add(polyLine2);

      // Function to group all the polyline objects into single object
      function group() {

         // Get all the objects as selection
         var sel = new fabric.ActiveSelection(canvas.getObjects(), {
            canvas: canvas,
         });

         // Make the objects active
         canvas.setActiveObject(sel);

         // Group the objects
         canvas.getActiveObject().toGroup();
      } 
   </script>
</body>
</html>

示例2:使用一键解组选定的分组折线

在这个示例中,我们将有一个按钮,点击这个按钮将解组选定的分组折线。因此,对象将会作为独立的对象进行操作。首先,我们将创建两个组的折线,每个组都有两个折线对象。

此外,当选择一个组并按下解组按钮时,我们将使用 toActiveSelection() 来解组这些对象。

<!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>Ungrouping the selected grouped Polylines using one click</h2>
   Click on the grouped object and click on `Ungroup` button to ungroup the objects, further click in empty space to discard the selection 
   <canvas id="canvas"></canvas>
   <button type="button" onclick="ungroup()">Ungroup</button>
   <script>

      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate another Polyline object
      var polyLine1 = new fabric.Polyline([
         { x: 300, y: 100 },
         { x: 150, y: 60 },
         { x: 250, y: 10 },
      ], {
         stroke: "green", 
         fill: "white",
         strokeWidth: 5,
      });

      // Initiate another Polyline object
      var polyLine2 = new fabric.Polyline([
         { x: 400, y: 200 },
         { x: 250, y: 160 },
         { x: 150, y: 200 },
      ], {
         stroke: "blue",
         fill: "white",
         strokeWidth: 5,
      });

      // Group two objects and add to canvas
      var group1 = new fabric.Group([polyLine1, polyLine2]);
      canvas.add(group1);

      // Initiate another Polyline object
      var polyLine4 = new fabric.Polyline([
         { x: 400, y: 10 },
         { x: 350, y: 60 },
         { x: 450, y: 10 },
      ], {
         stroke: "pink",
         fill: "white",
         strokeWidth: 5,
      });

      // Initiate a Polyline object
      var polyLine3 = new fabric.Polyline([
         { x: 500, y: 200 },
         { x: 550, y: 60 },
         { x: 350, y: 100 },
      ], {
         stroke: "red",
         fill: "white",
         strokeWidth: 5,
      });

      // Group two objects and add to canvas
      var group2 = new fabric.Group([polyLine3, polyLine4]);
      canvas.add(group2); 

      // Function to ungroup the selected grouped polyline objects
      function ungroup() {
         canvas.getActiveObject().toActiveSelection();
      }
   </script>
</body>
</html>

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程