2012-03-08 34 views
0

我正在阅读JSON文件和foreach元素,我正在创建一个路径和一个圆。我需要使路径与圆圈一起拖动。该路径终止于圆心的x,y坐标。我只希望路径的圆圈末端用圆圈拖动。路径的另一端是固定的。在Raphael 2.0中制作路径的一面可以拖动

我有拖动工作的圈子,但它没有为路径做任何事情。我发布的代码很糟糕,不包含定位圆的智能。我只需要帮助拖动路径的一端。我的脚本正在阅读JSON,并在正确的坐标处用圆圈和路径绘制画布。在此先感谢您的帮助。

我不回答,需要额外的插件。

<script type="text/javascript"> 

$.getJSON('jsonScript.php?view=json', function(json){ 

var start = function() { 
     this.ox = this.attr("cx"); 
     this.oy = this.attr("cy"); 
     this.animate(); 
    }, 
    move = function (dx, dy) { 
      this.attr({cx: this.ox + dx, cy: this.oy + dy}); 
    }, 
    up = function() { 
      this.animate(); 
    }; 

    var paper = Raphael(canvas.leftMargin, canvas.topMargin, canvas.width, canvas.height); 

    $.each(json, function(a , z) { 

    var circleObj = paper.circle(x, y, radius); 
    circleObj.attr({"fill":"black","stroke":"red","stroke-width":5}); 
    circleObj.node.id = jsonVar; 

    var pathObj = paper.path("M396,16L641,187"); 
    path.attr({"stroke":"#fdfdfd","stroke-width":3}).toBack(); 

    paper.set(circleObj, pathObj).drag(move, start, up) 

    }); 
}); 

</script> 

回答

2

您可以只是你的。每块内分配其附加pathcircleObject ......

$.each(json, function(a, z) { 
    ... 
    circleObject.path = path; 
}); 

这样你可以从一开始就内访问它,并移动功能...

move = function (dx, dy) { 
     this.path.attr(path)[1][1] = this.ox + dx; 
     this.path.attr(path)[1][2] = this.oy + dy; 
}, 
+0

有没有办法像遍历DOM一样遍历Raphael集合?说我有一个文本块的集合中的一个圈...我可以circle.parentNode拉斐尔集,然后找到该文本块的那部分拉斐尔集?在此先感谢您的帮助 – 2012-03-15 18:57:11

+0

您是否需要 Duopixel 2012-03-15 21:04:17

+0

nope。这将是一个不错的加号,但它并不需要 – 2012-03-16 00:32:45