d3 / d3-drag

Drag and drop SVG, HTML or Canvas using mouse or touch input.

Home Page:https://d3js.org/d3-drag

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use distance threshold when suppressing click.

flekschas opened this issue · comments

Currently, the drag handler suppresses all click events by checking for the mousemoving variable that always validates to true because as soon as the mousedown event fires, D3 starts listening to the mousemove.drag event, which immediately starts firing without any actual cursor move.

See lines 44, 53, and 59

By default it might be okay to suppress all click events but sometimes it's desirable to actually trigger a click event when the cursor did not move or moved within a tolerable distance only (e.g., 1 or 2 pixel).

Currently I am doing something like the following to work around the click suppression:

let dX, dY;

drag.on('start', () => {
  dX = d3.event.x;
  dY = d3.event.y;
});

drag.on('end', () => {
  dX = Math.abs(dX - d3.event.x);
  dY = Math.abs(dY - d3.event.y);
  if (Math.max(dX, dY) <= (clickTolerance || 0)) {
    d3.select(window).on('click.drag', null);
  }
});

or

let dX, dY;

drag.on('start', () => {
  dX = 0;
  dY = 0;
});

drag.on('drag', () => {
  if (!mouseMoved) {
    dX += Math.abs(d3.event.dx);
    dY += Math.abs(d3.event.dy);
    if (Math.max(dX, dY) > (clickTolerance || 0)) {
      mouseMoved = true;
    }
  }
});

drag.on('end', () => {
  if (!mouseMoved) {
    d3.select(window).on('click.drag', null);
  }
  mouseMoved = false;
});

It works but it feels kind of hacky. Or is there another, more elegant way to achieve the same thing?

I am not sure if my approach adds lots of overhead but I would advocate to let the user decide whether he/she wants all click events to be suppressed.

as soon as the mousedown event fires, D3 starts listening to the mousemove.drag event, which immediately starts firing without any actual cursor move.

I’m going to be a tiny bit pedantic, but bear with me, as your point is taken.

Yes, d3-drag listens for a mousemove following mousedown, and interprets that as a non-empty drag gesture and thus cancels the subsequent click. But no, in general, a mousemove event does not follow a mousedown event unless the mouse also moved during the button press. Yet this behavior may be browser- or platform-specific, and is certainly device-specific: given a sensitive input device, pressing the mouse button is more likely to cause a small amount of mouse movement which then triggers mousemove events.

On the devices and platforms I primarily use (macOS), the current implementation works fine. But it is brittle in that any degree of mouse movement is interpreted as a non-empty drag gesture, and given the analog (or uncertain) nature of input, it would be more robust to have a distance threshold.

I’d compute the threshold based on the squared Euclidean distance, not that it makes much difference:

let d2;

drag.on('start', () => {
  d2 = 0;
});

drag.on('drag', () => {
  d2 += d3.event.dx * d3.event.dx + d3.event.dy * d3.event.dy;
});

drag.on('end', () => {
  if (d2 < clickTolerance) {
    d3.select(window).on('click.drag', null);
  }
});

Since you can have multiple active drag gestures, the d2 state here should be tracked on a per-gesture basis.

I have to confess I based my observations on one setup only (Mac OS X 10.11.6 and Google Chrome). For whatever reason I experienced constant mousemove event firing but I am unable to reproduce this. As long as I can't figure out what caused this weird behavior I assume this is no issue (or simply a technical issue on my end).

Sorry for taking up your time.

I’m going to leave this open because I think it’s still a good enhancement to use a distance threshold for suppressing click.

Fixed in 1.1.0.