动画多边形指向snap.svg

时间:2016-01-25 11:15:30

标签: javascript svg snap.svg

这是我第一次尝试使用snap.svg为​​多边形点设置动画,我感觉很糟糕我在那里做错了。

这是我的代码:

var fdr = Snap('#fdright');

var time1_stp0 = [363.617,262.895, 363.562,367.4, 273.145,315.191, 273.145,315.191];
var time1_stp1 = [363.617,262.895, 363.562,367.4, 273.145,315.191, 273.145,210.688];

var timeline1 = fdr.polygon(time1_stp0).attr({fill:'red',opacity:'0.5'});

timeline1_anim = function(){
timeline1.animate({"points":time1_stp1},3000,mina.linear);
}

timeline1_anim();

加载页面后,我的多边形消失了(我猜是因为我的函数在创建多边形后立即被调用)。我检查了html,我的多边形仍在那里,但这是我得到的:

<polygon fill="#ff0000" style="opacity: 0.5;" points="363.617"></polygon>

我没有得到可能存在的问题,所以如果有人得到答案,我会很高兴听到它。

编辑:我尝试添加“toString()”,但它仍无效:

timeline1_anim = function(){
timeline1.animate({"points":time1_stp1.toString()},3000,mina.linear);
}

1 个答案:

答案 0 :(得分:2)

我认为Snaps多边形动画中存在一个错误..它列出的here已经提交了一个从那里链接的补丁。

但是,如果需要,可以通过设置数组值来轻松解决这个问题。

timeline1_anim = function(){
  Snap.animate(time1_stp0, time1_stp1, 
    function( val ){ 
      timeline1.attr({ points: val })
    }, 
  2000);  
}

jsfiddle

如果你做了很多,你可以写一个小插件来包含它......

Snap.plugin( function( Snap, Element, Paper, global ) {
  Element.prototype.polyAnimate = function( destPoints, duration, easing, callback ) {
    var poly = this;
    Snap.animate( this.attr('points'), destPoints,  
       function( val ){ poly.attr({ points: val }) }, duration, easing, callback)
    };
});

timeline1.polyAnimate( time1_stp1, 2000, mina.linear, function() { alert('finished')})

jsfiddle

相关问题