making one side of a path draggable in Raphael 2.0

2019-08-17 19:06发布

I am reading a JSON file and foreach element I am creating a path and a circle. I need to make the path drag with the circle. The path terminates at the exact x,y coordinates of the circle center. I only want the circle end of the path to drag with the circle. The other end of the path is fixed.

I have drag working for the circles but it is not doing anything for the paths. I have posted code that is dumbed down and does not contain the intelligence for positioning circles. I only need help with dragging one end of the path. My script is reading the JSON fine and painting the canvas with circles and paths at the correct coordinates. Thanks in advance for your help.

I do not answers that require an additional plug in please.

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

标签: raphael
1条回答
Deceive 欺骗
2楼-- · 2019-08-17 20:00

You can attach path to circleObject by just assigning it within your .each block...

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

This way you can access it from within the start, up and move functions...

 move = function (dx, dy) {
         this.path.attr(path)[1][1] = this.ox + dx;
         this.path.attr(path)[1][2] = this.oy + dy;
 },
查看更多
登录 后发表回答