首页 > web前端 > js教程 > 正文

如何使用 FabricJS 查找 Polygon 对象的平移矩阵?

PHPz
发布: 2023-08-23 16:53:48
转载
929 人浏览过

如何使用 FabricJS 查找 Polygon 对象的平移矩阵?

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

为了找到平移矩阵,我们使用_calcTranslateMatrix()方法。此方法返回一个具有给定值的数组 [ 1, 0, 0, 1, A, B];其中 A 是 X 坐标,B 是 Y 坐标。

语法

_calcTranslateMatrix(): Array
登录后复制

示例 1:使用 _calcTranslateMatrix 方法

让我们看一个代码示例,了解如何使用 _calcTranslateMatrix 方法找到多边形的平移矩阵。

<!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 _calcTranslateMatrix method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the translation matrix 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: 60,
            left: 140,
            fill: "red",
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using _calcTranslateMatrix method
      console.log(
         "The translation matrix of the polygon instance is: ", polygon._calcTranslateMatrix()
      );
   </script>
</body>
</html> 
登录后复制

示例 2:使用 _calcTranslateMatrix 方法和 Scale 方法

让我们看一个代码示例,以了解当我们对多边形对象应用变换时,返回数组的值是如何受到影响的。在本例中,我们使用了缩放方法,该方法在 x 和 y 方向上均匀缩放对象。缩放会变换矩阵值,如下所示。

<!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 _calcTranslateMatrix method along with scale method</h2>
   <p>
      You can open console from dev tools and see that the logged output contains the translation matrix 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: 60,
            left: 140,
            fill: "red",
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using scale method
      polygon.scale(2);
      
      // Using _calcTranslateMatrix method
      console.log(
         "The translation matrix of the polygon instance is: ", polygon._calcTranslateMatrix()
      );
   </script>
</body>
</html> 
登录后复制

结论

在本教程中,我们使用两个简单的示例来演示如何使用 FabricJS 查找 Polygon 对象的平移矩阵。

以上是如何使用 FabricJS 查找 Polygon 对象的平移矩阵?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!