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

FabricJSJavascriptHTML5 Canvas

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 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

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>
raja
Updated on 25-May-2022 13:43:56

Advertisements