function zoom(projection, {
scale = projection._scale === undefined
? (projection._scale = projection.scale())
: projection._scale,
scaleExtent = [0.8, 8]
} = {}) {
let v0, q0, r0, a0, tl;
const zoom = d3.zoom()
.scaleExtent(scaleExtent.map(x => x * scale))
.on("start", zoomstarted)
.on("zoom", zoomed);
function point(event, that) {
const t = d3.pointers(event, that);
if (t.length !== tl) {
tl = t.length;
if (tl > 1) a0 = Math.atan2(t[1][1] - t[0][1], t[1][0] - t[0][0]);
zoomstarted.call(that, event);
}
return tl > 1
? [
d3.mean(t, p => p[0]),
d3.mean(t, p => p[1]),
Math.atan2(t[1][1] - t[0][1], t[1][0] - t[0][0])
]
: t[0];
}
function zoomstarted(event) {
v0 = versor.cartesian(projection.invert(point(event, this)));
q0 = versor((r0 = projection.rotate()));
}
function zoomed(event) {
projection.scale(event.transform.k);
const pt = point(event, this);
const v1 = versor.cartesian(projection.rotate(r0).invert(pt));
const delta = versor.delta(v0, v1);
let q1 = versor.multiply(q0, delta);
if (pt[2]) {
const d = (pt[2] - a0) / 2;
const s = -Math.sin(d);
const c = Math.sign(Math.cos(d));
q1 = versor.multiply([Math.sqrt(1 - s * s), 0, 0, c * s], q1);
}
projection.rotate(versor.rotation(q1));
if (delta[0] < 0.7) zoomstarted.call(this, event);
}
return Object.assign(selection => selection
.property("__zoom", d3.zoomIdentity.scale(projection.scale()))
.call(zoom), {
on(type, ...options) {
return options.length
? (zoom.on(type, ...options), this)
: zoom.on(type);
}
});
}