【问题标题】:Raphael arc animation is morphing instead of following arc path拉斐尔弧形动画正在变形而不是跟随弧形路径
【发布时间】:2013-04-04 13:51:51
【问题描述】:

我已经能够使用此代码成功地沿圆弧路径制作圆弧动画

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");

使用此代码的唯一问题是我需要在页面上有多个画布元素,并且我不想在同一页面上定义 archtype.customAttributes.arc 10 次。

为了解决这个问题,我想我可以这样做......

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");

但是,当我尝试这样做时,弧的末端会以最直的路径到达新端点,从而导致扭曲变形效果。

谁能解释为什么我的示例会这样做并推荐一个是让我克服这个问题,而不必为页面上我需要的每个画布声明一个自定义属性?我是否错误地假设 arc: [50, 50, 0, 100, 30]path: path 相同?

感谢您的帮助。

【问题讨论】:

  • 遇到同样的问题 - 有人解决了吗?

标签: javascript animation svg raphael


【解决方案1】:

我无法给出有根据的答案,但通过修改此示例解决了我的问题: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
    });

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多