FabricJS – 檢查多邊形物件是否完全包含在另一個物件內?


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

我們使用isContainedWithinObject來查詢多邊形物件是否完全包含在另一個物件的區域內。它可以用來測試一個物件是否完全包含在另一個物件的區域內。

語法

isContainedWithinObject(other: Object, absolute: Boolean, calculate: Boolean ): Boolean

引數

  • other − 此引數接受一個物件,該物件指定我們要測試的物件。

  • absolute (可選) − 此引數接受一個字串值,該值指定是否使用不帶viewportTransform的座標。此引數是可選的。

  • calculate (可選) − 此引數接受一個布林值,該值指定是否使用當前位置的座標。此引數是可選的。

示例1:使用isContainedWithinObject方法

讓我們來看一個程式碼示例,看看使用isContainedWithinObject方法時的日誌輸出。isContainedWithinObject方法在檢查多邊形物件是否完全包含在另一個物件的區域內時返回true或false。

<!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 isContainedWithinObject method</h2>
   <p> You can open console from dev tools and see that the logged output contains a true value </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: -20, y: -35 },
            { x: 20, y: -35 },
            { x: 40, y: 0 },
            { x: 20, y: 35 },
            { x: -20, y: 35 },
            { x: -40, y: 0 },
         ],
         {
            stroke: "red",
            left: 100,
            top: 50,
            fill: "black",
            strokeWidth: 2,
            strokeLineJoin: "bevil",
         }
      );
      
      // Initiate a rectangle object
      var rectangle = new fabric.Rect({
         width: 390, 
         height: 130,
         top: 40,
         left: 80,
         fill: "transparent",
         stroke: "red",
         strokeWidth: 6,
      });
      
      // Add them to the canvas
      canvas.add(polygon);
      canvas.add(rectangle);
      
      // Using isContainedWithinObject method
      console.log(
         "Is the Polygon object contained within the area of the rectangle object?: ",
         polygon.isContainedWithinObject(rectangle)
      );
   </script>
</body>
</html> 

示例2:使用isContainedWithinObject方法處理多個物件

在這個例子中,我們使用了isContainedWithinObject方法以及兩個矩形物件rect1rect2。由於多邊形物件不包含在rect2的區域內,控制檯返回false值。

<!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 isContainedWithinObject method with multiple objects</h2>
   <p>You can open console from dev tools and see that the logged output  contains a false value  </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: -20, y: -35 },
            { x: 20, y: -35 },
            { x: 40, y: 0 },
            { x: 20, y: 35 },
            { x: -20, y: 35 },
            { x: -40, y: 0 },
         ],
         {
            stroke: "red",
            left: 100,
            top: 50,
            fill: "black",
            strokeWidth: 2,
            strokeLineJoin: "bevil",
         }
      );
      
      // Initiate a rectangle object
      var rect1 = new fabric.Rect({
         width: 390,
         height: 130,
         top: 40,
         left: 80,
         fill: "transparent",
         stroke: "red", 
         strokeWidth: 6,
      });
      
      // Initiate another rectangle object
      var rect2 = new fabric.Rect({
         width: 120,
         height: 40,
         top: 170,
         left: 320,
         fill: "blue",
      });
      
      // Add them to the canvas
      canvas.add(rect1);
      canvas.add(rect2);
      canvas.add(polygon);
      
      // Using isContainedWithinObject method
      console.log(
         "Is the Polygon object contained within the area of the rectangle (rect2) object?: ",
         polygon.isContainedWithinObject(rect2)
      );
   </script>
</body>
</html> 

結論

在本教程中,我們使用兩個簡單的示例演示瞭如何使用FabricJS檢查多邊形物件是否完全包含在另一個物件的區域內。

更新於:2022年12月29日

442 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告