1

I have a drag behavior that looks like the following:

 var boxDrag = d3.behavior.drag()
    .origin(function (d) {
        return d;
    })
    .on("drag", drawBox);

function drawBox( /*d*/ ) {
    console.log("asdf");
}

It's only a temporary skeleton, and yet it seems to be pulling up some errors about undefined x's and such.

Errors

error

Here's the full code: http://jsfiddle.net/gamea12/5zsj853h/

gamehen
  • 324
  • 1
  • 5
  • 18
  • Your problem occurs because of `.origin()` removing that logs asdf as intended. Because it's only a skeleton, I'm not sure if other problems might be present. For that matter, I'm not sure what `.origin()` does, except that removing seems to fix the problem. – Roland Heath Oct 07 '15 at 01:53
  • `.origin()` offsets the mouse position to make it equivalent to the start of the graph itself instead of the window. I think that removing it would cause some problems later on. – gamehen Oct 07 '15 at 05:27
  • That sounds like a good reason to have it, but it might make a good starting point for your investigations - could just be returning the wrong thing. Maybe have a look [here](http://stackoverflow.com/questions/15966256/how-to-set-the-origin-drag-origin-for-drag-behavior-in-d3-javascript-library), and take a close look at whether `d.x` exists. – Roland Heath Oct 07 '15 at 05:34
  • Where you `return d;`, I think you might actually mean to `return this;` You can check this in the chrom dev-tools under sources -> jsfiddle.net -> gamea12/5zsj853h/ – Roland Heath Oct 07 '15 at 05:43

1 Answers1

1

In your fiddle, you have:

var boxDrag = d3.behavior.drag()
    .origin(function (d) {
        return d;
    })
    .on("drag", drawBox);

However, in this case, d is undefined, which is causing your error. Changing this to:

var boxDrag = d3.behavior.drag()
    .origin(function (d) {
        return this;
    })
    .on("drag", drawBox);

Should fix the problem for you.

gamehen
  • 324
  • 1
  • 5
  • 18
Roland Heath
  • 334
  • 2
  • 10