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

Zoom center #212

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ function constrain(transform, extent, translateExtent) {

The constraint function must return a [*transform*](#zoom-transforms) given the current *transform*, [viewport extent](#zoom_extent) and [translate extent](#zoom_translateExtent). The default implementation attempts to ensure that the viewport extent does not go outside the translate extent.

<a href="#zoom_center" name="zoom_center">#</a> <i>zoom</i>.<b>center</b>([<i>center</i>]) · [Source](https://github.com/d3/d3-zoom/blob/main/src/zoom.js), [Examples](https://observablehq.com/@d3/zoom-center-212)

If *center* is a function, sets the center to the specified function, and returns the zoom behavior. If *center* is an array, sets the center to a constant function that returns the specified array. If *center* is not specified, returns the current center, which defaults to the pointer’s position relative to the current DOM element:

```js
function center(event) {
return d3.pointer(event, this);
}
```

The center is passed the current event (`event`) and datum `d`, with the `this` context as the current DOM element. It must return the reference position as [*x*, *y*].

<a href="#zoom_filter" name="zoom_filter">#</a> <i>zoom</i>.<b>filter</b>([<i>filter</i>]) · [Source](https://github.com/d3/d3-zoom/blob/main/src/zoom.js)

If *filter* is specified, sets the filter to the specified function and returns the zoom behavior. If *filter* is not specified, returns the current filter, which defaults to:
Expand Down
20 changes: 16 additions & 4 deletions src/zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function defaultFilter(event) {
return (!event.ctrlKey || event.type === 'wheel') && !event.button;
}

function defaultCenter(event) {
return pointer(event, this);
}

function defaultExtent() {
var e = this;
if (e instanceof SVGElement) {
Expand Down Expand Up @@ -52,6 +56,7 @@ function defaultConstrain(transform, extent, translateExtent) {

export default function() {
var filter = defaultFilter,
center = defaultCenter,
extent = defaultExtent,
constrain = defaultConstrain,
wheelDelta = defaultWheelDelta,
Expand Down Expand Up @@ -243,6 +248,7 @@ export default function() {
if (g.wheel) {
if (g.mouse[0][0] !== p[0] || g.mouse[0][1] !== p[1]) {
g.mouse[1] = t.invert(g.mouse[0] = p);
g.mouse[2] = center.apply(this, arguments);
}
clearTimeout(g.wheel);
}
Expand All @@ -252,14 +258,14 @@ export default function() {

// Otherwise, capture the mouse point and location at the start.
else {
g.mouse = [p, t.invert(p)];
g.mouse = [p, t.invert(p), center.apply(this, arguments)];
interrupt(this);
g.start();
}

noevent(event);
g.wheel = setTimeout(wheelidled, wheelDelay);
g.zoom("mouse", constrain(translate(scale(t, k), g.mouse[0], g.mouse[1]), g.extent, translateExtent));
g.zoom("mouse", constrain(translate(scale(t, k), g.mouse[2], t.invert(g.mouse[2])), g.extent, translateExtent));

function wheelidled() {
g.wheel = null;
Expand All @@ -278,7 +284,7 @@ export default function() {

dragDisable(event.view);
nopropagation(event);
g.mouse = [p, this.__zoom.invert(p)];
g.prev = p;
interrupt(this);
g.start();

Expand All @@ -288,8 +294,10 @@ export default function() {
var dx = event.clientX - x0, dy = event.clientY - y0;
g.moved = dx * dx + dy * dy > clickDistance2;
}
var p = pointer(event, currentTarget);
g.event(event)
.zoom("mouse", constrain(translate(g.that.__zoom, g.mouse[0] = pointer(event, currentTarget), g.mouse[1]), g.extent, translateExtent));
.zoom("mouse", constrain(translate(g.that.__zoom, p, g.that.__zoom.invert(g.prev)), g.extent, translateExtent));
g.prev = p;
}

function mouseupped(event) {
Expand Down Expand Up @@ -406,6 +414,10 @@ export default function() {
return arguments.length ? (touchable = typeof _ === "function" ? _ : constant(!!_), zoom) : touchable;
};

zoom.center = function(_) {
return arguments.length ? (center = typeof _ === "function" ? _ : constant([+_[0], +_[1]]), zoom) : center;
};

zoom.extent = function(_) {
return arguments.length ? (extent = typeof _ === "function" ? _ : constant([[+_[0][0], +_[0][1]], [+_[1][0], +_[1][1]]]), zoom) : extent;
};
Expand Down