How to disable a specific control point of Image object using FabricJS?


In this tutorial, we are going to learn how to disable a specific control point of an Image object using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to disable a specific control point of an Image object, we use the setControlVisible method.

Syntax

setControlVisible(controlKey: String, visible: Boolean): fabric.Object

Parameters

  • controlKey − This parameter accepts a String value which specifies the key of control. The possible values are −

    • ‘tl’ − This property accepts a Boolean value which enables or disables the top-left control.

    • ‘tr’ − This property accepts a Boolean value which enables or disables the top-right control.

    • ‘br’ − This property accepts a Boolean value which enables or disables the bottom-right control.

    • ‘bl’ − This property accepts a Boolean value which enables or disables the bottom-left control.

    • ‘ml’ − This property accepts a Boolean value which enables or disables the middle-left control.

    • ‘mt’ − This property accepts a Boolean value which enables or disables the middle-top control.

    • ‘mr’ − This property accepts a Boolean value which enables or disables the middle-right control.

    • ‘mb’ − This property accepts a Boolean value which enables or disables the middle-bottom control.

    • ‘mtr’ − This property accepts a Boolean value which enables or disables the middle-top-rotate control.

  • visible − This parameter accepts a Boolean value which when ‘true’ sets the specified control visible and invisible in case of ‘false’.

Using setControlVisible method

Example

Let’s see a code example to see the output when the setControlVisible method is used. The setControlVisible method sets the visibility of the specified control. In this case, since we have passed the ‘br’ control to a false value, the bottom-right control will no longer 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>Using setControlVisible method</h2> <p> You can select the image object to see that the bottom-right control is not visible </p> <canvas id="canvas"></canvas> <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Add it to the canvas canvas.add(image); // Using setControlVisible method image.setControlVisible("br", false); </script> </body> </html>

Using setControlVisible method to disable the middle-top-rotate control

Example

In this example, we use the setControlVisible method to disable the ‘mtr’ control, which is also known as the middle-top-rotate control.

<!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> Using setControlVisible method to disable the middle-top-rotate control </h2> <p> You can select the Image object to see that the middle-top-rotate control is not visible </p> <canvas id="canvas"></canvas> <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Add it to the canvas canvas.add(image); // Using setControlVisible method image.setControlVisible("mtr", false); </script> </body> </html>

Updated on: 27-Oct-2022

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements