我有一个循环扇区,我希望逐渐将该扇区动画为一个完整的圆圈,我怎样才能用Rapheal.js制作它。
BTW,我是SVG的新手,我使用svg-editor使用SVG绘制形状。
这是我的代码
var arcPath1 = paper.path("m325.30255,127.93972c33.86002,6.238 52.02301,8.85 90.823,49.616l105.53491,-65.494c-44.84491,-59.9466 -101.70261,-109.25547 -201.98761,-108.83956l5.6297,124.71756z");
arcPath1.attr({
"fill": "#003a60",
stroke: 'none'
});
arcPath1.rotate(1.0569896697998047 ,420.6666870117177,90.38764953613116);
arcPath1.node.id= "arch1";
这是我的代码http://jsfiddle.net/v2KeV/
的示例如何让动画完成完整圈子的路径?
注意:动画我只是测试路径动画..
答案 0 :(得分:2)
嗯,首先要做的事情是:为了优雅地为弧段设置动画,我们需要能够以编程方式生成它们。考虑一下这种笨拙的三角学:
// arcPath:
// cx, cy are the center point
// inner_radius and outer_radius describe the distance of the arc segment's inner and outer boundaries from the center point.
// starting_radians describes the offset of the segment start;
// arc_radians describes the width of the arg segment.
function arcPath( cx, cy, inner_radius, outer_radius, starting_radians, arc_radians )
{
var x1 = cx + Math.cos( starting_radians ) * inner_radius;
var y1 = cy + Math.sin( starting_radians ) * inner_radius;
var x2 = cx + Math.cos( starting_radians ) * outer_radius;
var y2 = cy + Math.sin( starting_radians ) * outer_radius;
var x3 = cx + Math.cos( starting_radians + arc_radians ) * outer_radius;
var y3 = cy + Math.sin( starting_radians + arc_radians ) * outer_radius;
var x4 = cx + Math.cos( starting_radians + arc_radians ) * inner_radius;
var y4 = cy + Math.sin( starting_radians + arc_radians ) * inner_radius;
var pathstr = "M" + x1 + "," + y1 + " "
+ "L" + x2 + "," + y2 + " "
+ "A" + outer_radius + "," + outer_radius + " " + arc_radians + " " + ( arc_radians > Math.PI ? "1" : "0" ) + " 1 " + x3 + "," + y3 + " "
+ "L" + x4 + "," + y4 + " "
+ "A" + inner_radius + "," + inner_radius + " " + ( 0 - arc_radians ) + " " + ( arc_radians > Math.PI ? "1" : "0" ) + " 0 " + x1 + "," + y1 + " z";
return pathstr;
}
使用更简单的几何构造,您可以简单地为从部分弧段到整个弧段的路径设置动画,并让Raphael进行提升,如下所示:
var arcPath1 = paper.path(arcPath( 150, 150, 75, 125, Math.PI / 2, Math.PI * 0.65 ) )
.attr({ fill: "#003a60", 'fill-opacity': 0.5, stroke: 'black' });
arcPath1.click(function ()
{
arcPath1.animate( { path: arcPath( 150, 150, 75, 125, Math.PI / 2, Math.PI * 2 - 0.0001 ) }, 2000, "<>" );
});
不幸的是,拉斐尔在这方面做得非常糟糕,所以你会得到一个相交弧段的爆炸,将自己重塑成一个完整的圆圈而不是所需的弧扫。所以为了让它做你想做的事,我们必须手动执行动画。
function arcSweep( arc, x, y, inner, outer, angle_offset, from_sweep, to_sweep, duration )
{
var steps = 100;
var current_step = 0;
var intervalID = setInterval( function()
{
current_step++;
if ( current_step >= steps )
clearInterval( intervalID );
arc.attr( { path: arcPath( x, y, inner, outer, angle_offset, from_sweep + ( ( to_sweep - from_sweep ) * ( current_step / steps ) ) ) } );
}, duration / steps );
}
所以基本上我们只计算一系列中间弧并按顺序进行,直到我们达到目标角度。除了目标结束角度(不能是Math.PI * 2 - 因为它计算为空弧段!)时,动画功能需要弧的所有原始参数。可能需要将其抽象为一个类,以便弧段的每个实例都可以跟踪它自己的变量。
这是展示成品的fiddle。