3

Raphael を発見したばかりで気に入っていますが、JavaScript はあまり得意ではありません。現在、3 つの異なる div で同じ円を描画するコードの 3 つの繰り返しセクションがあります。Raphael でキャンバスを作成するためのデフォルトでは、ID で要素が検索されますが、クラス「circle」のすべての div に円を描画する変数のセットが 1 つ必要です。これをコーディングするより効率的な方法が必要だと思います。これが私が今使っているコードです:

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

助けてくれてどうもありがとう!

4

1 に答える 1

8

プロセスを自動化できるように、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");
};
于 2010-03-30T14:44:14.683 に答える