How to set the style of individual characters in Text using FabricJS?


In this tutorial, we are going to learn how to set the style of individual characters 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. Similarly, we can also set the style of individual characters by using the styles property.

Syntax

new fabric.Text(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 text. 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 Text object

Let’s see a code example to see how our text 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 Text 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 a text object var text = new fabric.Text("Add sample
text here."
, { width: 300, left: 60, top: 70, fill: "green", }); // Add it to the canvas canvas.add(text); </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 a text object var text = new fabric.Text("Add sample
text here."
, { width: 300, left: 110, top: 70, fill: "green", styles: { 0: { 0: { fontSize: 55, fontWeight: "bold", fontStyle: "oblique", } } } }); // Add it to the canvas canvas.add(text); </script> </body> </html>

Updated on: 14-Sep-2022

446 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements