FabricJS – Finding the current cursor position on the clicked Polygon object?



We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of 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 Polygon 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 polygon object by using the getLocalPointer method. As soon as we click on the polygon a mouse down event is fired which enables us to retrieve the current clicked left and top position of the polygon 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 polygon 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 polygon instance
      var polygon = new fabric.Polygon(
         [
            { x: 500, y: 20 },
            { x: 550, y: 60 },
            { x: 550, y: 200 },
            { x: 350, y: 200 },
            { x: 350, y: 60 },
            { x: 500, y: 20 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
         }
      );
      
      // Add it to the canvas
      canvas.add(polygon);
      
      // Using getLocalPointer method
      polygon.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 polygon instance
      var polygon = new fabric.Polygon( 
         [
            { x: 500, y: 20 },
            { x: 550, y: 60 },
            { x: 550, y: 200 },
            { x: 350, y: 200 },
            { x: 350, y: 60 },
            { x: 500, y: 20 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            lockMovementX: true,
            lockMovementY: true,
         }
      );
      
      // Add it to the canvas
      canvas.add(polygon);
      
      // Using getLocalPointer method
      polygon.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> 

Conclusion

In this tutorial, we used two simple examples to demonstrate how you can find the current cursor position on the clicked Polygon object using FabricJS.


Advertisements