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

In this tutorial, we are going to set the border opacity of a Textbox while moving using FabricJS. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. We can change the opacity of the border of a textbox while moving it around in the canvas by using the borderOpacityWhenMoving property.

Syntax

new fabric.Textbox(text: String, { borderOpacityWhenMoving: Number }: Object)

Parameters

  • text ? This parameter accepts a String which is the text string that we want to display inside our textbox.

  • options (optional) ? This parameter is an Object which provides additional customizations to our textbox. 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 textbox around. It allows us to control the opacity of the borders while moving a textbox object. The default value is 0.4.

Example 1: Default Behavior

Displaying the default behaviour of borderOpacityWhenMoving property

Let's see a code example that shows the default behaviour of borderOpacityWhenMoving property. When we select our textbox 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 textbox 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 textbox object
        var textbox = new fabric.Textbox("Small steps motivate. Big steps overwhelm.", {
            backgroundColor: "#ffe5b4",
            width: 400,
            top: 70,
            left: 110,
            borderColor: "red",
        });
        
        // Add it to the canvas
        canvas.add(textbox);
    </script>
</body>
</html>

Example 2: Custom Border Opacity

Passing borderOpacityWhenMoving as key

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 textbox 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 textbox 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 textbox object
        var textbox = new fabric.Textbox("Small steps motivate. Big steps overwhelm.", {
            backgroundColor: "#ffe5b4",
            width: 400,
            top: 70,
            left: 110,
            borderColor: "red",
            borderOpacityWhenMoving: 0,
        });
        
        // Add it to the canvas
        canvas.add(textbox);
    </script>
</body>
</html>

Key Points

  • The default value of borderOpacityWhenMoving is 0.4, making borders slightly transparent during movement.

  • Values range from 0 (completely transparent) to 1 (fully opaque).

  • This property only affects border visibility during the drag operation, not when the object is selected but stationary.

  • The property helps provide visual feedback during object manipulation without completely obscuring the content underneath.

Conclusion

The borderOpacityWhenMoving property in FabricJS allows you to control the visual feedback when dragging textbox objects. By adjusting this value, you can create a more polished user experience that suits your application's design requirements.

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

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements