Skip to content

Instantly share code, notes, and snippets.

@etiennecrb
Last active July 21, 2017 09:50
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save etiennecrb/863a08b5be3eafe7f1d61c85d724e6c4 to your computer and use it in GitHub Desktop.
d3-xyzoom demo

This plugin is a fork of d3-zoom that adds several features by separating scaling on x and y:

Scale independently along x-axis and y-axis

Constrain scale extent on zoom out to respect translate extent constraints

Apply "scale factor ratio" on user input. For instance, with a scale ratio of 0.5 on x-axis and 1 on y-axis, when user zoom with its mouse, the increase of scale factor on x-axis will only be half of the increase on y-axis.

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-dispatch'), require('d3-drag'), require('d3-interpolate'), require('d3-selection'), require('d3-transition')) :
typeof define === 'function' && define.amd ? define(['exports', 'd3-dispatch', 'd3-drag', 'd3-interpolate', 'd3-selection', 'd3-transition'], factory) :
(factory((global.d3 = global.d3 || {}),global.d3,global.d3,global.d3,global.d3,global.d3));
}(this, function (exports,d3Dispatch,d3Drag,d3Interpolate,d3Selection,d3Transition) { 'use strict';
function constant(x) {
return function() {
return x;
};
}
function ZoomEvent(target, type, transform) {
this.target = target;
this.type = type;
this.transform = transform;
}
function Transform(x, y, kx, ky) {
this.kx = kx;
this.ky = ky || kx;
this.x = x;
this.y = y;
}
Transform.prototype = {
constructor: Transform,
scale: function(kx, ky) {
ky = ky === undefined ? kx : ky;
return kx === 1 && (!ky || ky === 1) ? this : new Transform(this.x, this.y, this.kx * kx, this.ky * ky);
},
translate: function(x, y) {
return x === 0 & y === 0 ? this : new Transform(this.x + this.kx * x, this.y + this.ky * y, this.kx, this.ky);
},
apply: function(point) {
return [point[0] * this.kx + this.x, point[1] * this.ky + this.y];
},
applyX: function(x) {
return x * this.kx + this.x;
},
applyY: function(y) {
return y * this.ky + this.y;
},
invert: function(location) {
return [(location[0] - this.x) / this.kx, (location[1] - this.y) / this.ky];
},
invertX: function(x) {
return (x - this.x) / this.kx;
},
invertY: function(y) {
return (y - this.y) / this.ky;
},
rescaleX: function(x) {
return x.copy().domain(x.range().map(this.invertX, this).map(x.invert, x));
},
rescaleY: function(y) {
return y.copy().domain(y.range().map(this.invertY, this).map(y.invert, y));
},
toString: function() {
return "translate(" + this.x + "," + this.y + ") scale(" + this.kx + "," + this.ky + ")";
}
};
var identity = new Transform(0, 0, 1, 1);
transform.prototype = Transform.prototype;
function transform(node) {
return node.__zoom || identity;
}
function nopropagation() {
d3Selection.event.stopImmediatePropagation();
}
function noevent() {
d3Selection.event.preventDefault();
d3Selection.event.stopImmediatePropagation();
}
// Ignore right-click, since that should open the context menu.
function defaultFilter() {
return !d3Selection.event.button;
}
function defaultExtent() {
var e = this, w, h;
if (e instanceof SVGElement) {
e = e.ownerSVGElement || e;
w = e.width.baseVal.value;
h = e.height.baseVal.value;
} else {
if (e && e.clientWidth && e.clientHeight) {
w = e.clientWidth;
h = e.clientHeight;
} else {
throw 'd3-xyzoom failed to constrain scales because extent is undefined.';
}
}
return [[0, 0], [w, h]];
}
function defaultTransform() {
return this.__zoom || identity;
}
function zoom() {
var filter = defaultFilter,
extent = defaultExtent,
kx0 = 0,
ky0 = 0,
kx1 = Infinity,
ky1 = Infinity,
rx = 1,
ry = 1,
x0 = -Infinity,
x1 = Infinity,
y0 = -Infinity,
y1 = Infinity,
duration = 250,
interpolate = d3Interpolate.interpolateNumber,
gestures = [],
listeners = d3Dispatch.dispatch("start", "zoom", "end"),
touchstarting,
touchending,
touchDelay = 500,
wheelDelay = 150;
function zoom(selection) {
selection
.on("wheel.zoom", wheeled)
.on("mousedown.zoom", mousedowned)
.on("dblclick.zoom", dblclicked)
.on("touchstart.zoom", touchstarted)
.on("touchmove.zoom", touchmoved)
.on("touchend.zoom touchcancel.zoom", touchended)
.style("-webkit-tap-highlight-color", "rgba(0,0,0,0)")
.property("__zoom", defaultTransform);
}
zoom.transform = function(collection, transform) {
var selection = collection.selection ? collection.selection() : collection;
selection.property("__zoom", defaultTransform);
if (collection !== selection) {
schedule(collection, transform);
} else {
selection.interrupt().each(function() {
gesture(this, arguments)
.start()
.zoom(null, typeof transform === "function" ? transform.apply(this, arguments) : transform)
.end();
});
}
};
zoom.scaleBy = function(selection, kx, ky) {
zoom.scaleTo(selection, function() {
var k0 = this.__zoom.kx,
k1 = typeof kx === "function" ? kx.apply(this, arguments) : kx;
return k0 * k1;
}, function() {
var k0 = this.__zoom.ky,
k1 = typeof ky === "function" ? ky.apply(this, arguments) : ky;
return k0 * k1;
});
};
zoom.scaleTo = function(selection, kx, ky) {
zoom.transform(selection, function() {
var e = extent.apply(this, arguments),
t0 = this.__zoom,
p0 = centroid(e),
p1 = t0.invert(p0),
kx1 = typeof kx === "function" ? kx.apply(this, arguments) : kx,
ky1 = typeof ky === "function" ? ky.apply(this, arguments) : ky;
return constrain(translate(scale(t0, kx1, ky1), p0, p1), e);
});
};
zoom.translateBy = function(selection, x, y) {
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));
});
};
function scale(transform, kx, ky) {
kx = Math.max(kx0, Math.min(kx1, kx));
ky = Math.max(ky0, Math.min(ky1, ky));
return (kx === transform.kx && ky === transform.ky) ? transform : new Transform(transform.x, transform.y, kx, ky);
}
function translate(transform, p0, p1) {
var x = p0[0] - p1[0] * transform.kx, y = p0[1] - p1[1] * transform.ky;
return x === transform.x && y === transform.y ? transform : new Transform(x, y, transform.kx, transform.ky);
}
function constrain(transform, extent) {
var dx0 = transform.invertX(extent[0][0]) - x0,
dx1 = transform.invertX(extent[1][0]) - x1,
dy0 = transform.invertY(extent[0][1]) - y0,
dy1 = transform.invertY(extent[1][1]) - y1;
return transform.translate(
dx1 > dx0 ? (dx0 + dx1) / 2 : Math.min(0, dx0) || Math.max(0, dx1),
dy1 > dy0 ? (dy0 + dy1) / 2 : Math.min(0, dy0) || Math.max(0, dy1)
);
}
function centroid(extent) {
return [(+extent[0][0] + +extent[1][0]) / 2, (+extent[0][1] + +extent[1][1]) / 2];
}
function schedule(transition, transform) {
transition
.on("start.zoom", function() { gesture(this, arguments).start(); })
.on("interrupt.zoom end.zoom", function() { gesture(this, arguments).end(); })
.tween("zoom", function() {
var that = this,
args = arguments,
g = gesture(that, args),
a = that.__zoom,
b = typeof transform === "function" ? transform.apply(that, args) : transform;
var txi = interpolate(a.x, b.x);
var tyi = interpolate(a.y, b.y);
var kxi = interpolate(a.kx, b.kx);
var kyi = interpolate(a.ky, b.ky);
return function(t) {
if (t === 1) t = b; // Avoid rounding error on end.
else {
t = new Transform(txi(t), tyi(t), kxi(t), kyi(t));
}
g.zoom(null, t);
};
});
}
function gesture(that, args) {
for (var i = 0, n = gestures.length, g; i < n; ++i) {
if ((g = gestures[i]).that === that) {
return g;
}
}
return new Gesture(that, args);
}
function Gesture(that, args) {
this.that = that;
this.args = args;
this.index = -1;
this.active = 0;
this.extent = extent.apply(that, args);
}
Gesture.prototype = {
start: function() {
if (++this.active === 1) {
this.index = gestures.push(this) - 1;
this.emit("start");
}
return this;
},
zoom: function(key, transform) {
if (this.mouse && key !== "mouse") this.mouse[1] = transform.invert(this.mouse[0]);
if (this.touch0 && key !== "touch") this.touch0[1] = transform.invert(this.touch0[0]);
if (this.touch1 && key !== "touch") this.touch1[1] = transform.invert(this.touch1[0]);
this.that.__zoom = transform;
this.emit("zoom");
return this;
},
end: function() {
if (--this.active === 0) {
gestures.splice(this.index, 1);
this.index = -1;
this.emit("end");
}
return this;
},
emit: function(type) {
d3Selection.customEvent(new ZoomEvent(zoom, type, this.that.__zoom), listeners.apply, listeners, [type, this.that, this.args]);
}
};
function wheeled() {
if (!filter.apply(this, arguments)) return;
var g = gesture(this, arguments);
var t = this.__zoom;
var kx = Math.max(kx0, Math.min(kx1, t.kx * (1 + rx * (-1 + Math.pow(2, -d3Selection.event.deltaY * (d3Selection.event.deltaMode ? 120 : 1) / 500)))));
var ky = Math.max(ky0, Math.min(ky1, t.ky * (1 + ry * (-1 + Math.pow(2, -d3Selection.event.deltaY * (d3Selection.event.deltaMode ? 120 : 1) / 500)))));
var p = d3Selection.mouse(this);
// If a scale factor has reached scale extend, sync its value with the other one
if (t.kx === kx0) {
kx = ky >= kx0 ? kx : kx0;
}
if (t.kx === kx1) {
kx = ky <= kx1 ? kx : kx1;
}
if (t.ky === ky0) {
ky = kx >= ky0 ? ky : ky0;
}
if (t.ky === ky1) {
ky = kx <= ky1 ? ky : ky1;
}
// If the mouse is in the same location as before, reuse it.
// If there were recent wheel events, reset the wheel idle timeout.
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);
}
clearTimeout(g.wheel);
}
// If this wheel event won’t trigger a transform change, ignore it.
else if (t.kx === ky && t.ky === kx) return;
// Otherwise, capture the mouse point and location at the start.
else {
g.mouse = [p, t.invert(p)];
d3Transition.interrupt(this);
g.start();
}
noevent();
g.wheel = setTimeout(wheelidled, wheelDelay);
g.zoom("mouse", constrain(translate(scale(t, kx, ky), g.mouse[0], g.mouse[1]), g.extent));
function wheelidled() {
g.wheel = null;
g.end();
}
}
function mousedowned() {
if (touchending || !filter.apply(this, arguments)) return;
var g = gesture(this, arguments),
v = d3Selection.select(d3Selection.event.view).on("mousemove.zoom", mousemoved, true).on("mouseup.zoom", mouseupped, true),
p = d3Selection.mouse(this);
d3Drag.dragDisable(d3Selection.event.view);
nopropagation();
g.mouse = [p, this.__zoom.invert(p)];
d3Transition.interrupt(this);
g.start();
function mousemoved() {
noevent();
g.moved = true;
g.zoom("mouse", constrain(translate(g.that.__zoom, g.mouse[0] = d3Selection.mouse(g.that), g.mouse[1]), g.extent));
}
function mouseupped() {
v.on("mousemove.zoom mouseup.zoom", null);
d3Drag.dragEnable(d3Selection.event.view, g.moved);
noevent();
g.end();
}
}
function dblclicked() {
if (!filter.apply(this, arguments)) return;
var t0 = this.__zoom,
p0 = d3Selection.mouse(this),
p1 = t0.invert(p0),
kx1 = t0.kx * (1 + rx * (-1 + (d3Selection.event.shiftKey ? 0.5 : 2))),
ky1 = t0.ky * (1 + ry * (-1 + (d3Selection.event.shiftKey ? 0.5 : 2))),
t1 = constrain(translate(scale(t0, kx1, ky1), p0, p1), extent.apply(this, arguments));
noevent();
if (duration > 0) d3Selection.select(this).transition().duration(duration).call(schedule, t1, p0);
else d3Selection.select(this).call(zoom.transform, t1);
}
function touchstarted() {
if (!filter.apply(this, arguments)) return;
var g = gesture(this, arguments),
touches = d3Selection.event.changedTouches,
n = touches.length, i, t, p;
nopropagation();
for (i = 0; i < n; ++i) {
t = touches[i], p = d3Selection.touch(this, touches, t.identifier);
p = [p, this.__zoom.invert(p), t.identifier];
if (!g.touch0) g.touch0 = p;
else if (!g.touch1) g.touch1 = p;
}
// If this is a dbltap, reroute to the (optional) dblclick.zoom handler.
if (touchstarting) {
touchstarting = clearTimeout(touchstarting);
if (!g.touch1) {
g.end();
p = d3Selection.select(this).on("dblclick.zoom");
if (p) p.apply(this, arguments);
return;
}
}
if (d3Selection.event.touches.length === n) {
touchstarting = setTimeout(function() { touchstarting = null; }, touchDelay);
d3Transition.interrupt(this);
g.start();
}
}
function touchmoved() {
var g = gesture(this, arguments),
touches = d3Selection.event.changedTouches,
n = touches.length, i, t, p, l;
noevent();
if (touchstarting) touchstarting = clearTimeout(touchstarting);
for (i = 0; i < n; ++i) {
t = touches[i], p = d3Selection.touch(this, touches, t.identifier);
if (g.touch0 && g.touch0[2] === t.identifier) g.touch0[0] = p;
else if (g.touch1 && g.touch1[2] === t.identifier) g.touch1[0] = p;
}
t = g.that.__zoom;
if (g.touch1) {
var p0 = g.touch0[0], l0 = g.touch0[1],
p1 = g.touch1[0], l1 = g.touch1[1],
dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp,
dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
t = scale(t, Math.sqrt(dp / dl), Math.sqrt(dp / dl));
p = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
l = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2];
}
else if (g.touch0) p = g.touch0[0], l = g.touch0[1];
else return;
g.zoom("touch", constrain(translate(t, p, l), g.extent));
}
function touchended() {
var g = gesture(this, arguments),
touches = d3Selection.event.changedTouches,
n = touches.length, i, t;
nopropagation();
if (touchending) clearTimeout(touchending);
touchending = setTimeout(function() { touchending = null; }, touchDelay);
for (i = 0; i < n; ++i) {
t = touches[i];
if (g.touch0 && g.touch0[2] === t.identifier) delete g.touch0;
else if (g.touch1 && g.touch1[2] === t.identifier) delete g.touch1;
}
if (g.touch1 && !g.touch0) g.touch0 = g.touch1, delete g.touch1;
if (!g.touch0) g.end();
}
function constrainScaleExtent() {
kx0 = x1 !== x0 ? Math.max(kx0, (extent()[1][0] - extent()[0][0]) / (x1 - x0)) : Infinity;
ky0 = y1 !== y0 ? Math.max(ky0, (extent()[1][1] - extent()[0][1]) / (y1 - y0)) : Infinity;
}
zoom.filter = function(_) {
return arguments.length ? (filter = typeof _ === "function" ? _ : constant(!!_), zoom) : filter;
};
zoom.extent = function(_) {
return arguments.length ? (extent = typeof _ === "function" ? _ : constant([[+_[0][0], +_[0][1]], [+_[1][0], +_[1][1]]]), constrainScaleExtent(), zoom) : extent;
};
zoom.scaleExtent = function(_) {
if (arguments.length) {
if (Array.isArray(_[0])) {
kx0 = +_[0][0];
kx1 = +_[0][1];
ky0 = +_[1][0];
ky1 = +_[1][1];
} else {
kx0 = +_[0];
kx1 = +_[1];
ky0 = kx0;
ky1 = kx1;
}
constrainScaleExtent();
return zoom;
}
return [[kx0, kx1], [ky0, ky1]];
};
zoom.scaleRatio = function(_) {
return arguments.length ? (rx = +_[0], ry = +_[1], zoom) : [rx, ry];
};
zoom.translateExtent = function(_) {
return arguments.length ? (x0 = +_[0][0], x1 = +_[1][0], y0 = +_[0][1], y1 = +_[1][1], constrainScaleExtent(), zoom) : [[x0, y0], [x1, y1]];
};
zoom.duration = function(_) {
return arguments.length ? (duration = +_, zoom) : duration;
};
zoom.interpolate = function(_) {
return arguments.length ? (interpolate = _, zoom) : interpolate;
};
zoom.on = function() {
var value = listeners.on.apply(listeners, arguments);
return value === listeners ? zoom : value;
};
return zoom;
}
exports.xyzoom = zoom;
exports.xyzoomTransform = transform;
exports.xyzoomIdentity = identity;
Object.defineProperty(exports, '__esModule', { value: true });
}));
<!DOCTYPE html>
<!--suppress ALL -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="d3-xyzoom.js"></script>
<style>
.axis path {
display: none;
}
.axis line {
stroke-opacity: 0.1;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<div><button>Reset</button></div>
<svg width="960" height="500">
</svg>
<script>
var svg = d3.select('svg');
var width = +svg.attr('width');
var height = +svg.attr('height');
var x = d3.scaleLinear()
.domain([0, 100])
.range([-1, width + 1]);
var y = d3.scaleLinear()
.domain([0, 100])
.range([-1, height + 1]);
var ymax = 500;
var zoom = d3.xyzoom()
.extent([[x.range()[0], y.range()[0]], [x.range()[1], y.range()[1]]])
.scaleExtent([[1, 10], [0, 15]])
.scaleRatio([0.5, 1])
.translateExtent([[x.range()[0], y.range()[0]], [x.range()[1], y(ymax)]])
.on('zoom', zoomed);
var xAxis = d3.axisBottom(x)
.ticks((width + 2) / (height + 2) * 10)
.tickSize(height)
.tickPadding(8 - height);
var yAxis = d3.axisRight(y)
.ticks(10)
.tickSize(width)
.tickPadding(8 - width);
var gX = svg.append('g')
.attr('class', 'axis axis--x')
.call(xAxis);
var gY = svg.append('g')
.attr('class', 'axis axis--y')
.call(yAxis);
d3.select('button')
.on('click', reset);
svg.call(zoom);
function zoomed() {
gX.call(xAxis.scale(d3.event.transform.rescaleX(x)));
gY.call(yAxis.scale(d3.event.transform.rescaleY(y)));
}
function reset() {
svg.transition()
.duration(750)
.call(zoom.transform, d3.xyzoomIdentity);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment