最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Raphael js drawing circles in all elements with specified class - Stack Overflow

programmeradmin0浏览0评论

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 @ .html

Thanks so much for your help!

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./fluid/test.html

Thanks so much for your help!

Share Improve this question edited Mar 30, 2010 at 14:33 HandiworkNYC. asked Mar 30, 2010 at 14:26 HandiworkNYC.HandiworkNYC. 11.1k25 gold badges95 silver badges156 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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");
};
发布评论

评论列表(0)

  1. 暂无评论