views:

592

answers:

1

Hi all,

I've just discovered Raphael, and love it, but I am not much of a javascript-er. Right now I have three repeated sections of code to draw the same circle in three different divs. The default for making a canvas in Raphael finds an element by ID, but I want to have one set of variables to have circles drawn in all divs with class "circle". I think there must be a more efficient way of coding this. Here is the code I'm using now:

window.onload = function () {
    var paper = Raphael("c1", 26, 26); /* Make canvas 26*26px in div id "c1" */
    var circle = paper.circle(13, 13, 10.5); /* Draw circle at the center of the canvas with radius 10.5  */
    circle.attr("stroke", "#f1f1f1");
    circle.attr("stroke-width", 2);
    var text = paper.text(13, 13, "1"); /* Print text "1" inside the circle  */
    text.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
    text.attr("fill", "#f1f1f1");

    var paper2 = Raphael("c2", 26, 26);
    var circle2 = paper2.circle(13, 13, 10.5);
    circle2.attr("stroke", "#f1f1f1");
    circle2.attr("stroke-width", 2);
    var text2 = paper2.text(12, 13, "2");
    text2.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
    text2.attr("fill", "#f1f1f1");

    var paper3 = Raphael("c3", 26, 26);
    var circle3 = paper3.circle(13, 13, 10.5);
    circle3.attr("stroke", "#f1f1f1");
    circle3.attr("stroke-width", 2);
    var text3 = paper3.text(12, 13, "3");
    text3.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
    text3.attr("fill", "#f1f1f1");
};

Test site is @ http://jesserosenfield.com/fluid/test.html

Thanks so much for your help!

+3  A: 

define a function which takes a argument for the div so that you can automate the process:

function drawcircle(div, text) { 
    var paper3 = Raphael(div, 26, 26); //<<
    var circle3 = paper3.circle(13, 13, 10.5);
    circle3.attr("stroke", "#f1f1f1");
    circle3.attr("stroke-width", 2);
    var text3 = paper3.text(12, 13, text); //<<
    text3.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
    text3.attr("fill", "#f1f1f1");
}

Then in your window.onload:

window.onload = function () {
    drawcircle("c1", "1");
    drawcircle("c2", "2");
    drawcircle("c3", "3");
};
agscala
brilliant, thank you.
j-man86