How to center a Text object vertically on canvas using FabricJS?

In this tutorial, we are going to learn how to center a Text object vertically on canvas using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. We can also center the text object vertically on the canvas by using the centerV() method.

Syntax

centerV()

This method centers the text object vertically within the canvas bounds. It automatically calculates the vertical center position based on the canvas height and positions the text accordingly.

Example 1: Default Text Position

Let's see how our text object appears when the centerV() method is not used. In this case, the text object will not be centered vertically on the canvas.

<!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 text object has not been centered vertically on the canvas</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\ntext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
      });

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

Example 2: Using the centerV Method

In this example, we will see how by using the centerV() method, we are able to place the text object exactly at the vertical center of the canvas.

<!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 centerV method</h2>
   <p>You can see that the text object has now been centered vertically on the canvas</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\ntext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
      });
      
      // Add it to the canvas
      canvas.add(text);

      // Using the centerV() method to center text object vertically 
      text.centerV();
   </script>
</body>
</html>

Key Points

  • The centerV() method only affects vertical positioning - horizontal position remains unchanged
  • This method works for any fabric object, not just text
  • You can combine centerV() with centerH() for complete centering
  • The method must be called after adding the object to the canvas

Conclusion

The centerV() method provides a simple way to vertically center text objects on a FabricJS canvas. This is particularly useful for creating balanced layouts and improving visual presentation of text elements.

Updated on: 2026-03-15T23:19:00+05:30

850 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements