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.

Return Value

Returns an object containing x and y coordinates representing the pointer position relative to the polygon object's local coordinate system.

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>
Coordinates of the pointer relative to the object are: {x: 150, y: 100}

Example 2: Using the getLocalPointer Method with 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 y axis 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>
Coordinates of the pointer relative to the object are: {x: 125, y: 85}

Key Points

  • The getLocalPointer method returns coordinates relative to the object's local coordinate system, not the canvas

  • It works with various event listeners like "mousedown", "skewing", "moving", etc.

  • The method is particularly useful for implementing custom interaction behaviors on polygon objects

  • The returned coordinates help in precise positioning and manipulation of elements within the polygon

Conclusion

The getLocalPointer method is essential for finding cursor positions relative to polygon objects in FabricJS. It provides accurate local coordinates that can be used for custom interactions and precise object manipulation.

Updated on: 2026-03-15T23:19:00+05:30

922 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements