How to set the fill colour of a Triangle using FabricJS?

In this tutorial, we are going to learn how we can change the appearance of a Triangle object by changing its fill colour using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of fabric.Triangle class and add it to the canvas.

We can change the fill colour by using the fill property which allows us to specify the colour of the object's fill.

Syntax

new fabric.Triangle({ fill: String }: Object)

Parameters

  • Options (optional) ? This parameter is an Object which provides additional customizations to our triangle. 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 fill is a property.

Options Keys

  • fill ? This property accepts a String which allows us to change an object's fill colour. Its default value is rgb(0,0,0) which is the colour black.

Example 1: Default Fill Colour

Let's see a code example that shows us the default fill colour of a triangle object in FabricJS. If we completely skip the fill property while creating a triangle object, it will be rendered with the fill colour as black.

<!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 fill colour of a triangle object</h2>
   <p>You can see that the default fill colour of triangle is black</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 triangle object
      var triangle = new fabric.Triangle({
         left: 105,
         top: 60,
         width: 100,
         height: 70,
         stroke: "#507d2a",
         strokeWidth: 5,
      });

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

Example 2: Setting Custom Fill Colour

We can assign the fill property any colour name, hex value, or RGBA value. In this example, we have assigned it a value of "blue" which changes the fill colour accordingly.

<!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>Setting custom fill colour</h2>
   <p>You can see that the fill colour of triangle is blue</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 triangle object with blue fill
      var triangle = new fabric.Triangle({
         left: 105,
         top: 60,
         width: 100,
         height: 70,
         stroke: "#507d2a",
         strokeWidth: 5,
         fill: "blue",
      });

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

Common Fill Color Options

The fill property accepts various color formats:

  • Color names: "red", "blue", "green", "orange"
  • Hex values: "#FF0000", "#0066CC", "#33AA33"
  • RGB values: "rgb(255, 0, 0)", "rgb(0, 102, 204)"
  • RGBA values: "rgba(255, 0, 0, 0.7)" for transparency

Conclusion

The fill property in FabricJS allows you to easily customize triangle colors using various color formats. By default, triangles are rendered with black fill, but you can specify any valid CSS color value to achieve the desired appearance.

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

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements