最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why isnt `d` defined in d3.behavior.drag() - Stack Overflow

programmeradmin2浏览0评论

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 e it is saying that d is undefined in lines 3, 4, and 5?

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 e it is saying that d is undefined in lines 3, 4, and 5?

Share Improve this question asked Jun 21, 2013 at 21:30 chris Frisinachris Frisina 19.3k23 gold badges91 silver badges172 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Working fiddle: http://jsfiddle/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

Updated: http://jsfiddle/6a6da/32/

define d as var d = d3.event;.

Later on, though, x and y are undefined here: return "translate(" + [ x,y ] + ")". I'm not sure what you want to do there.

发布评论

评论列表(0)

  1. 暂无评论