如何使用FabricJS將僅選定的多段線組合成單個物件?


我們可以透過建立fabric.Polyline的例項來建立一個多段線物件。多段線物件可以由一組連線的直線段來表徵。由於它是FabricJS的基本元素之一,我們也可以透過應用角度、不透明度等屬性來輕鬆定製它。為了對多個多段線物件進行分組,我們可以使用toGroup()方法。

語法

toGroup(): Fabric.Group

示例1:建立fabric.Polyline()的例項並將其新增到我們的畫布

在瞭解如何將多個物件組合成一個物件之前,讓我們先看一個程式碼示例,說明如何將多段線物件新增到我們的畫布中。唯一需要的引數是points陣列,而第二個引數是可選的options物件。

<!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> Creating an instance of fabric.Polyline() and adding it to our canvas </h2>
   <p>You can see that the polyline object has been added</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a points array
      var points = [
         { x: 30, y: 50 },
         { x: 0, y: 0 },
         { x: 60, y: 0 },
      ];
      
      // Initiating a polyline object
      var polyline = new fabric.Polyline(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "green",
      });
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html>

示例2:一鍵分組僅選定的多段線

在這個例子中,我們將有一個按鈕,點選該按鈕後,選定的多段線將被組合成一個單一的物件。因此,移動該物件將移動所有分組的多段線,並且在調整大小或傾斜時,它也將表現為單個物件。

我們將建立一個函式,該函式獲取畫布中所有選定的物件並將它們組合成一個單一的物件。

<!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 only selected Polyline objects using one click</h2>
   <p> Select the polylines by dragging on required area and click on the`Group` Button to group all the selected Polyline objects in the canvas </p>
   <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: 100 },
         { x: 150, y: 60 },
         { x: 250, y: 10 },
      ], {
         stroke: "green",
         fill: "white",
         strokeWidth: 5,
      });

      // Initiate another Polyline object
      var polyLine3 = new fabric.Polyline([
         { x: 400, y: 200 },
         { x: 250, y: 160 },
         { x: 150, y: 200 },
      ], {
         stroke: "green",
         fill: "white",
         strokeWidth: 5,
      });
      
      // Add them to the canvas instance
      canvas.add(polyLine1);
      canvas.add(polyLine2);
      canvas.add(polyLine3);

      // Function to group the selected polyline objects into single object
      function group() {
         canvas.getActiveObject().toGroup();
      }
   </script>
</body>
</html>

更新於:2023年2月16日

228 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告