简体   繁体   English

使用Snap.svg的SVG线动画

[英]SVG line animation using Snap.svg

I am literally a crawling baby in snap SVG and given that the amount of resources available online is not sufficient for a Rookie like me, I thought I would ask here. 我真的是一个抓住SVG的爬行宝宝,并且考虑到像我这样的新秀在线可用的资源量不足,我想我会问这里。 I want to learn how to do this animation that I have generated in SVG using CSS3 animations however I recently found out that, FF, IE have some issues with CSS3 animations and CSS3 transforms in SVG, so I worked only with webkit and i decided to use Snap SVG for the others, I think you know what I mean. 我想学习如何使用CSS3动画在SVG中生成这个动画但是我最近发现,FF,IE在SVG中有CSS3动画和CSS3转换的一些问题,所以我只使用webkit而且我决定使用Snap SVG为其他人,我想你知道我的意思。 Any how, this is a glimpse of the animation: 不管如何,这是动画的一瞥:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
 width="711.1px" height="717.1px" viewBox="0 0 711.1 717.1" enable-background="new 0 0 711.1 717.1" xml:space="preserve">


<circle class="circle" fill="none" stroke="#FD4F00" stroke-width="66" stroke-miterlimit="10" cx="355.55" cy="358.55" r="285.956"/>

</svg>

This is the CSS3 animation: 这是CSS3动画:

.circle {
stroke-dasharray: 2000;
stroke-dashoffset: 2000;
-webkit-animation: dash 5s linear alternate infinite;
-webkit-transform-origin: 50% 50% ;
-webkit-transform: rotate(-90deg) ;
}
@-webkit-keyframes dash {
  from {
    stroke-dashoffset: 2000;
  }
  to {
    stroke-dashoffset: 0;
  }
}

Here is the Fiddle, to see the Full animation in action so you can see what I am talking about here. 这是小提琴,看到动画中的完整动画,这样你就可以看到我在这里谈论的内容。

Thanks for your help, I apologize as I am really new in Snap SVG and I really dont know how to achieve that animation so I am hoping to learn from this mini project. 感谢您的帮助,我道歉,因为我是Snap SVG的新手,我真的不知道如何实现这个动画,所以我希望从这个迷你项目中学习。

EDIT: Alright so I have completed the animation Here is a Fiddle . 编辑:好吧,所以我完成了动画这里是一个小提琴 Now the only problem is this,that how to adjust the repeatCount using snap.svg ? 现在唯一的问题是,如何使用snap.svg调整repeatCount? Could you please tell me how to adjust the repeatCount in Snap.svg ? 你能告诉我如何在Snap.svg中调整repeatCount吗? Thanks for your help ! 谢谢你的帮助 !

DEMO DEMO

Update 更新

I really like your improvements, pure css. 我真的很喜欢你的改进,纯粹的CSS。
Now since I'm not sure what you need help with I remade my answer but with your new fiddle. 现在,因为我不确定你需要什么帮助,所以我用你的新小提琴重新制作了我的答案。

so the animation run a set amount of times: 所以动画运行一定次数:

  -webkit-animation: dash 5s linear alternate 3 forwards;

Note 注意
Consider adding all the prefixes so you have both -wekkit-animation and animation and so you have support for none webkit browsers. 考虑添加所有前缀,以便同时拥有-wekkit-animationanimation ,因此您-wekkit-animation支持任何webkit浏览器。

Cross Browser 跨浏览器
Animation: 动画:

-webkit-animation: 
-moz-animation:  
-o-animation:

Keyframe: 关键帧:

@-webkit-keyframes NAME {
    //code
}
@-moz-keyframes NAME {
    //code
}
@-o-keyframes NAME {
    //code
}
@keyframes NAME-YOUR-ANIMATION {
    //code
}

 .circle { stroke-dasharray: 2000; stroke-dashoffset: 2000; -webkit-animation: dash 5s linear alternate 3 forwards; -webkit-transform-origin: 50% 50% ; -webkit-transform: rotate(-90deg) ; } .circle1 { stroke-dasharray: 2000; stroke-dashoffset: 2000; -webkit-animation: dash 5s linear alternate 3 forwards; -webkit-transform-origin: 50% 50% ; -webkit-transform: rotate(-90deg) ; } .circle2 { stroke-dasharray: 2000; stroke-dashoffset: 2000; -webkit-animation: dash 5s linear alternate 3 forwards; -webkit-transform-origin: 50% 50% ; -webkit-transform: rotate(-90deg) ; } .circle3 { stroke-dasharray: 2000; stroke-dashoffset: 2000; -webkit-animation: dash 5s linear alternate 3 forwards; -webkit-transform-origin: 50% 50% ; -webkit-transform: rotate(-90deg) ; } @-webkit-keyframes dash { from { stroke-dashoffset: 2000; } to { stroke-dashoffset: 0; } } 
 <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="400px" height="400px" viewBox="0 0 711.1 717.1" enable-background="new 0 0 711.1 717.1" xml:space="preserve"> <circle class="circle" fill="none" stroke="#FD4F00" stroke-width="66" stroke-miterlimit="10" cx="355.55" cy="358.55" r="285.956"/> <circle class="circle1" fill="none" stroke="#FD4F00" stroke-width="3.0819" stroke-miterlimit="10" cx="355.55" cy="358.55" r="233.497"/> <circle class="circle2" fill="none" stroke="#FD4F00" stroke-width="2.8006" stroke-miterlimit="10" cx="355.55" cy="358.55" r="212.186"/> <circle class="circle3" fill="none" stroke="#FD4F00" stroke-width="2.5572" stroke-miterlimit="10" cx="355.55" cy="358.55" r="193.744"/> </svg> 

I know I'm pretty late here, but I Wanted to answer the last question in your edit. 我知道我已经很晚了,但我想在你的编辑中回答最后一个问题。

http://jsfiddle.net/oc2aus6w/9/ http://jsfiddle.net/oc2aus6w/9/

The general idea is to call the original function as the callback for the end of the animation. 一般的想法是将原始函数称为动画结束的回调。

code below for those who cannot see the fiddle: 对于那些看不到小提琴的人来说,代码如下:

var s = Snap.select('#circle')
s1 = s.select('circle:nth-child(1)');
s2 = s.select('circle:nth-child(2)');
s3 = s.select('circle:nth-child(3)');
s4 = s.select('circle:nth-child(4)');

s1.attr({
      transform: 'r-90'
    });
s2.attr({
  transform: 'r-90'
});
s3.attr({
  transform: 'r-90'
});
s4.attr({
  transform: 'r-90'
});
function circleAnim() {  
    Snap.animate(2000,4000,function(value){
        s1.attr({
            'stroke-dasharray':value
        });
                s2.attr({
            'stroke-dasharray':value
        });
        s3.attr({
            'stroke-dasharray':value
        });
        s4.attr({
            'stroke-dasharray':value
        });
    },3000, null, function(){
        Snap.animate(4000,2000,function(value){
            s1.attr({
                'stroke-dasharray':value
            });
            s2.attr({
                'stroke-dasharray':value
            });
            s3.attr({
                'stroke-dasharray':value
            });
            s4.attr({
                'stroke-dasharray':value
            });
        }, 3000, null, circleAnim);    
    });
}

circleAnim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM