如何使用 FabricJS 判斷給定物件是否為 Polyline 例項?


我們可以透過建立 fabric.Polyline 的例項來建立 Polyline 物件。Polyline 物件可以由一組連線的直線段來表徵。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性輕鬆自定義它。

為了判斷給定物件是否為 Polyline 例項,我們使用 isType 方法。此方法檢查物件是否為指定型別,並根據此返回 true 或 false 值。

語法

isType(type: String): Boolean

這裡,type 是一個引數,它接受一個字串,指定我們要檢查的型別。

示例 1:使用 isType 方法

讓我們看一個程式碼示例,以檢視使用 isType 方法時的日誌輸出。isType 方法根據例項的型別是否與我們要檢查的型別匹配返回 true 或 false 值。在本例中,由於型別匹配,因此返回 true 值。

<!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 isType method</h2>
   <p>You can open console from dev tools and see the logged output</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 polyline object
      var polyline = new fabric.Polyline(
         [
            { 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,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polyline);
      
      // Using isType method
      console.log(
         "Is the specified type identical to a polyline instance? : ",
         polyline.isType("polyline")
      );
   </script>
</body>
</html>

示例 2:使用帶有不同值的 isType 方法

在本例中,我們使用了 isType 來檢查指定的圓形型別是否與 Polyline 例項相同。這裡返回 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 isType method with a different value</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 polyline object
      var polyline = new fabric.Polyline(
         [
            { 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,
         } 
      );
      
      // Adding it to the canvas
      canvas.add(polyline);
      
      // Using isType method
      console.log(
         "Is the specified type identical to a polyline instance? : ",
         polyline.isType("circle")
      );
   </script>
</body>
</html>

更新時間: 2023年2月16日

108 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.