- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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>
- Related Articles
- How to shift the baseline of individual characters in Text using FabricJS?
- How to set the style of individual characters in IText using FabricJS?
- How to insert characters in IText using FabricJS?
- How to set the style of individual characters in Text using FabricJS?
- How to add space between characters in IText using FabricJS?
- How to disable the selectability of IText using FabricJS?
- How to set the baseline of subscript with Text using FabricJS?
- How to set the baseline of superscript with Text using FabricJS?
- How to add underline in IText using FabricJS?
- How to change the colour of cursor in IText using FabricJS?
- How to change the font family of IText using FabricJS?
- How to change the font style of IText using FabricJS?
- How to change the font weight of IText using FabricJS?
- How to set the text overline of IText using FabricJS?
- How to change the cursor width in IText using FabricJS?
