How to insert characters in IText using FabricJS?


In this tutorial, we are going to learn about how to insert 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 insert characters by using the insertChars method.

Syntax

insertChars(text: String, style: Array, start: Number, end: Number)

Parameters

  • text − This parameter accepts a String which specifies the text to be inserted.

  • style − This parameter accepts an Array which denotes the array of style objects to be applied to text.

  • start − This parameter accepts a Number which denotes the position where the character is to be inserted.

  • end − This parameter accepts a Number which marks the end position. It defaults to start + 1.

Example 1

Default appearance of IText object

Let’s see a code example to see the default appearance of IText object when insertChars method is not used. In this case, no characters will be inserted

<!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 IText object</h2> <p>You can see that no characters have been inserted.</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
Lorem ipsum
dolor sit amet"
,{ width: 300, left: 50, top: 70, fill: "#b666d2", backgroundColor: "#f8f4ff", } ); // Add it to the canvas canvas.add(itext); </script> </body> </html>

Example 2

Using the insertChars method

Let’s see a code example to see the appearance of IText object when the insertChars method is used. In this case, “a” has been inserted in the place of “m" in the 6th index since that is the start position. As we have provided a style array, the necessary style changes have also been applied.

<!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 insertChars method</h2> <p>You can see the character "m" has been replaced with "a"</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
Lorem ipsum
dolor sit amet"
,{ width: 300, left: 50, top: 70, fill: "#b666d2", backgroundColor: "#f8f4ff", } ); // Using the insertChars method itext.insertChars("a", [{ fill: "green", fontStyle: "bold" }], 6, 7); // Add it to the canvas canvas.add(itext); </script> </body> </html>

Updated on: 13-Sep-2022

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements