简体   繁体   English

SVG动画:在绘制弧时为其设置动画

[英]SVG Animation: Animate an arc as it is drawn

I am drawing an Arc with SVG using the following snippet: 我正在使用以下代码段使用SVG绘制Arc:

https://jsfiddle.net/e6dx9oza/293/ https://jsfiddle.net/e6dx9oza/293/

The starting and ending angle of the arc will be dynamically fed in when describeArc method is called to calculate the path. 当调用describeArc方法计算路径时,将动态输入弧的起始和结束角度。

Does anyone know how can I animate the arc as it gets drawn? 有没有人知道如何在绘制弧线时为其设置动画? Basically I want the arc to be drawn smoothly with a delay, instead of it getting drawn at one go as in the present case. 基本上我希望延迟平滑地绘制圆弧,而不是像现在这样一次性绘制。

You question doesn't describe what you mean by "animate". 你的问题没有描述你的“动画”是什么意思。 Please think about that next time you ask a question. 下次你问一个问题时请考虑一下。

I am going to assume you want the sector to open like a fan. 我假设你想让这个部门像粉丝一样开放。

Here's one way to do it. 这是一种方法。

 function polarToCartesian(centerX, centerY, radius, angleInDegrees) { var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0; return { x: centerX + (radius * Math.cos(angleInRadians)), y: centerY + (radius * Math.sin(angleInRadians)) }; } function describeArc(x, y, radius, startAngle, endAngle){ var start = polarToCartesian(x, y, radius, endAngle); var end = polarToCartesian(x, y, radius, startAngle); var arcSweep = endAngle - startAngle <= 180 ? "0" : "1"; var d = [ "M", start.x, start.y, "A", radius, radius, 0, arcSweep, 0, end.x, end.y, "L", x,y, "L", start.x, start.y ].join(" "); //console.log(d); return d; } function animateSector(x, y, radius, startAngle, endAngle, animationDuration) { var startTime = performance.now(); function doAnimationStep() { // Get progress of animation (0 -> 1) var progress = Math.min((performance.now() - startTime) / animationDuration, 1.0); // Calculate the end angle for this point in the animation var angle = startAngle + progress * (endAngle - startAngle); // Calculate the sector shape var arc = describeArc(x, y, radius, startAngle, angle); // Update the path document.getElementById("arc1").setAttribute("d", arc); // If animation is not finished, then ask browser for another animation frame. if (progress < 1.0) requestAnimationFrame(doAnimationStep); } requestAnimationFrame(doAnimationStep); } animateSector(100, 100, 100, 120, 418.25, 1000); 
 svg { height: 200px; width: 200px; } 
 <svg> <path id="arc1" fill="green" /> </svg> 

Fiddle here: https://jsfiddle.net/e6dx9oza/351/ 在这里小提琴: https//jsfiddle.net/e6dx9oza/351/

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

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