Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drag.filter is awkward. #12

Closed
mbostock opened this issue May 9, 2016 · 0 comments
Closed

drag.filter is awkward. #12

mbostock opened this issue May 9, 2016 · 0 comments
Assignees

Comments

@mbostock
Copy link
Member

mbostock commented May 9, 2016

In the case of using d3-drag to drag nodes with Canvas, you might want to use drag.filter to check whether a node was clicked on before allowing the drag behavior. However, there’s no way to access whatever is computed during filter on dragstart (aside from having the caller use a global). For example, you might hit-test your circles:

function filter() {
  var node, i, x, y, n = nodes.length;
  for (i = 0; i < n; ++i) {
    node = nodes[i];
    x = d3.event.x - node.x;
    y = d3.event.y - node.y;
    if (x * x + y * y < 25) return node;
  }
}

Unfortunately, you’d have to perform this same hit-test again to determine the current node on dragstart.

Maybe drag.filter should be replaced by drag.target, which sets the target property on the event to whatever you like? But the default filter is still useful:

function filter() {
  return !d3.event.sourceEvent.button;
}

Would it be weird to do this?

function target() {
  return d3.event.sourceEvent.button ? null : this;
}

Maybe context is a better name, since there’s already a concept of the event’s target, which is the DOM element.

@mbostock mbostock self-assigned this May 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant