【问题标题】:How can I animate the position of an svg polygon point using velocityjs?如何使用velocityjs为svg多边形点的位置设置动画?
【发布时间】:2018-06-08 08:25:33
【问题描述】:

我有一个看起来像这样的 svg:

<svg id="baseSplashGradient" width="720px" height="1024px" viewBox="0 0 720 1024" version="1.1"
         xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink">
        <defs>
            <linearGradient x1="41.1044918%" y1="0%" x2="50%" y2="101.663162%" id="linearGradient-1">
                <stop stop-color="#56CCF2" offset="0%"></stop>
                <stop stop-color="#2F80ED" offset="100%"></stop>
            </linearGradient>
        </defs>
    <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <polygon id="left-background" fill="url(#linearGradient-1)"
                     points="0 0 439.837891 0 720 1024 0 1024"></polygon>
    </g>
</svg>

它形成了一种三角形的形状。我想弄清楚如何为点数组设置动画:

points="0 0 439.837891 0 720 1024 0 1024

看起来像这样:

points="0 0 720 0 720 1024 0 1024"

本质上,使形状成为矩形。使用translateX 之类的东西,我不确定如何与实际的points 交谈。

我可以移动整个 svg:

var svg = $('#baseSplashGradient'),
    polygon = svg.find('polygon'),
    points = polygon.attr('points'),
    pointsArray = points.split(' ');

if (polygon.data('animating') === true) {
    //
} else {
    polygon.velocity({
        translateX: '200px'
    }, {
        duration: 500
    });
}

但这不是我想要完成的。任何建议将不胜感激!

【问题讨论】:

  • 为什么不直接使用 SMIL?
  • 通读帖子,我以为我读过 &lt;animate&gt; 正在被弃用。
  • Chrome 弃用了它,然后他们改变了主意。所以目前不是

标签: animation svg css-animations velocity.js


【解决方案1】:

目前您需要使用进度回调,并手动更改属性。当 Velocity v2 发布时,您应该能够直接将其作为属性进行动画处理(免责声明:我是 v2 的主要开发人员)。

polygon.velocity({
   tween: ["0 0 439.837891 0 720 1024 0 1024", "0 0 720 0 720 1024 0 1024"]
}, {
   duration: 500,
   progress: function(elements, complete, remaining, start, tweenValue) {
      elements[0].setAttribute("points", tweenValue);
   }
});

当 Velocity v2(目前是开发分支)发布时,您将能够更轻松地做到这一点:

polygon.velocity({
   points: ["0 0 439.837891 0 720 1024 0 1024", "0 0 720 0 720 1024 0 1024"]
}, {
   duration: 500
});

【讨论】:

  • 太棒了!动画点正是我正在寻找的。非常感谢!
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
相关资源
最近更新 更多