Animating a counter-clockwise arc in raphael.js

爱⌒轻易说出口 提交于 2019-12-23 04:06:16

问题


I got the exact arc animation I need from the thread drawing centered arcs in raphael js

However, I need another instance of this arc drawn counter-clockwise, while this one only seems to be clockwise. Is it possible for this arc to be drawn and animated counter-clockwise?

code snippet from the answered question:

// Custom Arc Attribute, position x&y, value portion of total, total value, Radius
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");

回答1:


As Kevin said, you need the sweep_flag set, and you need to invert a few variables. Here is my final code with it going anti-clockwise;

paper.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;
    console.log(x);
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), 0, x, y]
    ];
}
return {
    path: path
};
};



回答2:


I was not able to figure out how to make the arc itself go counter clockwise, but I was able to mirror the SVG itself using the CSS from this topic.




回答3:


The answer to your question is buried in the w3c documentation for elliptical arcs in the svg1.1 spec. What you were looking for was the sweep-flag -- 1 (which you are using throughout) results in a clockwise arc, and 0 results in a counterclockwise one. When I stumbled across this question a few months ago I found nothing on RaphaelJS.com to answer the question, which annoyed me greatly.

You could implement this a number of ways, and something like this would probably be the straightest approach:

archtype.customAttributes.arc = function (xloc, yloc, value, total, R, sweep_flag ) {
    sweep_flag = sweep_flag || 1;
    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, sweep_flag, xloc - 0.01, yloc - R]
        ];
    } else {
        path = [
            ["M", xloc, yloc - R],
            ["A", R, R, 0, +(alpha > 180), sweep_flag, x, y]
        ];
    }
    return {
        path: path
    };
};

Using this you should be able to control the direction of the arc using an optional 6th parameter.




回答4:


Using this you can pass negative numbers to the value parameter.

r.ca.arc = function(centerX, centerY, value, total, radius){
    var
        angle = value / total * 359.99,
        absAngle = Math.abs(angle),
        a = Math.PI / 180 * (90 - angle),
        x = centerX + radius * Math.cos(a),
        y = centerY - radius * Math.sin(a),
        sweep = +(value >= 0),
        path;

        path = [
            ['M', centerX, centerY - radius],
            ['A', radius, radius, 0, +(absAngle > 180), sweep, x, y]
        ];

        return { 'path': path };
};


来源:https://stackoverflow.com/questions/12012006/animating-a-counter-clockwise-arc-in-raphael-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!