How to set the border opacity of a Circle while moving using FabricJS?

FabricJSJavascriptHTML5 Canvas

In this tutorial, we are going to set the border opacity of Circle while moving 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 opacity of circle while moving it around in the canvas by using the borderOpacityWhenMoving property.

Syntax

new fabric.Circle({ borderOpacityWhenMoving: Number }: 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 borderOpacityWhenMoving is a property.

Options Keys

  • borderOpacityWhenMoving − This property accepts a Number that specifies how opaque we want the borders to be while moving the circle around. It allows us to control the opacity of the borders while moving a circle object. The default value is 0.4.

Example 1

Displaying the default behaviour of borderOpacityWhenMoving property

Let's see an example that shows the default behaviour of boderOpacityWhenMoving  property. When we select our circle object and move it around the canvas, the selection border changes its opacity from 1(fully opaque) to 0.4 which makes it look a bit translucent.

<!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 border opacity of Circle while moving using FabricJS</h2>
      <p>Select the object and move it around. Notice that the opacity of the outline border reduces slightly while moving the object. This is the default behavior. Here we have not used the <b>boderOpacityWhenMoving</b> property.</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: "",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#966fd6",
         });

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

Example 2

Passing borderOpacityWhenMoving as key

Let's see an example to assign a value to the borderOpacityWhenMoving property. In this case we have assigned the value as 0. This tells us that when we move our circle around, the border opacity will change to 0 and will not be visible.

<!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>How to set the border opacity of Circle while moving using FabricJS?</h2>
      <p>Select the object and move it around. You will notice that the border opacity becomes 0 when moving the object. Here we have set <b>borderOpacityWhenMoving</b> to 0.</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: "",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#966fd6",
            borderOpacityWhenMoving: 0,
         });

         // 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:12:35

Advertisements