Home > Web Front-end > JS Tutorial > body text

How to insert characters in IText using FabricJS?

WBOY
Release: 2023-08-31 08:53:03
forward
911 people have browsed it

How to insert characters in IText using FabricJS?

In this tutorial, we will learn how to insert characters in IText using FabricJS. The IText class was introduced in FabricJS version 1.4, which extends Fabric.Text and is used to create IText instances. IText instances give us the freedom to select, cut, paste or add new text without additional configuration. There are also various supported key combinations and mouse/touch combinations to make text interactive that are not available in Text.

However, IText-based Textbox allows us to resize the text rectangle and wrap it automatically. This is not the case for IText, as the height does not adjust based on line breaks. We can manipulate IText objects by using various properties. Likewise, we can insert characters using the insertChars method.

grammar

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

parameter

  • text - This parameter accepts a String that specifies the text to insert.

  • style - This parameter accepts an array, representing an array of style objects to be applied to the text.

  • start - This parameter accepts a number, indicating the position where the character is to be inserted.

  • end - This parameter accepts a Number that marks the end position. Default is start 1.

Example 1

Default appearance of IText object

Let's look at a code example to see the default appearance of an IText object when not using the insertChars method. 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 HereLorem 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>
Copy after login

Example 2

Use insertChars method

Let's look at a code example to see what an IText object looks like when using the insertChars method. In this example, "a" has been inserted into the 6th index at position "m" because this is the starting position. Since we provided an array of styles, the necessary style changes are also 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 HereLorem 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>
Copy after login

The above is the detailed content of How to insert characters in IText using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template