首頁  >  文章  >  web前端  >  FabricJS – 找出表示多邊形物件目前變換的變換矩陣?

FabricJS – 找出表示多邊形物件目前變換的變換矩陣?

PHPz
PHPz轉載
2023-08-29 12:17:101227瀏覽

FabricJS – 找到表示多边形对象当前变换的变换矩阵?

我們可以透過建立fabric.Polygon的實例來建立一個Polygon物件。多邊形物件的特徵可以是由一組連接的直線段組成的任何閉合形狀。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自訂它。為了找到表示目前變換的變換矩陣,我們使用 calcOwnMatrix 方法。

文法

calcOwnMatrix(): Array

範例 1:使用 calcOwnMatrix 方法

讓我們來看一個程式碼範例,了解如何使用 calcOwnMatrix 方法找到表示多邊形目前變換的變換矩陣。您可以從開發工具開啟控制台來查看正在顯示的陣列值。

<!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>Using the calcOwnMatrix method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the transform matrix of the polygon instance
   </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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 200, y: 10 },
            { x: 250, y: 50 },
            { x: 250, y: 180 },
            { x: 150, y: 180 },
            { x: 150, y: 50 },
            { x: 200, y: 10 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            skewX: 15,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using calcOwnMatrix method
      console.log(
         "The transform matrix which represents current transformation of the polygon instance is: ",
         polygon.calcOwnMatrix()
      );
   </script>
</body>
</html> 

範例 2:使用 calcOwnMatrix 方法和 ScaleX 屬性

讓我們看一個程式碼範例,以了解當我們對多邊形物件應用水平縮放時,傳回數組的值是如何受到影響的。在這裡,我們向scaleX屬性傳遞了值2。這確保了我們的多邊形物件在水平方向上縮放2倍。我們也可以在控制台中看到傳回數組的第0個索引值發生了變化。這是因為第 0 個索引表示scaleX 值。

<!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>Using the calcOwnMatrix method along with scaleX property</h2>
   <p>
      You can open console from dev tools and see that the logged output has changed
   </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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 200, y: 10 },
            { x: 250, y: 50 },
            { x: 250, y: 180 },
            { x: 150, y: 180 },
            { x: 150, y: 50 },
            { x: 200, y: 10 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            skewX: 15,
            scaleX: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using calcOwnMatrix method
      console.log(
         "The transform matrix which represents current transformation of the polygon instance is: ",
         polygon.calcOwnMatrix()
      );
   </script>
</body>
</html> 

結論

在本教學中,我們使用兩個簡單的範例來示範如何使用 FabricJS 找到表示 Polygon 物件目前變換的變換矩陣。

以上是FabricJS – 找出表示多邊形物件目前變換的變換矩陣?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除