Question

I have been attempting to use the Raphael JavaScript library to draw a percentage of a circle anti-clockwise, whilst specifying the starting position.

At the moment I have managed to get it drawing anti-clockwise and starting at the 6 o'clock position using the code below, but I would like it to start drawing at the 4 o'clock position (or ideally at any position I specify).

var amount = 75;

var archtype = Raphael("canvas", 500, 500);
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;
            console.log('x - ' + x);
            console.log('y - ' + y);
        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
        };
    };

var my_arc = archtype.path().attr({
    "stroke": "#00A5FF",
    "stroke-width": 14,
    arc: [90, 90, 0, 100, 80]
});

my_arc.animate({
    arc: [90, 90, amount, 100, 80]
}, 1500);

I am just starting out using the Raphael library and would really appreciate any help.

Thanks

Était-ce utile?

La solution

Just found the .rotate function. The following code rotates the circle -45 degrees which starts it at the 4 o'clock position. I think you would need to use the transform function in Raphael v2.

my_arc.rotate(-45, 90, 90).animate({
    arc: [90, 90, amount, 100, 80]
}, 1500);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top