How to set the background colour of text lines with Text using FabricJS?

In this tutorial, we are going to learn how to set the background colour of text lines with 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 background colour of text lines by using the textBackgroundColor property.

Syntax

new fabric.Text(text: String , { textBackgroundColor : String }: 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 textBackgroundColor is a property.

Options Keys

  • textBackgroundColor ? This property accepts a String value which allows us to set the background colour of text lines.

Example 1: Using Hexadecimal Color Values

Let's see a code example to assign a background colour to our text object using hexadecimal value of colour. In this example, we have used the hex colour code of #ebdef0 which is a lilac colour.

<!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 textBackgroundColor property as key with a hexadecimal value</h2>
   <p>You can see the background colour of the text lines</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 \ntext here.", {
         width: 300,
         left: 60,
         top: 70,
         fill: "green",
         textBackgroundColor: "#ebdef0"
      });

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

Example 2: Using RGBA Color Values

We can use an RGBA value, instead of a hexadecimal colour code, which stands for: red, green, blue and alpha. The alpha parameter specifies the opacity of a colour. In this example, we have used the rgba value (255,20,147,0.2) which is the colour pink with 0.2 opacity.

<!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 textBackgroundColor property as key with a RGBA value</h2>
   <p>You can see the new background colour of the text lines</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 \ntext here.", {
         width: 300,
         left: 60,
         top: 70,
         fill: "green",
         textBackgroundColor: "rgba(255,20,147,0.2)"
      });

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

Color Format Options

Format Example Description
Hexadecimal #ebdef0 Standard hex color codes
Named Colors "red", "blue" CSS color names
RGBA rgba(255,20,147,0.2) RGB with alpha transparency

Conclusion

The textBackgroundColor property in FabricJS provides an easy way to highlight text by adding background colors to text lines. You can use various color formats including hex codes, named colors, or RGBA values for transparency effects.

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

404 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements