How to disable the selectability of Textbox using FabricJS?


In this tutorial, we are going to learn how to disable the selectability of a Textbox 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. In order to modify an object, we have to select it in FabricJS. However, we can change this behaviour by using the selectable property.

Syntax

new fabric.Textbox(text: String, { selectable: Boolean }: 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 colour, cursor, stroke width and a lot of other properties can be changed related to the object of which selectable is a property.

Options Keys

  • selectable − This property accepts a Boolean value. When a "false" value is assigned to it, the object cannot be selected for modifications. Its default value is true.

Example 1

Default behaviour or when selectable property is set to "true"

Let’s see a code example to understand how the object behaves when by default the selectable property is set to True. We are allowed to select an object, move it around the canvas and make modifications to it when the selectable property is set to True.

<!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>Default behaviour or when selectable property is set to ‘true’</h2>
   <p>You can try moving the textbox around the canvas or scaling it to provethat it's selectable.</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("Every noble work is at first impossible.", {
         width: 400,
         left: 110,
         top: 70,
         fill: "orange",
         strokeWidth: 2,
         stroke: "green",
         textAlign: "center",
      });

      // Add it to the canvas
      canvas.add(textbox);
   </script>
</body>
</html>

Example 2

Passing selectable property as key

In this example, we are assigning a false value to the selectable property. This means that we can no longer select the textbox object for modifications.

<!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 selectable property as key</h2>
   <p>You can see that the textbox is no longer selectable</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("Every noble work is at first impossible.", {
         width: 400,
         left: 110,
         top: 70,
         fill: "orange",
         strokeWidth: 2,
         stroke: "green",
         textAlign: "center",
         selectable: false,
      });

      // Add it to the canvas
      canvas.add(textbox);
   </script>
</body>
</html>

Updated on: 29-Jul-2022

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements