如何使用 FabricJS 將多個 Polyline 物件組合成單個物件?


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

語法

toGroup(): Fabric.Group

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

在瞭解如何將多個物件組合成一個物件之前,讓我們先看一個程式碼示例,說明如何將 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:一鍵組合所有 Polyline

在這個例子中,我們將有一個按鈕,單擊該按鈕將所有 Polyline 組合成一個物件。因此,移動該物件將移動所有 Polyline,並且在調整大小或傾斜時,它也將表現為單個物件。我們將建立一個函式,該函式獲取畫布中的所有物件並將它們組合成一個物件。

<!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>
   <p> Click on the `Group` Button to group all the 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: 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>

更新於:2023年2月16日

504 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.