Home  >  Article  >  Web Front-end  >  How to make a polygon object react to tilt events using FabricJS?

How to make a polygon object react to tilt events using FabricJS?

WBOY
WBOYforward
2023-09-16 09:17:141026browse

如何使用 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 skewing event to demonstrate how polygonal objects react to the user when manipulated. By controlling the tilt.

grammar

polygon.on("skewing", callbackFunction);

Example 1: Show how an object responds to tilt events

Let's look at a code example to see how a polygon object reacts when using the Tilt event. You can tilt an object horizontally and vertically by pressing the shift key and then dragging the middle control horizontally or vertically. When the object is tilted, the tilt event fires continuously.

<!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 skewing event</h2>
   <p>
      You can keep skewing the object while the console from dev tools is open to see the logged output
   </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: "red",
            stroke: "blue",
            strokeWidth: 2,
            objectCaching: false,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the skewing event
      polygon.on("skewing", () => {
         canvas.renderAll();
         console.log("The polygon object is being skewed");
      });
   </script>
</body>
</html>

Example 2: Changing fill color when tilt occurs

Let's look at a code example to see how to change the fill color when a tilt event occurs. We used the set method, which is a setter that allows us to specify the property we want to change. Here, whenever we tilt the polygon, the fill color changes to "green".

<!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>Changing the fill colour when skew happens</h2>
   <p>
      You can see that the fill colour changes when the polygon is skewed
   </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: "red",
            stroke: "blue",
            strokeWidth: 2,
            objectCaching: false, 
            top: 50,
            left: 30,
            scaleX: 0.5,
            scaleY: 0.5
         }
      );

      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the skewing event
      polygon.on("skewing", () => {
         polygon.set("fill", "green")
         canvas.renderAll();
      });
   </script>
</body>
</html> 

in conclusion

In this tutorial, we use two simple examples to demonstrate how to make a polygon object react to tilt events using FabricJS.

The above is the detailed content of How to make a polygon object react to tilt events using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete