Home  >  Article  >  Web Front-end  >  How to set vertical origin of text transformation using FabricJS?

How to set vertical origin of text transformation using FabricJS?

王林
王林forward
2023-08-24 19:49:081504browse

How to set vertical origin of text transformation using FabricJS?

In this tutorial, we will learn how to set the vertical origin of text transformations using FabricJS. We can display text on the canvas by adding an instance of Fabric.Text. Not only does it allow us to move, scale and change the dimensions of text, but it also provides additional features such as text alignment, text decoration, line height, which are available through the properties textAlign, underline and lineHeight respectively. Similarly, we can also use the originY attribute to set the vertical origin of the transformation.

grammar

new fabric.Text(text: String , { originY : String }: Object)

parameter

  • text - This parameter accepts String, which is the text string we want to display.

  • Options (optional) - This parameter is an object that provides additional customization to our text. Using this parameter, you can change the color, cursor, border width, and many other properties associated with the object whose originY is the attribute.

Option key

  • originY - This property accepts a String value which allows us to set the vertical origin of the transformation. Possible values ​​are "Top", "Bottom" and "Center". Its default value is "top".

Example 1

Default appearance of text objects

Let's look at a code example to see what a text object looks like without using the originY attribute. In this case, the vertical origin of the transformation will be the top, which is also the default.

<!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>Default appearance of the Text object</h2>
   <p>You can see that the vertical origin of transformation is towards top</p>
   <canvas id="canvas"></canvas>

   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a text object
      var text = new fabric.Text("Add sample text here.", {
         width: 300,
         left: 50,
         top: 70,
         fill: "green",
      });

      // Add it to the canvas
      canvas.add(text);
   </script>
</body>
</html>

Example 2

Pass originY attribute as key to value

In this example we will see how assigning a value to the originY property changes the vertical origin of the transform. We use two text objects in this example to show the differences. In our first text object, since we passed the value as "bottom", the vertical origin of the transform is now at the bottom. The same top attribute of 100 is applied to both texts, but since the vertical origin of the transformation changes, they are still in different vertical positions.

<!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 originY property as key with a value</h2>
   <p>You can see that origin of transformation for the first text object(text1) is bottom while text2 maintains the default vertical origin of transformation</p>
   <canvas id="canvas"></canvas>

   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a text object
      var text1 = new fabric.Text("Text 1", {
         width: 300,
         left: 200,
         top: 100,
         fill: "green",
         originY: "bottom",
      });

      // Initiate a text object
      var text2 = new fabric.Text("Text 2", {
         width: 300,
         left: 50,
         top: 100,
         fill: "red",
      });

      // Add it to the canvas
      canvas.add(text1);
      canvas.add(text2);
   </script>
</body>
</html>

The above is the detailed content of How to set vertical origin of text transformation using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete