如何使用 FabricJS 拉直 Image 对象?

WBOY
WBOY 转载
2023-09-14 16:53:02 1043浏览

如何使用 FabricJS 拉直 Image 对象?

在本教程中,我们将学习如何使用 FabricJS拉直图像对象。 我们可以通过创建fabric.Image的实例来创建一个Image对象。既然是一个 FabricJS 的基本元素,我们还可以通过应用轻松自定义它 诸如角度、不透明度等属性。为了拉直图像对象,我们使用 拉直方法。

语法

straighten(): fabric.Object

在不使用straighten的情况下向角度属性传递值 方法

示例

让我们看一个代码示例,看看当 straighten 时我们的 Image 对象看起来如何 方法没有被使用。 straighten 方法通过将对象从其所在位置旋转来straighten 当前角度为 0、90、180 或 270 等,具体取决于更接近的角度。角度 属性设置对象的旋转角度(以度为单位)。在这里,我们指定了 角度为 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 Image object has an angle of 45 degrees</p>
   <canvas id="canvas"></canvas>
   <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         angle: 45,
      });
      
      // Add it to the canvas
      canvas.add(image);
   </script>
</body>
</html>

Using the straighten 方法

示例

让我们看一个代码示例,看看当 straighten 时 Image 对象是什么样子的 方法与角度属性结合使用。虽然我们已经设定了角度 旋转为 45 度,我们的图像对象将通过将其旋转回 0 来拉直 degree as we have used the 拉直方法。

<!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 image object
   </p>
   <canvas id="canvas"></canvas>
   <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating the image element
      var imageElement = document.getElementById("img1");
      
      // Initiate an Image object
      var image = new fabric.Image(imageElement, {
         top: 50,
         left: 110,
         angle: 45,
      });
      
      // Add it to the canvas
      canvas.add(image);
      
      // Using the straighten method
      image.straighten();
   </script>
</body>
</html>

以上就是如何使用 FabricJS 拉直 Image 对象?的详细内容,更多请关注php中文网其它相关文章!

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