如何使用 FabricJS 返回多边形的无数据对象表示?

WBOY
WBOY 转载
2023-08-25 18:54:46 426浏览

如何使用 FabricJS 返回多边形的无数据对象表示?

我们可以通过创建fabric.Polygon的实例来创建一个Polygon对象。多边形对象的特征可以是由一组连接的直线段组成的任何闭合形状。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。我们可以使用 toDatalessObject 方法返回多边形的无数据对象表示。此方法返回多边形实例的对象表示。

语法

toDatalessObject( propertiesToInclude: Array ): Object

参数

propertiesToInclude(可选) - 此参数接受一个 Array,它允许我们添加要包含在输出中的任何属性。该参数是可选的。

示例1:使用toDatalessObject方法

让我们看一个代码示例,了解如何使用 toDatalessObject 方法在控制台中查看 Polygon 对象的无数据对象表示形式。

<!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 toDatalessObject method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the dataless object representation of the polygon instance
   </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 },
         ],
         {
            top: 50,
            left: 50,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the toDatalessObject method
      console.log(
         "Dataless object representation of a Polygon instance is: ",
         polygon.toDatalessObject()
      );
   </script>
</body>
</html> 

示例 2:使用 toDatalessObject 方法添加其他属性

让我们看一个代码示例,了解如何使用 toDatalessObject 方法包含其他属性。在本例中,我们添加了一个名为“name”的自定义属性。我们可以将特定属性作为选项对象中的第二个参数传递给fabric.Polygon实例,并将相同的键传递给toDatalessObject方法。

<!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 toDatalessObject method to add additional properties</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the property called name
   </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 with name key 
      // passed in options 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 },
         ],
         {
            top: 50,
            left: 50,
            name: "Polygon instance",
         } 
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the toDatalessObject method
      console.log(
         "Dataless object representation of a Polygon instance is: ",
         polygon.toDatalessObject(["name"])
      );
   </script>
</body>
</html>

结论

在本教程中,我们使用两个简单的示例来演示如何使用 FabricJS 返回多边形的无数据对象表示。

以上就是如何使用 FabricJS 返回多边形的无数据对象表示?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除