Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2023-08-29 11:57:06
forward
907 people have browsed it

如何使用 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 scaling event to demonstrate how polygonal objects react to scaling.

grammar

polygon.on(“scaling”, callbackFunction);
Copy after login

Example 1: Show how an object responds to zoom events

Let's look at a code example to see how a polygon object reacts when the Scale event occurs. We can scale the object by dragging any corner control. Here, when the object is scaled, the scale event will be triggered 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 scaling event</h2>
   <p>
      You can scale the polygon object and open the console from dev tools 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: 0, y: 0 },
            { x: 0, y: 50 },
            { x: 50, y: 50 },
            { x: 50, y: 0 },
         ],
         {
            left: 100,
            top: 30,
            fill: "red",
            stroke: "blue",
            strokeWidth: 3,
            objectCaching: false,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the scaling event
      polygon.on("scaling", () => {
         canvas.renderAll();
         console.log("The object is being scaled");
      });
   </script>
</body>
</html>
Copy after login

Example 2: Changing stroke color when zooming

Let's look at a code example to see how to change the stroke color when a zoom event occurs. We use the set method to set the polygon's stroke color to "orange". So when we scale the object, its stroke color will change to orange.

<!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 stroke colour when scaling happens</h2>
   <p>
      You can see that the stroke colour changes when the polygon is scaled
   </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: 5,
            objectCaching: false,
            top: 50,
            left: 30,
            scaleX: 0.5,
            scaleY: 0.5
         }
      );

      // Adding it to the canvas
      canvas.add(polygon);

      // Using the scaling event
      polygon.on("scaling", () => {
         polygon.set("stroke", "orange")
         canvas.renderAll();
      });
   </script>
</body>
</html>
Copy after login

in conclusion

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

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

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!