Home  >  Article  >  Web Front-end  >  How to set border opacity when moving rectangle using FabricJS?

How to set border opacity when moving rectangle using FabricJS?

PHPz
PHPzforward
2023-08-25 10:17:07701browse

How to set border opacity when moving rectangle using FabricJS?

In this tutorial we will set the border opacity of a rectangle when moving FabricJS. Rectangle is one of the various shapes provided by FabricJS. for To create a rectangle, we have to create an instance of the Fabric.Rect class and add it to canvas.

We can use the borderOpacityWhenMoving property to change the opacity of a rectangle when moving it within the canvas.

Syntax

new fabric.Rect({ borderOpacityWhenMoving: Number }: Object)

Parameters

  • Options (optional) - This parameter is an object which is our rectangle Provides additional customization. Using this parameter, you can change properties such as color, cursor, stroke width, and many other properties associated with the object for which borderOpacityWhenMoving is an attribute.

  • ul>

    Option Key

    • borderOpacityWhenMoving - This property accepts a number that specifies the level of opacity we Want the border to stay the same when moving the rectangle. It allows us to control the opacity of the border when moving the rectangular object. The default value is 0.4.

    Example 1

    Showing the default behavior of the borderOpacityWhenMoving property

    Let’s look at a code example , which shows the default behavior of the boderOpacityWhenMoving property . When we select the rectangular object and move it Around the canvas, the selection border's opacity changes from 1 (fully opaque) to 0.4 This 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>Displaying the default behaviour of borderOpacityWhenMoving property</h2>
       <p>Move the rectangle to see the default behaviour of <b>borderOpacityWhenMoving</b></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 rectangle object
          var rect = new fabric.Rect({
             left: 125,
             top: 90,
             width: 170,
             height: 70,
             fill: "#cf1020",
             padding: 9,
             borderColor: "black",
          });
    
          // Add it to the canvas
          canvas.add(rect);
       </script>
    </body>
    </html>

    Example 2

    Passing borderOpacityWhenMoving as a key

    Let’s look at a code example that assigns a value to the borderOpacityWhenMoving property. In this example, we specify the value as 0. It tells us that when we move the rectangle, the border opacity will change to 0 and become invisible.

    <!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 borderOpacityWhenMoving as key</h2>
       <p>Move the rectangle to see the change in value of <b>borderOpacityWhenMoving</b></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 rectangle object
          var rect = new fabric.Rect({
             left: 125,
             top: 90,
             width: 170,
             height: 70,
             fill: "#cf1020",
             padding: 9,
             borderColor: "black",
             borderOpacityWhenMoving: 0,
          });
         
           // Add it to the canvas
          canvas.add(rect);
       </script>
    </body>
    </html>

The above is the detailed content of How to set border opacity when moving rectangle using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete