How to disable the selectability of Triangle using FabricJS?


In this tutorial, we are going to learn how to disable selectability of a Triangle using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of fabric.Triangle class and add it to the canvas.

In order to modify an object, we have to select it in FabricJS. However, we can disable this behaviour by using the selectable property.

Syntax

new fabric.Triangle{ selectable: Boolean }: Object)

Parameters

  • Options (optional) − This parameter is an Object which provides additional customizations to our triangle. 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 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 triangle around the canvas or scaling it to prove that 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 triangle object
      var triangle = new fabric.Triangle({
         left: 105,
         top: 70,
         width: 90,
         height: 80,
         fill: "#746cc0",
         stroke: "#967bb6",
         strokeWidth: 5,
      });

      // Add it to the canvas
      canvas.add(triangle);
   </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 triangle 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 triangle 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 triangle object
      var triangle = new fabric.Triangle({
         left: 105,
         top: 70,
         width: 90,
         height: 80,
         fill: "#746cc0",
         stroke: "#967bb6",
         strokeWidth: 5,
         selectable: false,
      });

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

Updated on: 24-Jun-2022

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements