How to create a Triangle with background colour using FabricJS?


In this tutorial, we are going to create a Triangle with background 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.

The backgroundColor property allows us to assign a colour to our object's background. It is the colour of the container where the Triangle lives and is rectangular in shape for the triangle.

Syntax

new fabric.Triangle({ backgroundColor: 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 Triangle of which backgroundColor is a property.

Options Keys

  • backgroundColor − This property accepts a value of type String which will determine the background colour.

Example 1

Passing backgroundColor property as key with a hexadecimal value

Let's see a code example to assign a background colour to our Triangle object using a hexadecimal value of colour. In this example, we have used the hex colour code #ff0000 which represents the colour 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 backgroundColor property as key with a hexadecimal value</h2>
   <p>You can see the new background colour on the triangle</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: 55,
         top: 60,
         width: 100,
         height: 70,
         fill: "blue",
         stroke: "#2a52be",
         backgroundColor: "#ff0000",
      });

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

Example 2

Passing backgroundColor property as key with a rgba value

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.8) which is the colour pink with 0.8 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 backgroundColor property as key with a rgba value</h2>
   <p>You can see the new background colour on the triangle</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: 55,
         top: 60,
         width: 100,
         height: 70,
         fill: "#deb887",
         backgroundColor: "rgba(255,20,147,0.8)",
      });

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

Updated on: 24-Jun-2022

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements