-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoom.js
More file actions
301 lines (264 loc) · 9.03 KB
/
Copy pathzoom.js
File metadata and controls
301 lines (264 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import "../core/document";
import "../core/rebind";
import "../event/drag";
import "../event/event";
import "../event/mouse";
import "../event/touches";
import "../selection/selection";
import "../interpolate/zoom";
import "behavior";
d3.behavior.zoom = function() {
var view = {x: 0, y: 0, k: 1},
translate0, // translate when we started zooming (to avoid drift)
center, // desired position of translate0 after zooming
size = [960, 500], // viewport size; required for zoom interpolation
scaleExtent = d3_behavior_zoomInfinity,
mousedown = "mousedown.zoom",
mousemove = "mousemove.zoom",
mouseup = "mouseup.zoom",
mousewheelTimer,
touchstart = "touchstart.zoom",
touchmove = "touchmove.zoom",
touchend = "touchend.zoom",
touchtime, // time of last touchstart (to detect double-tap)
event = d3_eventDispatch(zoom, "zoomstart", "zoom", "zoomend"),
x0,
x1,
y0,
y1;
function zoom(g) {
g .on(mousedown, mousedowned)
.on(d3_behavior_zoomWheel + ".zoom", mousewheeled)
.on(mousemove, mousewheelreset)
.on("dblclick.zoom", dblclicked)
.on(touchstart, touchstarted);
}
zoom.event = function(g) {
g.each(function() {
var event_ = event.of(this, arguments),
view1 = view;
if (d3_transitionInheritId) {
d3.select(this).transition()
.each("start.zoom", function() {
view = this.__chart__ || {x: 0, y: 0, k: 1}; // pre-transition state
zoomstarted(event_);
})
.tween("zoom:zoom", function() {
var dx = size[0],
dy = size[1],
cx = dx / 2,
cy = dy / 2,
i = d3.interpolateZoom(
[(cx - view.x) / view.k, (cy - view.y) / view.k, dx / view.k],
[(cx - view1.x) / view1.k, (cy - view1.y) / view1.k, dx / view1.k]
);
return function(t) {
var l = i(t), k = dx / l[2];
this.__chart__ = view = {x: cx - l[0] * k, y: cy - l[1] * k, k: k};
zoomed(event_);
};
})
.each("end.zoom", function() {
zoomended(event_);
});
} else {
this.__chart__ = view;
zoomstarted(event_);
zoomed(event_);
zoomended(event_);
}
});
}
zoom.translate = function(_) {
if (!arguments.length) return [view.x, view.y];
view = {x: +_[0], y: +_[1], k: view.k}; // copy-on-write
rescale();
return zoom;
};
zoom.scale = function(_) {
if (!arguments.length) return view.k;
view = {x: view.x, y: view.y, k: +_}; // copy-on-write
rescale();
return zoom;
};
zoom.scaleExtent = function(_) {
if (!arguments.length) return scaleExtent;
scaleExtent = _ == null ? d3_behavior_zoomInfinity : [+_[0], +_[1]];
return zoom;
};
zoom.center = function(_) {
if (!arguments.length) return center;
center = _ && [+_[0], +_[1]];
return zoom;
};
zoom.size = function(_) {
if (!arguments.length) return size;
size = _ && [+_[0], +_[1]];
return zoom;
};
zoom.x = function(z) {
if (!arguments.length) return x1;
x1 = z;
x0 = z.copy();
view = {x: 0, y: 0, k: 1}; // copy-on-write
return zoom;
};
zoom.y = function(z) {
if (!arguments.length) return y1;
y1 = z;
y0 = z.copy();
view = {x: 0, y: 0, k: 1}; // copy-on-write
return zoom;
};
function location(p) {
return [(p[0] - view.x) / view.k, (p[1] - view.y) / view.k];
}
function point(l) {
return [l[0] * view.k + view.x, l[1] * view.k + view.y];
}
function scaleTo(s) {
view.k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s));
}
function translateTo(p, l) {
l = point(l);
view.x += p[0] - l[0];
view.y += p[1] - l[1];
}
function rescale() {
if (x1) x1.domain(x0.range().map(function(x) { return (x - view.x) / view.k; }).map(x0.invert));
if (y1) y1.domain(y0.range().map(function(y) { return (y - view.y) / view.k; }).map(y0.invert));
}
function zoomstarted(event) {
event({type: "zoomstart"});
}
function zoomed(event) {
rescale();
event({type: "zoom", scale: view.k, translate: [view.x, view.y]});
}
function zoomended(event) {
event({type: "zoomend"});
}
function mousedowned() {
var target = this,
event_ = event.of(target, arguments),
eventTarget = d3.event.target,
dragged = 0,
w = d3.select(d3_window).on(mousemove, moved).on(mouseup, ended),
l = location(d3.mouse(target)),
dragRestore = d3_event_dragSuppress();
d3_selection_interrupt.call(target);
zoomstarted(event_);
function moved() {
dragged = 1;
translateTo(d3.mouse(target), l);
zoomed(event_);
}
function ended() {
w.on(mousemove, d3_window === target ? mousewheelreset : null).on(mouseup, null);
dragRestore(dragged && d3.event.target === eventTarget);
zoomended(event_);
}
}
// These closures persist for as long as at least one touch is active.
function touchstarted() {
var target = this,
event_ = event.of(target, arguments),
locations0, // touchstart locations
distance0 = 0, // distance² between initial touches
scale0, // scale when we started touching
w = d3.select(d3_window).on(touchmove, moved).on(touchend, ended),
t = d3.select(target).on(mousedown, null).on(touchstart, started), // prevent duplicate events
dragRestore = d3_event_dragSuppress();
d3_selection_interrupt.call(target);
started();
zoomstarted(event_);
function relocate() {
var touches = d3.touches(target);
scale0 = view.k;
locations0 = {};
touches.forEach(function(t) { locations0[t.identifier] = location(t); });
return touches;
}
// Temporarily override touchstart while gesture is active.
function started() {
var now = Date.now(),
touches = relocate();
if (touches.length === 1) {
if (now - touchtime < 500) { // dbltap
var p = touches[0], l = locations0[p.identifier];
scaleTo(view.k * 2);
translateTo(p, l);
d3_eventPreventDefault();
zoomed(event_);
}
touchtime = now;
} else if (touches.length > 1) {
var p = touches[0], q = touches[1],
dx = p[0] - q[0], dy = p[1] - q[1];
distance0 = dx * dx + dy * dy;
}
}
function moved() {
var touches = d3.touches(target),
p0 = touches[0],
l0 = locations0[p0.identifier];
if (p1 = touches[1]) {
var p1, l1 = locations0[p1.identifier],
scale1 = d3.event.scale;
if (scale1 == null) {
var distance1 = (distance1 = p1[0] - p0[0]) * distance1 + (distance1 = p1[1] - p0[1]) * distance1;
scale1 = distance0 && Math.sqrt(distance1 / distance0);
}
p0 = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
l0 = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2];
scaleTo(scale1 * scale0);
}
touchtime = null;
translateTo(p0, l0);
zoomed(event_);
}
function ended() {
if (d3.event.touches.length) {
relocate(); // locations may have detached due to rotation
} else {
w.on(touchmove, null).on(touchend, null);
t.on(mousedown, mousedowned).on(touchstart, touchstarted);
dragRestore();
zoomended(event_);
}
}
}
function mousewheeled() {
var event_ = event.of(this, arguments);
if (mousewheelTimer) clearTimeout(mousewheelTimer);
else d3_selection_interrupt.call(this), zoomstarted(event_);
mousewheelTimer = setTimeout(function() { mousewheelTimer = null; zoomended(event_); }, 50);
d3_eventPreventDefault();
var point = center || d3.mouse(this);
if (!translate0) translate0 = location(point);
scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * view.k);
translateTo(point, translate0);
zoomed(event_);
}
function mousewheelreset() {
translate0 = null;
}
function dblclicked() {
var event_ = event.of(this, arguments),
p = d3.mouse(this),
l = location(p),
k = Math.log(view.k) / Math.LN2;
zoomstarted(event_);
scaleTo(Math.pow(2, d3.event.shiftKey ? Math.ceil(k) - 1 : Math.floor(k) + 1));
translateTo(p, l);
zoomed(event_);
zoomended(event_);
}
return d3.rebind(zoom, event, "on");
};
var d3_behavior_zoomInfinity = [0, Infinity]; // default scale extent
// https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/wheel
var d3_behavior_zoomDelta, d3_behavior_zoomWheel
= "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); }, "wheel")
: "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { return d3.event.wheelDelta; }, "mousewheel")
: (d3_behavior_zoomDelta = function() { return -d3.event.detail; }, "MozMousePixelScroll");