How to find the current cursor position on the clicked Polyline object using FabricJS?


We can create a Polyline object by creating an instance of fabric.Polyline. A polyline object can be characterised by a set of connected straight-line segments. 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 find the current cursor position on the clicked Polyline object, we use the getLocalPointer method.

Syntax

getLocalPointer( e, pointer ): Object

Parameters

  • e − This parameter accepts an Event which denotes the event to operate upon.

  • pointer (optional) − This parameter is an Object which denotes the pointer to operate upon. This parameter is optional.

Example 1: Using the getLocalPointer Method

Let’s see a code example of how we can find the coordinates of a pointer relative to the polyline object by using the getLocalPointer method. As soon as we click on the polyline a mousedown event is fired which enables us to retrieve the current clicked left and top position of the polyline instance.

<!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 the getLocalPointer method</h2>
   <p>You can click on the polyline object while the console from dev tools is opened to see that the logged output contains the x and y coordinates </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 polyline instance
   var polyline = new fabric.Polyline(
      [
         { x: 500, y: 20 },
         { x: 550, y: 60 },
         { x: 550, y: 200 },
         { x: 350, y: 200 },
         { x: 350, y: 60 },
      ],
      {
         fill: "green",
         stroke: "blue",
         strokeWidth: 20,
      }
   );
   
   // Add it to the canvas
   canvas.add(polyline);
   
   // Using getLocalPointer method
   polyline.on("mousedown", function (options) {
      var pointer = this.getLocalPointer(options.e);
      console.log("Coordinates of the pointer relative to the object are: ", pointer);
   });
   </script>
</body>
</html>

Example 2: Using the getLocalPointer Method and using a Different Event Listener

Let’s see a code example to understand how we can still retrieve the "x" and "y" coordinates of the current cursor position by using a different event listener. Here, we have passed the value as “skewing” which ensures that the event is fired while skewing the object in horizontal or vertical directions.

Skewing is feasible by pressing the shift key and then dragging along the horizontal or vertical direction. You can open the console and see that the event is fired while the object is being skewed from the controls.

<!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 the getLocalPointer method and using a different event listener </h2>
   <p>You can press the shift-key and drag the middle edge along the x or yaxis to skew the object while the console from dev tools is opened to see that the logged output contains the x and y coordinates </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 polyline instance
      var polyline = new fabric.Polyline(
         [
            { x: 500, y: 20 },
            {x: 550, y: 60 },
            { x: 550, y: 200 },
            { x: 350, y: 200 },
            { x: 350, y: 60 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            lockMovementX: true,
            lockMovementY: true,
         }
      );
      
      // Add it to the canvas
      canvas.add(polyline);
      
      // Using getLocalPointer method
      polyline.on("skewing", function (options) {
         var pointer = this.getLocalPointer(options.e);
         console.log(
            "Coordinates of the pointer relative to the object are: ",  pointer
         );
      });
   </script>
</body>
</html>

Updated on: 16-Feb-2023

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements