2011-02-21 7 views
25

raphael.jsを使用してさまざまなサイズの同心円を描画する必要があります。私はhttp://raphaeljs.com/polar-clock.htmlの後ろのコードを理解しようとしましたが、これは私が望むものと非常によく似ていますが、コメントはありません。raphael jsの中心アークを描画する

理想的には、いくつかの中心点から所定の距離にあり、ある角度から始まり、ある角度で終了するパスを作成する関数が必要です。

答えて

53

その答えはokですとですが、アニメ化されカントとして動作SVGパス構文を使用してパスを作成します。私はあなたのために極クロックの重要なものを切り裂きました。成長していく赤い弧があります。楽しい。

// Custom Arc Attribute, position x&y, value portion of total, total value, Radius 
var archtype = Raphael("canvas", 200, 100); 
archtype.customAttributes.arc = function (xloc, yloc, value, total, R) { 
    var alpha = 360/total * value, 
     a = (90 - alpha) * Math.PI/180, 
     x = xloc + R * Math.cos(a), 
     y = yloc - R * Math.sin(a), 
     path; 
    if (total == value) { 
     path = [ 
      ["M", xloc, yloc - R], 
      ["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R] 
     ]; 
    } else { 
     path = [ 
      ["M", xloc, yloc - R], 
      ["A", R, R, 0, +(alpha > 180), 1, x, y] 
     ]; 
    } 
    return { 
     path: path 
    }; 
}; 

//make an arc at 50,50 with a radius of 30 that grows from 0 to 40 of 100 with a bounce 
var my_arc = archtype.path().attr({ 
    "stroke": "#f00", 
    "stroke-width": 14, 
    arc: [50, 50, 0, 100, 30] 
}); 

my_arc.animate({ 
    arc: [50, 50, 40, 100, 30] 
}, 1500, "bounce"); 
+9

便宜上、この答えはjsfiddleとして描画します。http://jsfiddle.net/Bzdnm/2/ – user568458

+0

これとPolar Clockの例と同様に、誰もこれを修正する方法を知っているので、開始点は常に12時にあるとは限りません。私は100のうちの最初の値がそれをするだろうと推測しましたが、それはうまくいかないようです。 – daxiang28

+0

rotateメソッドを使用します。 CODE {my_arc.rotate(-90,50,50)。アニメート({ 円:[50,50、量、100,30] }、1500、 "バウンス"); } Raphaelのドキュメントで詳細を見つけることができます。 – genkilabs

7

実際に自分自身で答えを見つけました。私はまず、ベジエ曲線を含む何かを想像しましたが、これはちょうどうまくいきます。

- >は、ラファエル

function arc(center, radius, startAngle, endAngle) { 
    angle = startAngle; 
    coords = toCoords(center, radius, angle); 
    path = "M " + coords[0] + " " + coords[1]; 
    while(angle<=endAngle) { 
     coords = toCoords(center, radius, angle); 
     path += " L " + coords[0] + " " + coords[1]; 
     angle += 1; 
    } 
    return path; 
} 

function toCoords(center, radius, angle) { 
    var radians = (angle/180) * Math.PI; 
    var x = center[0] + Math.cos(radians) * radius; 
    var y = center[1] + Math.sin(radians) * radius; 
    return [x, y]; 
} 
+0

誰かが興味がある場合に備えて[php version](http://ideone.com/m4fMU)を作ると思いました。 –

+0

あなた自身を円で近似する必要はありません。 SVGはすでに楕円形のパスを提供しています – Cuadue

7

ただ、この作品完全なコードで、user592699の回答からいくつかの当て推量を削除する:

<script src="raphael.js"></script> 
<script> 

    var paper = Raphael(20, 20, 320, 320); 

    function arc(center, radius, startAngle, endAngle) { 
     angle = startAngle; 
     coords = toCoords(center, radius, angle); 
     path = "M " + coords[0] + " " + coords[1]; 
     while(angle<=endAngle) { 
      coords = toCoords(center, radius, angle); 
      path += " L " + coords[0] + " " + coords[1]; 
      angle += 1; 
     } 
     return path; 
    } 

    function toCoords(center, radius, angle) { 
     var radians = (angle/180) * Math.PI; 
     var x = center[0] + Math.cos(radians) * radius; 
     var y = center[1] + Math.sin(radians) * radius; 
     return [x, y]; 
    } 

    paper.path(arc([100, 100], 80, 0, 270)); // draw an arc 
              // centered at (100, 100), 
              // radius 80, starting at degree 0, 
              // beginning at coordinate (80, 0) 
              // which is relative to the center 
              // of the circle, 
              // going clockwise, until 270 degree 

</script> 
+1

http://jsfiddle.net/pajtai/9LKEu/ –

+0

クリーンアップありがとう! – plang

10

は、ここで私はそれを行っている方法です。次のコードでは、開始角度と終了角度、および内側と外側の半径(これらのトレンディなドーナツスタイルの円グラフを実行するのに便利です)を指定することができます。このソリューションは、線分で曲線を近似することに依存せず、元の質問に記載されている時計の例に従ってアニメーション化することができます。

まず、ラファエロの描画領域を作成します。以下は、あなたのHTMLファイル内にID「raphael_paper」とdiv要素を前提としています、私たちは、カスタム属性arc、円(xとy COORDS)の中心を取る機能を追加し、このラファエル・オブジェクトへ

var paper = Raphael("raphael_paper", 800, 800); 

今、我々はそれらを例えばしたい場所の開始と終了、指定された厚さの弧を描くようにこれを使用することができます

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

:角度、終了角度、内側半径と外側の半径を開始します。

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

これは、赤、青、緑の1ピクセルの境界線で三個のグレー円弧セグメントをもたらすはずです。

+0

ラファエルでドーナツチャートを作成しようとしていましたが、これは私が必要としていたものでした! –

1

ループを使用せずにこれを行うこともできます。以下はこれを達成し、負の角度でも動作します。

ラファエロオブジェクトをrとして渡します。角度は、0度から始まります。これは、他のいくつかの解決策に挙げられているように、右よりも円の上にあります。アークは、閉じたパスではなく、ストロークで行うことがしたい人のための

 function drawArc(r, centerX, centerY, radius, startAngle, endAngle) { 
      var startX = centerX+radius*Math.cos((90-startAngle)*Math.PI/180); 
      var startY = centerY-radius*Math.sin((90-startAngle)*Math.PI/180); 
      var endX = centerX+radius*Math.cos((90-endAngle)*Math.PI/180); 
      var endY = centerY-radius*Math.sin((90-endAngle)*Math.PI/180); 
      var flg1 = 0; 

      if (startAngle>endAngle) 
       flg1 = 1; 
      else if (startAngle<180 && endAngle<180) 
       flg1 = 0; 
      else if (startAngle>180 && endAngle>180) 
       flg1 = 0; 
      else if (startAngle<180 && endAngle>180) 
       flg1 = 0; // edited for bugfix here, previously this was 1 
      else if (startAngle>180 && endAngle<180) 
       flg1 = 1; 

      return r.path([['M',startX, startY],['A',radius,radius,0,flg1,1,endX,endY]]); 
     }; 
2

、私はgenkilabsの溶液を作製するために答える高めています。あなたのアークに外側の卒中を与える必要がある場合、これは役に立ちます。

// Custom Arc Attribute, position x&y, value portion of total, total value, Radius, width 
var archtype = Raphael("canvas", 200, 100); 
archtype.customAttributes.arc = function (xloc, yloc, value, total, R, width) { 
    if(!width) width = R * 0.4; 
    var alpha = 360/total * value, 
     a = (90 - alpha) * Math.PI/180, 
     w = width/2, 
     r1 = R + w, 
     r2 = R - w, 
     x1 = xloc + r1 * Math.cos(a), 
     y1 = yloc - r1 * Math.sin(a), 
     x2 = xloc + r2 * Math.cos(a), 
     y2 = yloc - r2 * Math.sin(a), 
     path; 
    if (total == value) { 
     path = [ 
      ["M", xloc, yloc - r1], 
      ["A", r1, r1, 0, 1, 1, xloc - 0.01, yloc - r1], 
      ["Z"], 
      ["M", xloc - 0.01, yloc - r2], 
      ["A", r2, r2, 0, 1, 0, xloc, yloc - r2], 
      ["Z"] 
     ]; 
    } else { 
     path = [ 
      ["M", xloc, yloc - r1], 
      ["A", r1, r1, 0, +(alpha > 180), 1, x1, y1], 
      ["L", x2, y2], 
      ["A", r2, r2, 0, +(alpha > 180), 0, xloc, yloc - r2], 
      ["L", xloc, yloc - r1], 
      ["Z"] 
     ]; 
    } 
    return { 
     path: path 
    }; 
}; 

//make an arc at 50,50 with a radius of 30 that grows from 0 to 40 of 100 with a bounce 
var my_arc = archtype.path().attr({ 
    "fill": "#00f", 
    "stroke": "#f00", 
    "stroke-width": 5, 
    arc: [50, 50, 0, 100, 30] 
}); 

my_arc.animate({ 
    arc: [50, 50, 40, 100, 30] 
}, 1500, "bounce"); 

JSFiddle

1

私はgenkilabsは、回転と反転能力を含めるように答えるようになってきました。また、どのくらいの量のリングが満たされたかは、単一の数値に変更されました。 (反転はthis postから適応されました)。それが役に立つと願っています!

paper.customAttributes.arc = function (xloc, yloc, percent, rad, rot, invert) { 
    var alpha = 3.6 * percent, 
    a = (90 - alpha) * Math.PI/180, 
    x = xloc + rad * Math.cos(a), 
    y = yloc - rad * Math.sin(a), 
    path; 

    if (invert) { 
     x = xloc - rad * Math.cos(a); 
    } 

    if (percent >= 100) { 
     path = [ 
      ["M", xloc, yloc - rad], 
      ["A", rad, rad, 0, 1, 1, xloc - 0.01, yloc - rad] 
     ]; 
    } else { 
     path = [ 
      ["M", xloc, yloc - rad], 
      ["A", rad, rad, 0, +(alpha > 180), +(!invert), x, y] 
     ]; 
     } 
    return { 
     path: path, 
     transform: "r"+rot+","+xloc+","+yloc, 
    }; 
}; 
関連する問題