How to create a Textbox with border colour using FabricJS?

In this article, we are going to create a Textbox with border colour using FabricJS. We can customize, stretch or move around the text written in a textbox. We can also customize the text itself by using properties like fontSize, fontFamily, fontStyle, fontWeight etc. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. One of the properties that FabricJS provides is borderColor which allows us to manipulate the colour of the border when our object is active.

Syntax

new fabric.Textbox(text: String, { borderColor: String }: Object)

Parameters

  • text ? This parameter accepts a String which is the text string that we want to display inside our textbox.

  • options (optional) ? This parameter is an Object which provides additional customizations to our textbox. Using this parameter, properties such as colour, cursor, stroke width, and a lot of other properties can be changed related to the object of which borderColor is a property.

Options Keys

  • borderColor ? This property accepts a String which specifies the colour of the border when our textbox object is selected. Its default value is rgb(178,204,255).

Example 1: Using String Color Value

Let's see a code example of how we can assign a value to the borderColor property. We have assigned the value "red" to the borderColor key which helps to create the red border on selection of our textbox object.

<!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 borderColour key with a String value</h2>
   <p>You can select the textbox to see the red border colour</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 textbox object
      var textbox = new fabric.Textbox("If you don't love it, you're going to fail.", {
         backgroundColor: "#b0e0e6",
         width: 400,
         top: 70,
         left: 110,
         angle: 15,
         borderColor: "red",
      });

      // Add it to the canvas
      canvas.add(textbox);
   </script>
</body>
</html>

Example 2: Using RGB Color Value

Instead of passing simple colour names as a String, we can also use RGB values, whose components specify the amount of Red, Green, and Blue. In this example, we have used rgb(164,0,0) which is the RGB value for the colour dark red.

<!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 an RGB value to the borderColor key</h2>
   <p>You can select the textbox to see the border colour added using the rgb value</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 textbox object
      var textbox = new fabric.Textbox("If you don't love it, you're going to fail.", {
         backgroundColor: "#b0e0e6",
         width: 400,
         top: 70,
         left: 110,
         angle: 15,
         borderColor: "rgb(164,0,0)",
      });

      // Add it to the canvas
      canvas.add(textbox);
   </script>
</body>
</html>

Key Points

  • The borderColor property only appears when the textbox object is selected or active.

  • You can use color names ("red", "blue"), hex values ("#FF0000"), or RGB/RGBA values for the border color.

  • The default border color is rgb(178,204,255) - a light blue shade.

Conclusion

The borderColor property in FabricJS allows you to customize the selection border of textbox objects. You can use various color formats including color names, hex values, and RGB values to achieve the desired visual effect.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements