公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

我们可以通过创建fabric.Polygon的实例来创建一个Polygon对象。多边形对象的特征可以是由一组连接的直线段组成的任何闭合形状。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了添加收缩和展开动画,我们可以使用 scaleX 和 scaleY 属性与 animate 方法结合使用。

语法

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

登录后复制

参数

    property – 该属性接受一个String或Object值来确定我们想要哪些属性制作动画。

    value – 此属性接受 Number 或 Object 值,用于确定要设置动画的值特性。

    选项键

      scaleX:此属性接受 Number 值。分配的值决定水平对象比例因子。它的默认值为 1。

      scaleY:此属性接受 Number 值。分配的值决定垂直对象比例因子。它的默认值为 1。

      示例1:为多边形添加收缩动画

      让我们看一个代码示例,看看如何使用 animate 方法添加收缩动画。我们向scaleX和scaleY属性传递一个值0.5,这使得多边形从水平和垂直方向都是原始大小的一半。

      <!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>
      

      登录后复制

      示例 2:向多边形添加展开动画

      在此示例中,我们将了解如何使用 animate 方法添加 expand 动画。由于我们向scaleX和scaleY属性传递的值为1.5,因此多边形对象将在水平和垂直方向上缩放1.5倍。

      <!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> 
      

      登录后复制

      结论

      在本教程中,我们使用两个简单的示例来演示如何使用 FabricJS 将收缩和展开动画添加到 Polygon 对象

      以上就是使用 FabricJS 向 Polygon 对象添加收缩和展开动画的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:动画 对象 展开 收缩 添加
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定