Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to center a Text object horizontally on canvas using FabricJS?
In this tutorial, we are going to learn how to center a Text horizontally 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 horizontally on the canvas by using the centerH method.
Syntax
centerH()
Example 1: Default Text Position
Let's see a code example to see how our text object looks when the centerH method is not used. In this case, the text object will not be centered horizontally 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 horizontally 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 centerH Method
In this example, we will see how by using the centerH method, we are able to place the text object exactly at the horizontal center of the canvas. The centerH() method calculates the horizontal center of the canvas and positions the text accordingly.
<!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 centerH method</h2>
<p>You can see that the text object has now been centered horizontally 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 centerH() method to center text object horizontally
text.centerH();
</script>
</body>
</html>
Key Points
- The
centerH()method is called on the text object after adding it to the canvas - This method automatically calculates the horizontal center position based on the canvas width
- The text object's position is updated to align its center with the canvas center horizontally
- You can combine
centerH()withcenterV()to center text both horizontally and vertically
Conclusion
The centerH() method in FabricJS provides a simple way to horizontally center text objects on the canvas. This is particularly useful for creating balanced layouts and ensuring text appears centered regardless of canvas size.
