简体   繁体   中英

How to animate SVG fill and path in a similar style to vivus.js

I am trying to animate an SVG with vivus.js but it doesn't seem to be doing anything.

Vivus seems to be able to read the SVG correctly, I can run the following code:

var test = new Vivus('welcome_message', {duration: 200});
console.table(test.map);

And get the following result in the console (this is how vivus recommends you debug it):

(index) el    startAt             duration            progress
0       path  0                   133.33333333333331  0.0075000000000000015
1       path  5.555555555555556   133.33333333333331  0
2       path  11.111111111111112  133.33333333333331  0
3       path  16.666666666666668  133.33333333333331  0
4       path  22.222222222222225  133.33333333333331  0
5       path  27.777777777777782  133.33333333333331  0
6       path  33.333333333333336  133.33333333333331  0
7       path  38.88888888888889   133.33333333333331  0
8       path  44.44444444444445   133.33333333333331  0
9       path  50.00000000000001   133.33333333333331  0
10      path  55.555555555555564  133.33333333333331  0
11      path  61.11111111111112   133.33333333333331  0
12      path  66.66666666666667   133.33333333333331  0

This says to me the vivus is working, but when the code runs, nothing happens.

Please see the following very simple jsfiddle example:

https://jsfiddle.net/zzgnkwtn/2/

All I can thinks is that perhaps there is something wrong with my SVGs but seeing as it is picking up on the paths correctly this doesn't seem right.

Here is a link to the vivus documentation incase anyone wants to have a look, I am stumped.

https://github.com/maxwellito/vivus#vivusjs

Thanks in advance for any help.

EDIT

(changed title from 'SVG animation with vivus.js not working' to 'How to animate SVG fill and path in a similar style to vivus.js')

I have realised that the end goal I am trying to achieve isn't actually the above question, this is because I am new to SVG animation and started on the wrong path.

What I am more looking to achieve is something like the following tutorial: https://medium.com/@gordonnl/stylised-line-animations-ded23320ffe5#.v6va1kc1w

The key difference and the main thing I am struggling is I want that exact type of animation but I want to animate the fill and the path, not just the path.

With help from @Alvin I have realised that some kind of clipPath or mask is required, I have tried to do this by following the above tutorial but this is the result I am getting: http://codepen.io/anon/pen/dMeQLB

This seems to animate some of the paths but it is definitely not doing what I want.

Any help from anyone would be amazing!

FINAL RESULT

I finally managed to achieve what I was trying to do, most of the problem is that the SVG was not crated in the best way for animation in Illustrator, I needed to convert my object into a Compound Path so I could use it as clipPath and then have a simple path underneath which I can easily animate.

Demo: http://codepen.io/anon/pen/zqjyzX

See my demo, click on blue circle to restart animation.

 var sign = new Vivus('mySign', { type: 'scenario-sync', duration: 20, start: 'autostart', dashGap: 20, forceRender: false }, function() { if (window.console) { console.log('Animation finished. [log triggered from callback]'); } $('#mySign').find('path').attr('class', 'fill_path'); }); function runme() { $('#mySign').find('path').attr('class', ''); sign.reset().play(); }; 
 svg * { stroke: blue; stroke-width: 1; fill: none; } .fill_path { fill: blue; } 
 <script src="//code.jquery.com/jquery-2.2.2.min.js"></script> <script src="//cdn.jsdelivr.net/vivus/latest/vivus.min.js"></script> <svg id="mySign" 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="200px" height="200px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve" > <path data-duration="30" d="M27.9,9.6c0,0,0.2,1.1,0.4,2.7c0.1,0.4,0.2,1.1,0,1.6c-0.2,0.6-0.7,0.8-1.1,0.9c-0.4,0.1-1,0.1-1.3-0.2c-0.7,0.7-0.6,0.6-1.1,0.7c0,0-0.3,0.1-0.4,0.1c-0.1,0-0.2-0.1-0.3-0.1c-0.6,0.1-0.9-1-1-1.4c-0.3-1.4-0.6-2.8-0.9-4.2C22.2,9.5,22,9,22,8.9c-0.1-0.2-0.1-0.5-0.2-0.7c0.1,0,0.4-0.1,0.5-0.1c0.4-0.1,0.5,0.1,0.6,0.5c0.4,1.5,0.7,3,1.1,4.5c0.1,0.2,0.4,1.4,0.9,1.3c0.3-0.1,0.1-1.1,0.1-1.3c-0.2-0.8-0.3-1.6-0.5-2.4c-0.1-0.8-0.2-1.5-0.4-2.3c-0.2-0.7-0.1-0.8,0.3-0.9c0.2,0,0.6,0,0.6,0.2c0,0,0,0.4,0,0.4c0,0.2,0.1,0.4,0.1,0.6c0.3,1.4,0.6,2.9,0.8,4.3c0.1,0.4,0.6,1,1.1,0.9c0.4-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5c0-16.6-12.9-29.5-29.5-29.5c-16.6,0-29.5,12.9-29.5,29.5C295.1,48.4,308,61.2,324.6,61.2z" onclick="runme();" /> </svg> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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