首頁  >  文章  >  web前端  >  如何使用 FabricJS 使多邊形物件對旋轉事件做出反應?

如何使用 FabricJS 使多邊形物件對旋轉事件做出反應?

WBOY
WBOY轉載
2023-08-31 08:05:02842瀏覽

如何使用 FabricJS 使多边形对象对旋转事件做出反应?

我們可以透過建立fabric.Polygon的實例來建立一個Polygon物件。多邊形物件的特徵可以是由一組連接的直線段組成的任何閉合形狀。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自訂它。我們使用 rotating 事件來示範如何讓多邊形物件對旋轉做出反應通過控制。

文法

polygon.on(“rotating”, callbackFunction);

範例 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>Displaying how the object reacts to the rotating event</h2>
   <p>You can rotate the object to see the callback function fired</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: 500, y: 20 },
            { x: 550, y: 60 },
            { x: 550, y: 200 },
            { x: 350, y: 200 },
            { x: 350, y: 60 },
            { x: 500, y: 20 },
         ],
         {
            fill: "red",
            stroke: "blue",
            strokeWidth: 2,
            objectCaching: false,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the rotating event
      polygon.on("rotating", () => {
         canvas.renderAll();
         console.log("The polygon object is rotating");
      });
   </script>
</body>
</html>

範例 2:發生旋轉時變更填滿顏色

讓我們看一個程式碼範例,以了解如何在發生旋轉事件時更改填滿顏色。我們可以使用中間旋轉(mtr)控制項來旋轉畫布上的物件。在這裡,當我們使用 mtr 控制項旋轉多邊形物件時,填滿顏色變為「綠色」。

<!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>Changing the fill colour when rotate happens</h2>
   <p>
      You can see that the fill colour changes when the polygon is rotated
   </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: 500, y: 20 },
            { x: 550, y: 60 },
            { x: 550, y: 200 },
            { x: 350, y: 200 },
            { x: 350, y: 60 },
            { x: 500, y: 20 },
         ],
         {
            fill: "red",
            stroke: "blue",
            strokeWidth: 2,
            objectCaching: false,
            top: 50,
            left: 30, 
            scaleX: 0.5,
            scaleY: 0.5
         }
      );

      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the rotating event
      polygon.on("rotating", () => {
         polygon.set("fill", "green")
         canvas.renderAll();
      });
   </script>
</body>
</html>

結論

在本教學中,我們使用兩個簡單的範例來示範如何使用 FabricJS 來讓多邊形物件對旋轉事件做出反應。

以上是如何使用 FabricJS 使多邊形物件對旋轉事件做出反應?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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