I am trying to make a circle draggable.
var drag = d3.behavior.drag();
  drag.on("drag", function(d,i) {
      console.log(d);
      d.x += d3.event.dx;
      d.y += d3.event.dy;
      //This will change the center coordinates by the delta
      d3.select(this).attr("x", d.x).attr("y", d.y);
      //This should change the upper left x and y values by the delta
      d3.select(this).attr("transform", function(d,i){
          return "translate(" + [ x,y ] + ")"
      })
  })
Here is the fiddle
It throws errors for every move on the right red circle, but how come it is saying that d is undefined in lines 3, 4, and 5?
Working fiddle: http://jsfiddle.net/6a6da/33/
The d,i arguments would usually refer to bound data, but you're not binding any data. In your case it's sufficient to work with only the event.
drag.on("drag", function() {
  d3.select(this).attr("cx", +d3.select(this).attr("cx") + d3.event.dx);
  d3.select(this).attr("cy", +d3.select(this).attr("cy") + d3.event.dy);
})l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With