欢迎光临
我们一直在努力

如何使用 FabricJS 拉直 Image 对象?

如何使用 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="max-width:90%" / alt="如何使用 FabricJS 拉直 Image 对象?" >
   <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="max-width:90%" / alt="如何使用 FabricJS 拉直 Image 对象?" >
   <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>
赞(0) 打赏
未经允许不得转载:码农资源网 » 如何使用 FabricJS 拉直 Image 对象?
分享到

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册