Home > Web Front-end > JS Tutorial > Add shrink and expand animations to Polygon objects using FabricJS

Add shrink and expand animations to Polygon objects using FabricJS

WBOY
Release: 2023-09-23 22:29:02
forward
1337 people have browsed it

使用 FabricJS 向 Polygon 对象添加收缩和展开动画

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. To add shrink and expand animations we can use the scaleX and scaleY properties in conjunction with the animate method.

grammar

animate(property: String | Object, value: Number | Object):
fabric.Object | fabric.AnimationContext |
Array.<fabric.AnimationContext>
Copy after login

parameter

  • property - This property accepts a String or Object value to determine which properties we want to animate.

  • value - This property accepts a Number or Object value that determines the value property to animate.

Option key

  • scaleX: This property accepts a Number value. The assigned value determines the horizontal object scale factor. Its default value is 1.

  • scaleY: This property accepts a Number value. The assigned value determines the vertical object scale factor. Its default value is 1.

Example 1: Add shrink animation to polygons

Let's look at a code example to see how to add a shrink animation using the animate method. We pass a value of 0.5 to the scaleX and scaleY properties, which makes the polygon half its original size both horizontally and vertically.

<!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>Adding shrink animation to the polygon</h2>
   <p>You can see that shrink animation has been added to the polygon</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 object
      var polygon = new fabric.Polygon(
         [
            { x: 60, y: 0 },
            { x: 60, y: 60 },
            { x: 0, y: 60 },
            { x: 0, y: 0 },
         ],
         {
            fill: "#ffe4e1",
            stroke: "green",
            strokeWidth: 5,
            top: 90,
            left: 100,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the animate method and passing scaleX property
      polygon.animate("scaleX", "0.5", {
         onChange: canvas.renderAll.bind(canvas),
         easing: fabric.util.ease.easeInCubic,
         duration: 1000,
      });
      
      // Using the animate method and passing scaleY property
      polygon.animate("scaleY", "0.5", {
         onChange: canvas.renderAll.bind(canvas),
         easing: fabric.util.ease.easeInCubic,
         duration: 1000,
      });
   </script>
 </body>
</html>
Copy after login

Example 2: Adding an unfold animation to a polygon

In this example we will see how to add expand animation using the animate method. Since we passed a value of 1.5 to the scaleX and scaleY properties, the polygon object will be scaled 1.5 times horizontally and vertically.

<!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>Adding expand animation to the polygon</h2>
   <p>You can see that expand animation has been added to the polygon</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 object
      var polygon = new fabric.Polygon(
         [
            { x: 60, y: 0 },
            { x: 60, y: 60 },
            { x: 0, y: 60 },
            { x: 0, y: 0 },
         ],
         {
            fill: "#ffe4e1",
            stroke: "green",
            strokeWidth: 5,
            top: 90,
            left: 100,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using the animate method and passing scaleX property
      polygon.animate("scaleX", "1.5", {
         onChange: canvas.renderAll.bind(canvas),
         easing: fabric.util.ease.easeInCubic,
         duration: 1000,
      });
      
      // Using the animate method and passing scaleY property
      polygon.animate("scaleY", "1.5", {
         onChange: canvas.renderAll.bind(canvas),
         easing: fabric.util.ease.easeInCubic,
         duration: 1000,
      });
   </script>
</body>
</html> 
Copy after login

in conclusion

In this tutorial, we use two simple examples to demonstrate how to add shrink and expand animations to Polygon objects using FabricJS

The above is the detailed content of Add shrink and expand animations to Polygon objects 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