【问题标题】:Raphael js drawing circles in all elements with specified classRaphael js在具有指定类的所有元素中绘制圆圈
【发布时间】:2010-03-30 14:26:35
【问题描述】:

我刚刚发现了 Raphael,并且很喜欢它,但我并不是一个 javascript-er。现在我有三个重复的代码部分来在三个不同的 div 中绘制同一个圆圈。在 Raphael 中制作画布的默认设置是通过 ID 查找元素,但我希望有一组变量在所有具有“circle”类的 div 中绘制圆。我认为必须有一种更有效的编码方式。这是我现在使用的代码:

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");
};

测试地点是@http://jesserosenfield.com/fluid/test.html

非常感谢您的帮助!

【问题讨论】:

    标签: javascript drawing raphael


    【解决方案1】:

    定义一个接受 div 参数的函数,以便您可以自动化该过程:

    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");
    }
    

    然后在你的 window.onload 中:

    window.onload = function () {
        drawcircle("c1", "1");
        drawcircle("c2", "2");
        drawcircle("c3", "3");
    };
    

    【讨论】:

      猜你喜欢
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      相关资源
      最近更新 更多