How to disable the selectability of Circle using FabricJS?

FabricJSJavascriptHTML5 Canvas

In this tutorial, we are going to learn how to disable selectability of a Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will have to create an instance of fabric.Circle 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.Circle({ selectable: Boolean }: Object)

Parameters

  • options (optional) − This parameter is an Object which provides additional customizations to our circle. 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 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 an 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>Disabling the selectability of circle using FabricJs</h2>
      <p>Here you can select the object (circle) and move it around freely. This is the default behavior. Here we have not used the <b>selectable</b> property but by default, it is set to True. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 50,
            radius: 50,
            fill: "#85bb65"
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </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 circle 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>Disabling the selectability of circle using FabricJs</h2>
      <p>Now you can no longer select the circle because we have set <b>selectable</b> as False. </p>
      <canvas id="canvas"></canvas>
   
      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 50,
            radius: 50,
            fill: "#85bb65",
            selectable: false
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
raja
Updated on 25-May-2022 08:48:23

Advertisements