How to add stroke to Text using FabricJS?

In this tutorial, we are going to learn how to add stroke to 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. We can add stroke by using the stroke property.

Syntax

new fabric.Text(text: String, { stroke: 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, stroke width and a lot of other properties can be changed related to the object of which stroke is a property.

Options Keys

  • stroke ? This property accepts a String which determines the colour of that object's border.

Example 1: Using Hexadecimal Color Value

Let's see a code example to understand how our text object appears when the stroke property is used. A hexadecimal colour code starts with a # followed by a six-digit number representing a colour. In this case we have used "#ffc0cb" which is the colour pink.

<!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 stroke property as key with a hexadecimal value</h2>
   <p>You can see that the stroke around the text is of pink 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 text object
      var text = new fabric.Text("Add Sample Text Here", {
         top: 70,
         left: 50,
         fontStyle: "bold",
         fill: "black",
         stroke: "#ffc0cb",
      });

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

Example 2: Using RGBA Color Value

In this example, we will see how to assign an rgba value to the stroke property. 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 case, we have passed the rgba value as (0,128,0,1) which is the colour green with opacity 1.

<!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 rgba value to the stroke property</h2>
   <p>You can see that the stroke around the text is of green 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 text object
      var text = new fabric.Text("Add Sample Text Here", {
         top: 70,
         left: 50,
         fontStyle: "bold",
         fill: "black",
         stroke: "rgba(0,128,0,1)",
      });

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

Additional Stroke Properties

You can further customize the stroke by combining it with other properties:

  • strokeWidth ? Controls the thickness of the stroke
  • strokeLineJoin ? Defines how stroke lines join (miter, round, bevel)
  • strokeLineCap ? Controls the end caps of stroke lines (butt, round, square)

Conclusion

Adding stroke to text in FabricJS is simple using the stroke property. You can use hexadecimal colors, RGBA values, or named colors to customize the border appearance of your text objects.

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

372 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements