- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to find the current cursor position on the clicked Polyline object using FabricJS?
- FabricJS – Finding the Transform Matrix that Represents the Current Transformations for a Polygon Object?
- FabricJS - Finding the dimensions of a Polygon object converted to an HTMLCanvasElement?
- FabricJS – Finding the data url after converting a Polygon object to an HTMLCanvasElement?
- FabricJS – Finding the drawing context of a Polygon object converted to an HTMLCanvasElement?
- FabricJS – Implementing object duplication programmatically on a Polygon?
- How to get the current colour of the character at cursor in IText using FabricJS?
- How to serialize a Polygon object using FabricJS?
- FabricJS – How to center a Line object vertically on the current viewport of canvas?
- FabricJS – How to center a Line object on the current viewport of a canvas?
- FabricJS – Removing the object transformations of a Polygon converted to an HTMLCanvasElement?
- FabricJS – Setting the cropping offset of an HTMLCanvasElement of a Polygon object
- How to find the rotation matrix of a Polygon object using FabricJS?
- How to find the translation matrix of a Polygon object using FabricJS?
- How to return the dataless object representation of a Polygon using FabricJS?
