Skip to content

Commit

Permalink
Clarify which functions expect (event, …) and which expect (selection…
Browse files Browse the repository at this point in the history
…, …); and repair the calls that were mixed-up.

*event-based functions:*
- Gesture (hence event = this.args[0])
- filter
- wheelDelta

*selection-based functions:*
- extent
- transform
- schedule
- k,p (in scaleBy & scaleTo)
- x,y (in translateBy)

In addition, an event that triggers a transition (such as a zoom.dblclick) can be persisted throughout the transition as the zoomEvent.sourceEvent (fixes #202).
  • Loading branch information
Fil committed May 25, 2020
1 parent 4aade48 commit 1ec0da7
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ export default function() {
.style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
}

zoom.transform = function(collection, transform, point) {
zoom.transform = function(collection, transform, point, event) {
var selection = collection.selection ? collection.selection() : collection;
selection.property("__zoom", defaultTransform);
if (collection !== selection) {
schedule(collection, transform, point);
schedule(collection, transform, point, event);
} else {
selection.interrupt().each(function() {
gesture(this, [null, ...arguments])
Expand All @@ -95,35 +95,35 @@ export default function() {
}
};

zoom.scaleBy = function(selection, k, p) {
zoom.scaleBy = function(selection, k, p, event) {
zoom.scaleTo(selection, function() {
var k0 = this.__zoom.k,
k1 = typeof k === "function" ? k.apply(this, arguments) : k;
return k0 * k1;
}, p);
}, p, event);
};

zoom.scaleTo = function(selection, k, p) {
zoom.scaleTo = function(selection, k, p, event) {
zoom.transform(selection, function() {
var e = extent.apply(this, arguments),
t0 = this.__zoom,
p0 = p == null ? centroid(e) : typeof p === "function" ? p.apply(this, arguments) : p,
p1 = t0.invert(p0),
k1 = typeof k === "function" ? k.apply(this, arguments) : k;
return constrain(translate(scale(t0, k1), p0, p1), e, translateExtent);
}, p);
}, p, event);
};

zoom.translateBy = function(selection, x, y) {
zoom.translateBy = function(selection, x, y, event) {
zoom.transform(selection, function() {
return constrain(this.__zoom.translate(
typeof x === "function" ? x.apply(this, arguments) : x,
typeof y === "function" ? y.apply(this, arguments) : y
), extent.apply(this, arguments), translateExtent);
});
}, null, event);
};

zoom.translateTo = function(selection, x, y, p) {
zoom.translateTo = function(selection, x, y, p, event) {
zoom.transform(selection, function() {
var e = extent.apply(this, arguments),
t = this.__zoom,
Expand All @@ -132,7 +132,7 @@ export default function() {
typeof x === "function" ? -x.apply(this, arguments) : -x,
typeof y === "function" ? -y.apply(this, arguments) : -y
), e, translateExtent);
}, p);
}, p, event);
};

function scale(transform, k) {
Expand All @@ -149,14 +149,14 @@ export default function() {
return [(+extent[0][0] + +extent[1][0]) / 2, (+extent[0][1] + +extent[1][1]) / 2];
}

function schedule(transition, transform, point) {
function schedule(transition, transform, point, event) {
transition
.on("start.zoom", function() { gesture(this, arguments).start(); })
.on("interrupt.zoom end.zoom", function() { gesture(this, arguments).end(); })
.on("start.zoom", function() { gesture(this, [event, ...arguments]).start(); })
.on("interrupt.zoom end.zoom", function() { gesture(this, [event, ...arguments]).end(); })
.tween("zoom", function() {
var that = this,
args = arguments,
g = gesture(that, args),
g = gesture(that, [event, ...args]),
e = extent.apply(that, args),
p = point == null ? centroid(e) : typeof point === "function" ? point.apply(that, args) : point,
w = Math.max(e[1][0] - e[0][0], e[1][1] - e[0][1]),
Expand All @@ -179,7 +179,7 @@ export default function() {
this.that = that;
this.args = args;
this.active = 0;
this.extent = extent.apply(that, args);
this.extent = extent.apply(that, [...args].slice(1, args.length));
this.taps = 0;
}

Expand Down Expand Up @@ -299,11 +299,11 @@ export default function() {
p0 = pointer(event),
p1 = t0.invert(p0),
k1 = t0.k * (event.shiftKey ? 0.5 : 2),
t1 = constrain(translate(scale(t0, k1), p0, p1), extent.apply(this, arguments), translateExtent);
t1 = constrain(translate(scale(t0, k1), p0, p1), extent.apply(this, [...arguments].slice(1, arguments.length)), translateExtent);

noevent(event);
if (duration > 0) select(this).transition().duration(duration).call(schedule, t1, p0);
else select(this).call(zoom.transform, t1);
if (duration > 0) select(this).transition().duration(duration).call(schedule, t1, p0, event);
else select(this).call(zoom.transform, t1, p0, event);
}

function touchstarted(event) {
Expand Down

0 comments on commit 1ec0da7

Please sign in to comment.