Home > Article > Web Front-end > How to make a polygon object react to mouse events using FabricJS?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized as 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 such as angle, opacity, etc. We use the mouseup and mousedown events to demonstrate how polygon objects react to user-triggered mouse events.
polygon.on(“mouseup”, callbackFunction); polygon.on(“mousedown”, callbackFunction);
Let's look at a code example to see how a polygon object reacts when the mouseup event is fired. When the user releases the left mouse button, the mouseup event occurs. Here, once the mouseup event is triggered, the stroke width will become 33.
<!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>Displaying how the object reacts to the mouseup event</h2> <p> You can select the object and release the left mouse button to see that the stroke width has changed </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: 0, y: 0 }, { x: 0, y: 50 }, { x: 50, y: 50 }, { x: 50, y: 0 }, ], { left: 100, top: 30, fill: "red", stroke: "blue", strokeWidth: 2, objectCaching: false, } ); // Adding it to the canvas canvas.add(polygon); // Using the mouseup event polygon.on("mouseup", () => { polygon.set("strokeWidth", 33); canvas.renderAll(); }); </script> </body> </html>
Let's look at a code example to see how a polygon object reacts when the mousedown event is fired. When the user presses the button, the mousedown event occurs. Here we can see that the object responds to the mousedown event by changing its stroke width from 33 to 2.
<!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>Displaying how the object reacts to the mousedown event</h2> <p> You can press the left mouse button to trigger the mousedown event to see that the stroke width has changed </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: 0, y: 0 }, { x: 0, y: 50 }, { x: 50, y: 50 }, { x: 50, y: 0 }, ], { left: 100, top: 30, fill: "red", stroke: "blue", strokeWidth: 33, objectCaching: false, } ); // Adding it to the canvas canvas.add(polygon); // Using the mousedown event polygon.on("mousedown", () => { polygon.set("strokeWidth", 2); canvas.renderAll(); }); </script> </body> </html>
In this tutorial, we use two simple examples to demonstrate how to make a polygon object react to mouse events using FabricJS.
The above is the detailed content of How to make a polygon object react to mouse events using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!