FabricJS – How to set the border opacity of a Line while moving it?


In this tutorial, we are going to learn about how to set the border opacity of Line while moving using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to change the opacity of the border of a line object while moving it around in the canvas we use the borderOpacityWhenMoving property.

Syntax

new fabric.Line(points: Array, { borderOpacityWhenMoving: Number }: Object)

Parameters

  • points βˆ’ This parameter accepts an Array of points which determines the (x1, y1) and (x2, y2) values, those being the x- and y-axis coordinates of the start and end of the line respectively.

  • options (optional) βˆ’ This parameter is an Object which provides additional customizations to our line. Using this parameter origin, 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 object around. Here 1 means fully opaque and 0 means transparent. The default value is 0.4.

Displaying the default behaviour of borderOpacityWhenMoving property

Example

Let’s see a code example that shows the default behaviour of boderOpacityWhenMoving property. When we select our line 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>Displaying the default behaviour of borderOpacityWhenMoving property</h2> <p> You can select the line object and drag it around to see that the border opacity changes from being fully opaque(1) to being translucent(0.4) </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Passing borderOpacityWhenMoving as key

Example

Let’s see a code 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 line 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>Passing borderOpacityWhenMoving as key</h2> <p> You can select the line object and drag it around to see that the borders are no longer visible when being moved </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, borderOpacityWhenMoving: 0, }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Updated on: 25-Oct-2022

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements