How to shift the baseline of individual characters in IText using FabricJS?


In this tutorial, we are going to learn how to shift the baseline of individual characters in IText using FabricJS. The IText class was introduced in FabricJS version 1.4, extends fabric.Text and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text.

Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines automatically. This is not true for IText as height is not adjusted based on the wrapping of lines. We can manipulate our IText object by using various properties. Likewise, we can also shift the baseline of individual characters by using the deltaY property.

Syntax

new fabric.IText(text: String , { styles: { deltaY: Number }:Object }: Object)

Parameters

  • text − This parameter accepts a String which is the text string that we want to display.

  • options (optional) − This parameter is an Object which provides additional customizations to our IText object. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which styles is a property.

Options Keys

  • styles − This property accepts an Object value which allows us to add styles to individual characters.

  • deltaY − This property accepts a Number value which allows us to shift the baseline for styles only.

Example 1

Passing only the styles property as key

In this example, we can see how we can add individual styles to characters by using the styles property. As we can see in this example, only the 0th character will have fontSize as 55, fontWeight as bold and fontStyle as “oblique”. The first level property is the line number and second level property is the character number. Here we are using 0 for both which means first line and first character.

<!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 only the styles property as key</h2> <p>You can see that the first character looks different now</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 an itext object var itext = new fabric.IText("Add sample text here.", { width: 300, left: 310, top: 70, fill: "#9455da", styles: { 0: { 0: { fontSize: 55, fontWeight: "bold", fontStyle: "oblique", }, }, }, }); // Add it to the canvas canvas.add(itext); </script> </body> </html>

Example 2

Passing the styles property as key along with deltaY property

In this example, we will see how we can add a different baseline to characters by using the deltaY property. In this case the 2nd number (first index) in the second line (first index) has a different baseline from its neighbouring characters due to the deltaY being specified.

<!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 styles property as key along with deltaY property</h2> <p>You can see that the second number in the second line has a different baseline</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 an itext object var itext = new fabric.IText("Add sample text here.
H2O"
, { width: 300, left: 310, top: 70, fill: "#9455da", styles: { 1: { 0: { fontSize: 55, fontWeight: "bold", fill: "red", }, 1: { deltaY: 15, fill: "blue", }, 2: { fontSize: 55, fontWeight: "bold", fill: "red", }, }, }, }); // Add it to the canvas canvas.add(itext); </script> </body> </html>

Updated on: 13-Sep-2022

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements