如何使用 FabricJS 使多邊形物件對調整大小事件做出反應?


我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表示。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性來輕鬆自定義它。我們使用object:modified事件使多邊形物件對調整大小做出反應。

語法

object:modified

示例 1:多邊形物件的預設外觀

讓我們來看一個程式碼示例,說明在不使用object:modified事件時多邊形物件的外觀。在這種情況下,多邊形物件將被新增到畫布上。

<!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>Default appearance of the polygon object</h2>
   <p>You can see that the polygon object has been added to the canvas</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polygon instance
      var polygon = new fabric.Polygon(
         [
            { x: 0, y: 0 },
            { x: 0, y: 50 },
            { x: 50, y: 50 },
            { x: 50, y: 0 },
         ],
         {
            left: 100,
            top: 30,
            fill: "red",
            stroke: "blue",
            strokeWidth: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html>

示例 2:顯示物件如何對調整大小做出反應

讓我們來看一個程式碼示例,以檢視調整多邊形物件大小時記錄的輸出。我們使用了object:modified事件,該事件在任何物件轉換或與物件相關的任何更改結束時觸發。在這種情況下,每次我們更改物件比例時,都會記錄縮放後的高度和寬度。

<!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>Displaying how the object reacts to being resized</h2>
   <p>
      You can scale object using corner and open console from dev tools to see that the scaled width and height value of the polygon object is being logged
   </p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a polygon instance
      var polygon = new fabric.Polygon(
         [
            { x: 0, y: 0 },
            { x: 0, y: 50 },
            { x: 50, y: 50 },
            { x: 50, y: 0 },
         ],
         {
            left: 100,
            top: 30,
            fill: "red",
            stroke: "blue",
            strokeWidth: 2,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using object:modified event
      canvas.on("object:modified", (e) => {
         canvas.getActiveObjects().forEach((o) => { 
            console.log(
               "Scaled Height of the polygon is: ",
               o.getScaledHeight(),
               "Scaled Width of the polygon is:",
               o.getScaledWidth()
            );
         });
      });
   </script>
</body>
</html>

結論

在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 使多邊形物件對調整大小事件做出反應。

更新於:2023年1月2日

262 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

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