FabricJS – 點選多邊形物件後如何查詢當前游標位置?


我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表示。由於它是 FabricJS 的基本元素之一,我們還可以透過應用角度、不透明度等屬性輕鬆地對其進行自定義。為了找到點選多邊形物件時當前游標的位置,我們使用getLocalPointer方法。

語法

getLocalPointer( e, pointer ): Object

引數

  • e − 此引數接受一個事件,表示要操作的事件。

  • pointer(可選)− 此引數是一個物件,表示要操作的指標。此引數是可選的。

示例 1:使用 getLocalPointer 方法

讓我們來看一個程式碼示例,說明如何使用getLocalPointer方法查詢相對於多邊形物件的指標座標。只要我們點選多邊形,就會觸發滑鼠按下事件,這使我們能夠檢索多邊形例項當前點選的lefttop位置。

<!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 getLocalPointer method</h2>
   <p>
      You can click on the polygon object while the console from dev tools is opened to see that the logged output contains the x and y coordinates
   </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: "green",
            stroke: "blue",
            strokeWidth: 20,
         }
      );
      
      // Add it to the canvas
      canvas.add(polygon);
      
      // Using getLocalPointer method
      polygon.on("mousedown", function (options) {
         var pointer = this.getLocalPointer(options.e);
         console.log("Coordinates of the pointer relative to the object are: ", pointer);
      });
   </script> 
</body>
</html>

示例 2:使用 getLocalPointer 方法和不同的事件監聽器

讓我們來看一個程式碼示例,瞭解如何使用不同的事件監聽器仍然可以檢索當前游標位置的 x 和 y 座標。在這裡,我們將值作為“傾斜”傳遞,這確保在水平或垂直方向傾斜物件時觸發事件。

透過按Shift 鍵然後沿水平或垂直方向拖動來實現傾斜。您可以開啟控制檯,檢視在物件從控制元件傾斜時是否觸發了事件。

<!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 getLocalPointer method and using a different event listener
   </h2>
   <p>
      You can press the shift-key and drag the middle edge along the x or yaxis to skew the object while the console from dev tools is opened to see that the logged output contains the x and y coordinates
   </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: "green",
            stroke: "blue",
            strokeWidth: 20,
            lockMovementX: true,
            lockMovementY: true,
         }
      );
      
      // Add it to the canvas
      canvas.add(polygon);
      
      // Using getLocalPointer method
      polygon.on("skewing", function (options) {
         var pointer = this.getLocalPointer(options.e);
         console.log(
            "Coordinates of the pointer relative to the object are: ", 
            pointer
         );
      });
   </script>
</body>
</html> 

結論

在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 查詢點選多邊形物件時當前游標的位置。

更新於:2022年12月29日

717 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告