简体   繁体   English

如何在SVG中从0到100%制作饼图零件的动画?

[英]How can I animate a pie chart part from 0 to 100% in svg?

I am trying to figure out how to animate a pie chart part from 0 to 100. I can't even draw an arc that has more than 180 degrees. 我试图弄清楚如何对从0到100的饼图零件进行动画处理。我什至无法绘制超过180度的弧。 If I try to animate from a 1 degree angle to a 90 degree angle, instead of having a nice transition I'm getting a shape morph. 如果我尝试从1度角到90度角进行动画处理,而不是进行很好的过渡,我会得到形状变形。

I'm trying to draw parts of the pie chart with paths like this: 我正在尝试使用以下路径绘制饼图的一部分:

M 100 100 l 0 -50 a 50 50 0 0 0 -20 10 z

My first challenge is calculating the last two numbers, the end point of the arc, and the second challenge is writing an animation that goes from a 1 deg angle to a 360 deg angle. 我的第一个挑战是计算最后两个数字,即圆弧的终点,第二个挑战是编写从1度角到360度角的动画。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Its worth reading this answer on SO which has info on the dasharray effect which can be useful. 值得阅读关于SO的答案,它具有关于点阵效果的信息,这可能是有用的。 That doesn't directly answer the pie question, but may give some ideas. 那不能直接回答派的问题,但是可以给出一些想法。 A lot will depend on specifically how you want it animated, to whether these would work for you. 具体取决于您希望动画效果如何,以及这些效果是否适合您。

So you could draw a full circle with a string like "M 100, 100 m -75, 0 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0" which draws 2 arcs. 因此,您可以使用“ M 100,100 m -75,0 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0”这样的字符串绘制一个完整的圆,该圆将绘制2个圆弧。

You could also just do it with a circle. 您也可以只画一个圆圈。 So here's a few bits, and a Snap example alongside it, as you will be using that, and its useful to compare... 因此,这里有一些地方,以及旁边的一个Snap示例,您将使用它,并且它对于比较很有用...

<svg width="600" height="425">
    <path d="M 100, 100 m -75, 0 a 75,75 0 1,0 150,0 a 75,75 0 1,0 -150,0" fill="none" stroke="black" stroke-width="150" stroke-dasharray="0 600 600 0" stroke-dashoffset="1000">
        <animate attributeType="XML" attributeName="stroke-dashoffset" from="0" to="600" dur="2s" repeatCount="1" fill="freeze"/> 
    </path>
    <circle cx="150" cy="350" r="80" fill="none" stroke="red" stroke-width="161" stroke-dasharray="0 600 600 0" stroke-dashoffset="1000" >
        <animate attributeType="XML" attributeName="stroke-dashoffset" from="0" to="600" dur="2s" repeatCount="1" fill="freeze">
        </animate>
    </circle>
</svg>

same last bit with Snap.js 与Snap.js相同的最后一点

var s = Snap(600,600);

var c = s.circle(150, 150, 80).attr({
    fill: "none",
    stroke: 'red',
    strokeWidth: 161,
    strokeDasharray: "0 600 600 0",
    strokeDashoffset: 1000
});

Snap.animate(0,600, function( value ){ 
       c.attr({ 'strokeDashoffset': value })
},5000 );

Here is a jsfiddle with them all on 这是一个所有的jsfiddle

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

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