Maison  >  Article  >  interface Web  >  Comment trouver la matrice de traduction d'un objet Polygon à l'aide de FabricJS ?

Comment trouver la matrice de traduction d'un objet Polygon à l'aide de FabricJS ?

PHPz
PHPzavant
2023-08-23 16:53:48929parcourir

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

La traduction fait glisser un objet à une distance fixe dans une direction donnée. Nous pouvons créer un objet Polygon en créant une instance de fabric.Polygon. Un objet polygone peut être caractérisé comme n’importe quelle forme fermée constituée d’un ensemble de segments de ligne droite connectés. Puisqu'il s'agit de l'un des éléments de base de FabricJS, nous pouvons également le personnaliser facilement en appliquant des propriétés telles que l'angle, l'opacité, etc.

Pour trouver la matrice de traduction, nous utilisons la méthode _calcTranslateMatrix(). Cette méthode renvoie un tableau [ 1, 0, 0, 1, A, B] avec les valeurs données ; où A est la coordonnée X et B est la coordonnée Y.

Grammaire

_calcTranslateMatrix(): Array

Exemple 1 : Utilisation de la méthode _calcTranslateMatrix

Regardons un exemple de code sur la façon de trouver la matrice de traduction d'un polygone à l'aide de la méthode _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> 

Exemple 2 : Utilisation de la méthode _calcTranslateMatrix et de la méthode Scale

Regardons un exemple de code pour comprendre comment les valeurs du tableau renvoyé sont affectées lorsque nous appliquons une transformation à un objet polygone. Dans ce cas, nous avons utilisé la méthode d'échelle, qui met l'objet à l'échelle uniformément dans les directions x et y. La mise à l'échelle transforme les valeurs matricielles comme indiqué ci-dessous.

<!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> 

Conclusion

Dans ce tutoriel, nous utilisons deux exemples simples pour montrer comment trouver la matrice de traduction d'un objet Polygon à l'aide de FabricJS.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer