Raphael.js customAttributes动画

时间:2013-10-19 17:17:22

标签: javascript animation raphael

我正在尝试使用Raphael.js创建一个简单的圆圈扇区动画。动画应该从0到2 * Pi展开圆扇区。问题是它只是在1秒后绘制一个没有任何动画的圆圈。

这是我的代码:

<html>
<head>
    <title></title>
    <script src="raphael.js"></script>
</head>
<body>
    <script>
        window.onload = function () {
            var paper = Raphael("holder");

            paper.ca.sector = function (x, y, r1, r2, startAngle, endAngle, color) {
                    var x11 = x + r1 * Math.sin(startAngle);
                    var y11 = y - r1 * Math.cos(startAngle);
                    var x21 = x + r1 * Math.sin(endAngle);
                    var y21 = y - r1 * Math.cos(endAngle);

                    var x12 = x + r2 * Math.sin(startAngle);
                    var y12 = y - r2 * Math.cos(startAngle);
                    var x22 = x + r2 * Math.sin(endAngle);
                    var y22 = y - r2 * Math.cos(endAngle);

                    var big = 0;
                    if (endAngle - startAngle > Math.PI) 
                        big = 1;

                    var pathList = ["M", x12, y12,
                                    "A", r2, r2, 0, big, 1, x22, y22,
                                    "L", x21, y21, 
                                    "A", r1, r1, 0, big, 0, x11, y11,
                                    "Z"];

                    return {path: pathList, fill: color, stroke: "none"};
                }

            var p = paper.path().attr({sector: [300, 300, 100, 140, 0, 0.0001, "pink"]});
            p.animate({sector: [300, 300, 100, 140, 0, Math.PI * 1.999999, "pink"]}, 1000, "bounce");
        };
    </script>

    <div id="holder" style="width: 700px; height: 700px;"></div>
</body>
</html>

很抱歉长列表。

所以问题为什么会发生?我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

这是工作代码:

        var paper = Raphael("holder");

        var createSector = function (x, y, r1, r2, startAngle, endAngle, color) {
                var x11 = x + r1 * Math.sin(startAngle);
                var y11 = y - r1 * Math.cos(startAngle);
                var x21 = x + r1 * Math.sin(endAngle);
                var y21 = y - r1 * Math.cos(endAngle);

                var x12 = x + r2 * Math.sin(startAngle);
                var y12 = y - r2 * Math.cos(startAngle);
                var x22 = x + r2 * Math.sin(endAngle);
                var y22 = y - r2 * Math.cos(endAngle);

                var big = 0;
                if (endAngle - startAngle > Math.PI) 
                    big = 1;

                var pathList = ["M", x12, y12,
                                "A", r2, r2, 0, big, 1, x22, y22,
                                "L", x21, y21, 
                                "A", r1, r1, 0, big, 0, x11, y11,
                                "Z"];

                return {path: pathList, fill: color, stroke: "none"};
            }

        var p = paper.path();
           p.attr(createSector(300, 300, 100, 140, 0, 0.001, "pink"));
        p.animate(createSector(300, 300, 100, 140, 0.01, Math.PI * 1.999999, "pink"), 1000, "bounce");

我仍然不确定,为什么你的代码无效

JSFiddle demo

编辑: 好吧,我一直在尝试一些值,它看起来像raphael中的某种错误,因为路径的填充不与动画一起使用。

var paper = Raphael("holder");

        paper.customAttributes.sector = function (x, y, r1, r2, startAngle, endAngle, color,strokeWidth) {
                var x11 = x + r1 * Math.sin(startAngle);
                var y11 = y - r1 * Math.cos(startAngle);
                var x21 = x + r1 * Math.sin(endAngle);
                var y21 = y - r1 * Math.cos(endAngle);

                var x12 = x + r2 * Math.sin(startAngle);
                var y12 = y - r2 * Math.cos(startAngle);
                var x22 = x + r2 * Math.sin(endAngle);
                var y22 = y - r2 * Math.cos(endAngle);

                var big = 0;
                if (endAngle - startAngle > Math.PI) 
                    big = 1;

                var pathList = ["M", x12, y12,
                                "A", r2, r2, 0, big, 1, x22, y22,
                                "L", x21, y21, 
                                "A", r1, r1, 0, big, 0, x11, y11,
                                "Z"];

            return {path: pathList, fill: color,'stroke-width':strokeWidth};
            }

        var p = paper.path();
           p.attr({sector: [300, 300, 100, 140, 0, 0.0001, "#caac12",1]});
        p.animate({sector: [300, 300, 100, 140, 0.0001, Math.PI * 1.999999, "#cacaca",0]}, 3000);

JSFiddle demo 2