Fabric.js 图像角度属性

Fabric.js 图像角度属性

在本文中,我们将看到如何使用FabricJS设置画布图像的角度。画布意味着图像是可移动的,可以根据需求进行拉伸。此外,当涉及到角度、描边宽度、填充等时,图像可以进行自定义。

要实现这一点,我们将使用一个名为FabricJS的JavaScript库。导入库之后,我们将在body标签中创建一个包含我们图像的画布块。此外,我们将创建一个包含要添加到画布中的图像的img元素,并将其style属性设置为display: none;,因为我们不希望图像在画布外可见。之后,我们将实例化由FabricJS提供的Canvas和Image,并在Canvas上呈现Image,并使用angle属性设置画布图像的角度,如下面的示例所示。

语法:

fabric.Image({
    image,
    angle: number
}); 

参数:

此函数接受上述提及并以下所述的两个参数:

  • image: 它指定图像对象。
  • angle: 它指定图像的角度(度)。

示例:

此示例使用FabricJS来设置画布图像的角度。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>  
        Fabric.js | Image angle Property 
    </title> 
  
    <!-- FabricJS CDN -->
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js"> 
    </script> 
</head> 
  
<body> 
    <div style="text-align: center;width: 600px;"> 
        <h1 style="color: green;"> 
            GeeksforGeeks 
        </h1> 
        <b> 
            Fabric.js | Image angle Property 
        </b> 
    </div> 
  
    <div style="text-align: center;"> 
        <canvas id="canvas" width="600" height="400" 
                style="border:1px solid #000000;"> 
        </canvas> 
    </div> 
  
    <!-- Add the image to be used in the canvas and hide it here  
         because only need it inside the canvas -->
    <img style="display: none;" src="https://media.geeksforgeeks.org/wp-content/uploads/20200327230544/g4gicon.png"
        id="my-image" alt=""> 
          
    <script> 
        // Initiate a Canvas instance 
        var canvas = new fabric.Canvas("canvas"); 
  
        // Get the image element 
        var image = document.getElementById('my-image'); 
  
        // Initiate a Fabric instance 
        var fabricImage = new fabric.Image(image, { 
            angle: 30 
        }); 
  
        // Add the image to canvas 
        canvas.add(fabricImage); 
    </script> 
</body> 
  
</html>                    

输出:
Fabric.js 图像角度属性

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程