Así es como lo he hecho. El siguiente código le permite especificar un ángulo de inicio y final, así como un radio interno y externo (útil para hacer los gráficos circulares de moda estilo rosquilla). La solución no se basa en aproximar una curva con segmentos de línea y puede animarse según el ejemplo de reloj mencionado en la pregunta original.
Primero crea tu área de dibujo de Rafael; la siguiente asume un div con id "raphael_paper" en el archivo HTML:
var paper = Raphael("raphael_paper", 800, 800);
a este objeto Raphael añadimos una costumbre arc
atributo, una función que toma el centro de un círculo (x, y coordenadas y), una iniciar ángulo, un ángulo final, un radio interior y un radio exterior:
paper.customAttributes.arc = function (centerX, centerY, startAngle, endAngle, innerR, outerR) {
var radians = Math.PI/180,
largeArc = +(endAngle - startAngle > 180);
// calculate the start and end points for both inner and outer edges of the arc segment
// the -90s are about starting the angle measurement from the top get rid of these if this doesn't suit your needs
outerX1 = centerX + outerR * Math.cos((startAngle-90) * radians),
outerY1 = centerY + outerR * Math.sin((startAngle-90) * radians),
outerX2 = centerX + outerR * Math.cos((endAngle-90) * radians),
outerY2 = centerY + outerR * Math.sin((endAngle-90) * radians),
innerX1 = centerX + innerR * Math.cos((endAngle-90) * radians),
innerY1 = centerY + innerR * Math.sin((endAngle-90) * radians),
innerX2 = centerX + innerR * Math.cos((startAngle-90) * radians),
innerY2 = centerY + innerR * Math.sin((startAngle-90) * radians);
// build the path array
var path = [
["M", outerX1, outerY1], //move to the start point
["A", outerR, outerR, 0, largeArc, 1, outerX2, outerY2], //draw the outer edge of the arc
["L", innerX1, innerY1], //draw a line inwards to the start of the inner edge of the arc
["A", innerR, innerR, 0, largeArc, 0, innerX2, innerY2], //draw the inner arc
["z"] //close the path
];
return {path: path};
};
ahora podemos usar esto para dibujar arcos de un espesor especificado, empezando y terminando dondequiera que los queremos por ejemplo.
var redParams = {stroke: "#f00", "stroke-width": 1, fill:"#eee"},
greenParams = {stroke: "#0f0", "stroke-width": 1, fill:"#eee"},
blueParams = {stroke: "#00f", "stroke-width": 1, fill:"#eee"},
cx = 300, cy = 300, innerRadius = 100, outerRadius = 250,
var red = paper.path().attr(redParams).attr({arc: [cx, cy, 0, 90, innerRadius, outerRadius]});
var green = paper.path().attr(greenParams).attr({arc: [cx, cy, 270, 320, innerRadius, outerRadius]});
var blue = paper.path().attr(blueParams).attr({arc: [cx, cy, 95, 220, innerRadius, outerRadius]});
Esto debería producir tres segmentos de arco gris con bordes de 1px rojo, azul y verde.
Por conveniencia, aquí está esta respuesta como un jsfiddle donde puede ingresar cuánto del círculo dibujar: http://jsfiddle.net/Bzdnm/2/ – user568458
Al igual que con este y el ejemplo del Reloj Polar, ¿Alguien sabe cómo modificar esto para que el punto de inicio no sea siempre a las 12 en punto? Supuse que el valor inicial de 100 haría eso, pero parece que no funciona. – daxiang28
Usa el método de rotación. CÓDIGO {my_arc.rotate (-90, 50, 50) .animate ({ arco: [50, 50, cantidad, 100, 30] }, 1500, "rebote"); } Puedes encontrar más sobre esto en los documentos de Raphael. – genkilabs