How to set the fill color of a Circle using FabricJS?

In this tutorial, we are going to learn how we can change the appearance of a Circle object by changing its fill colour using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will have to create an instance of fabric.Circle 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.Circle({ fill: String }: Object)

Parameters

  • options (optional) ? This parameter is an Object which provides additional customizations to our circle. 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 value 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 Color

Default fill colour of a circle object

Let's see a code that shows us the default fill colour of a circle object in FabricJS. If we completely skip the fill property while creating a circle object, it will be rendered 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>Setting the fill color of a Circle using FabricJS</h2>
      <p>Here we have not used the <b>fill</b> property, so by default, the circle is rendered black. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Example 2: Custom Fill Color

Passing the fill property as key

We can also assign the fill property any colour name. In this example, we have assigned it a value of "skyBlue" which thus 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 the fill colour of a Circle using FabricJS</h2>
      <p>Here we have used the <b>fill</b> property to render the body of the circle sky-blue. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "skyBlue",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Common Fill Color Options

FabricJS supports various color formats for the fill property:

Format Example Description
Color Names "red", "blue", "skyBlue" Predefined HTML color names
Hex Codes "#FF0000", "#00BFFF" Hexadecimal color values
RGB Values "rgb(255, 0, 0)" Red, Green, Blue values
RGBA Values "rgba(255, 0, 0, 0.5)" RGB with transparency

Conclusion

The fill property in FabricJS allows you to customize the interior color of circle objects using various color formats. By default, circles are rendered black, but you can easily change this by specifying any valid color value.

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

835 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements