【问题标题】:Animate polygon points snap.svg动画多边形点 snap.svg
【发布时间】:2016-01-25 20:14:43
【问题描述】:

这是我第一次尝试使用 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);
}

【问题讨论】:

    标签: javascript svg snap.svg


    【解决方案1】:

    我认为 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

    【讨论】:

    • 您的解决方案效果很好,非常适合我想做的事情,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-04-21
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多