我已经能够使用这段代码成功地沿着圆弧路径制作圆弧动画
var archtype = Raphael("canvas", 200, 100);
archtype.customAttributes.arc = function (xloc, yloc, value, total, R) {
var alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = xloc + R * Math.cos(a),
y = yloc - R * Math.sin(a),
path;
if (total == value) {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
];
} else {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, +(alpha > 180), 1, x, y]
];
}
return {
path: path
};
};
//make an arc at 50,50 with a radius of 30 that grows from 0 to 40 of 100 with a bounce
var my_arc = archtype.path().attr({
"stroke": "#f00",
"stroke-width": 14,
arc: [50, 50, 0, 100, 30]
});
my_arc.animate({
arc: [50, 50, 40, 100, 30]
}, 1500, "bounce");
使用此代码的唯一问题是我需要在页面上有多个 Canvas 元素,我不想在同一页面上定义 10 次 archtype.customAttributes.arc
.
为了解决这个问题,我想我可以这样做......
function arc (xloc, yloc, value, total, R) {
var alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = xloc + R * Math.cos(a),
y = yloc - R * Math.sin(a),
path;
if (total == value) {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
];
} else {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, +(alpha > 180), 1, x, y]
];
}
return path;
}
var path = arc(50, 50, 0, 100, 30);
var my_arc = archtype.path().attr({
"stroke": "#f00",
"stroke-width": 14,
path:path
});
var path = arc(50, 50, 40, 100, 30);
my_arc.animate({
path:path
}, 1500, "bounce");
但是,当我尝试这样做时,圆弧的末端会采用最直的路径到达新的端点,从而产生扭曲、变形的效果。
任何人都可以解释为什么我的示例会这样做并建议我解决这个问题而不必为我在页面上需要的每个 Canvas 声明自定义属性吗?我是否错误地假设 arc: [50, 50, 0, 100, 30]
与 path: path
相同?
感谢您的帮助。
最佳答案
我无法给出有根据的答案,但通过改编此示例解决了我的问题:http://jsfiddle.net/7jHPv/5/在这个问题中引用:Animating an arc in Raphael JS wobbles in Chrome
var myArc = r.path().attr({
"stroke": arcColours[i]
, "stroke-width": 6
, arc: [1, 100, muffinRadius, step*(i+1), diagramHeight/2]
});
myArc.animate({
arc: [values[i], 100, muffinRadius, step*(i+1), diagramHeight/2]
}, 1500, "<>", function() {
// anim complete here
});
关于javascript - 拉斐尔圆弧动画正在变形而不是跟随圆弧路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15964530/