Skip to content

Commit

Permalink
Fix #21 - expose dx and dy on drag event.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 22, 2016
1 parent 6e9063d commit 24e9583
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ When a [drag event listener](#drag_on) is invoked, [d3.event](https://github.com
* `subject` - the drag subject, defined by [*drag*.subject](#drag_subject).
* `x` - the *x*-coordinate of the subject; see [*drag*.x](#drag_x) and [*drag*.container](#drag_container).
* `y` - the *y*-coordinate of the subject; see [*drag*.y](#drag_y) and [*drag*.container](#drag_container).
* `dx` - the change in *x*-coordinate since the previous drag event.
* `dy` - the change in *y*-coordinate since the previous drag event.
* `identifier` - the string “mouse”, or a numeric [touch identifier](https://www.w3.org/TR/touch-events/#widl-Touch-identifier).
* `active` - the number of currently active drag gestures (on start and end, not including this one).
* `sourceEvent` - the underlying input event, such as mousemove or touchmove.
Expand Down
14 changes: 7 additions & 7 deletions src/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,25 @@ export default function(started) {
}

function beforestart(id, container, point, that, args) {
var p0 = point(container, id), dx, dy,
var p = point(container, id), dx, dy,
sublisteners = listeners.copy(),
node;

if (!customEvent(new DragEvent("beforestart", node, id, active, p0[0], p0[1], sublisteners), function() {
if (!customEvent(new DragEvent("beforestart", node, id, active, p[0], p[1], sublisteners), function() {
if ((event.subject = node = subject.apply(that, args)) == null) return false;
dx = x.apply(that, args) - p0[0] || 0;
dy = y.apply(that, args) - p0[1] || 0;
dx = x.apply(that, args) - p[0] || 0;
dy = y.apply(that, args) - p[1] || 0;
return true;
})) return;

return function gesture(type) {
var p, n;
var p0 = p, n;
switch (type) {
case "start": p = p0, gestures[id] = gesture, n = active++; break;
case "start": gestures[id] = gesture, n = active++; break;
case "end": delete gestures[id], --active; // nobreak
case "drag": p = point(container, id), n = active; break;
}
customEvent(new DragEvent(type, node, id, n, p[0] + dx, p[1] + dy, sublisteners), sublisteners.apply, sublisteners, [type, that, args]);
customEvent(new DragEvent(type, node, id, n, p[0] + dx, p[1] + dy, p[0] - p0[0], p[1] - p0[1], sublisteners), sublisteners.apply, sublisteners, [type, that, args]);
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/event.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export default function DragEvent(type, subject, id, active, x, y, dispatch) {
export default function DragEvent(type, subject, id, active, x, y, dx, dy, dispatch) {
this.type = type;
this.subject = subject;
this.identifier = id;
this.active = active;
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this._ = dispatch;
}

Expand Down

0 comments on commit 24e9583

Please sign in to comment.