首页 > 文章列表 > 如何使用 FabricJS 拉直旋转的 Polygon 对象?

如何使用 FabricJS 拉直旋转的 Polygon 对象?

229 2023-08-31

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

我们可以使用straighten方法拉直旋转的Polygon对象。 straighten 方法通过将对象从当前角度旋转到 0、90,180 或 270 等角度来拉直对象,具体取决于更接近的角度。

语法

straighten(): fabric.Object

示例 1:在不使用 Straighten 方法的情况下向 Angle 属性传递值

让我们看一个代码示例,看看不使用 straighten 方法时 Polygon 对象的外观。 angle 属性设置对象的旋转角度(以度为单位)。在这里,我们将角度指定为 45 度。但由于我们没有应用 straighten 属性,因此旋转角度将保持为 45 度。

<!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>
      Passing the angle property a value without using the straighten method
   </h2>
   <p>You can see that the polygon has an angle of 45 degrees</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",
            angle: 45,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html> 

示例 2:使用拉直方法

让我们看一个代码示例,看看当 straighten 方法与 angle 属性结合使用时,Polygon 对象是什么样子。虽然我们将旋转角度设置为 45 度,但我们的多边形对象将通过将其旋转回 0 度来拉直,因为我们使用了 straighten 方法。

<!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 straighten method</h2>
   <p>
      You can see that the angle of rotation is 0 degree for the polygon object
   </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",
            angle: 45, 
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the straighten method
      polygon.straighten();
   </script>
</body>
</html>

结论

在本教程中,我们使用两个简单的示例来演示如何使用 FabricJS 拉直旋转的 Polygon 对象。