Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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\nLorem ipsum \ndolor 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\nLorem ipsum \ndolor 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>
How It Works
The insertChars method works by specifying the start and end positions in the text where characters should be inserted or replaced. When you provide a start position of 6 and end position of 7, it replaces the character at index 6 with the new character. The style array allows you to apply custom formatting to the inserted text, such as color, font weight, or font style.
Conclusion
The insertChars method in FabricJS IText provides a powerful way to dynamically insert and style characters at specific positions. This method is particularly useful for creating interactive text editing functionality in canvas applications.
