How to compute the height of a character at given position in Text using FabricJS?


In this tutorial, we are going to learn how to compute the height of line at required line index in Text 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 compute the height of line at required line index by using the getHeightOfLine method.

Syntax

getHeightOfLine(lineIndex: Number)

Parameters

  • lineIndex − This parameter accepts a Number as value. The value given determines the index of the line we want to calculate the height of.

Example 1

Using the getHeightOfLine method

Let’s see a code example to see the logged output when the getHeightOfLine method is used. In this case, the height of the line at the 1st line index will be displayed in the console.

<!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 getHeightOfLine method</h2> <p>You can open console from dev tools and see that the height of line is being displayed in the console </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, fill: "green", fontWeight: "bold", }); // Add it to the canvas canvas.add(text); // Using getHeightOfLine method console.log("The height of line is", text.getHeightOfLine(1)); </script> </body> </html>

Example 2

Using the getHeightOfLine method and passing a different font size

Let’s see a code example to see the logged output when the getHeightOfLine method is used along with a different font size of 30. In this case, the height of the line at the 0th line index will be displayed of given font size.

<!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 getHeightOfLine method and passing a different font size</h2> <p>You can open console from dev tools and see that the height of line is being displayed in the console </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, fill: "green", fontWeight: "bold", fontSize: 30 }); // Add it to the canvas canvas.add(text); // Using getHeightOfLine method console.log("The height of line is", text.getHeightOfLine(0)); </script> </body> </html>

Updated on: 13-Sep-2022

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements