- 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 set the style of individual characters in IText using FabricJS?
In this tutorial, we are going to learn how to set the style 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 set the style of individual characters by using the styles property.
Syntax
new fabric.IText(text: String , { styles: 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.
Example 1
Default appearance of the IText object
Let’s see a code example to see how our IText object looks when styles property is not used. In this case, the style of our text looks as there is no change in individual styles of characters.
<!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 IText object</h2> <p>You can see that no styles have been added to individual characters</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", }); // Add it to the canvas canvas.add(itext); </script> </body> </html>
Example 2
Passing the styles property as key
In this example we will 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 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>
- Related Articles
- How to set the style of individual characters in Text using FabricJS?
- How to shift the baseline of individual characters in IText using FabricJS?
- How to insert characters in IText using FabricJS?
- How to change the font style of IText using FabricJS?
- How to shift the baseline of individual characters in Text using FabricJS?
- How to set the text overline of IText using FabricJS?
- How to add space between characters in IText using FabricJS?
- How to get the complete style declaration of character in IText using FabricJS?
- How to set the path side in IText using FabricJS?
- How to set the text alignment in IText using FabricJS?
- How to set the horizontal origin of transformation of IText using FabricJS?
- How to set the vertical origin of transformation of IText using FabricJS?
- How to set the background colour of text lines with IText using FabricJS?
- How to set the quality level in the URL string of IText object using FabricJS?
- How to set the offset amount for text path in IText using FabricJS?
