source: node_modules/d3-zoom/src/zoom.js@ a762898

Last change on this file since a762898 was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 15.3 KB
Line 
1import {dispatch} from "d3-dispatch";
2import {dragDisable, dragEnable} from "d3-drag";
3import {interpolateZoom} from "d3-interpolate";
4import {select, pointer} from "d3-selection";
5import {interrupt} from "d3-transition";
6import constant from "./constant.js";
7import ZoomEvent from "./event.js";
8import {Transform, identity} from "./transform.js";
9import noevent, {nopropagation} from "./noevent.js";
10
11// Ignore right-click, since that should open the context menu.
12// except for pinch-to-zoom, which is sent as a wheel+ctrlKey event
13function defaultFilter(event) {
14 return (!event.ctrlKey || event.type === 'wheel') && !event.button;
15}
16
17function defaultExtent() {
18 var e = this;
19 if (e instanceof SVGElement) {
20 e = e.ownerSVGElement || e;
21 if (e.hasAttribute("viewBox")) {
22 e = e.viewBox.baseVal;
23 return [[e.x, e.y], [e.x + e.width, e.y + e.height]];
24 }
25 return [[0, 0], [e.width.baseVal.value, e.height.baseVal.value]];
26 }
27 return [[0, 0], [e.clientWidth, e.clientHeight]];
28}
29
30function defaultTransform() {
31 return this.__zoom || identity;
32}
33
34function defaultWheelDelta(event) {
35 return -event.deltaY * (event.deltaMode === 1 ? 0.05 : event.deltaMode ? 1 : 0.002) * (event.ctrlKey ? 10 : 1);
36}
37
38function defaultTouchable() {
39 return navigator.maxTouchPoints || ("ontouchstart" in this);
40}
41
42function defaultConstrain(transform, extent, translateExtent) {
43 var dx0 = transform.invertX(extent[0][0]) - translateExtent[0][0],
44 dx1 = transform.invertX(extent[1][0]) - translateExtent[1][0],
45 dy0 = transform.invertY(extent[0][1]) - translateExtent[0][1],
46 dy1 = transform.invertY(extent[1][1]) - translateExtent[1][1];
47 return transform.translate(
48 dx1 > dx0 ? (dx0 + dx1) / 2 : Math.min(0, dx0) || Math.max(0, dx1),
49 dy1 > dy0 ? (dy0 + dy1) / 2 : Math.min(0, dy0) || Math.max(0, dy1)
50 );
51}
52
53export default function() {
54 var filter = defaultFilter,
55 extent = defaultExtent,
56 constrain = defaultConstrain,
57 wheelDelta = defaultWheelDelta,
58 touchable = defaultTouchable,
59 scaleExtent = [0, Infinity],
60 translateExtent = [[-Infinity, -Infinity], [Infinity, Infinity]],
61 duration = 250,
62 interpolate = interpolateZoom,
63 listeners = dispatch("start", "zoom", "end"),
64 touchstarting,
65 touchfirst,
66 touchending,
67 touchDelay = 500,
68 wheelDelay = 150,
69 clickDistance2 = 0,
70 tapDistance = 10;
71
72 function zoom(selection) {
73 selection
74 .property("__zoom", defaultTransform)
75 .on("wheel.zoom", wheeled, {passive: false})
76 .on("mousedown.zoom", mousedowned)
77 .on("dblclick.zoom", dblclicked)
78 .filter(touchable)
79 .on("touchstart.zoom", touchstarted)
80 .on("touchmove.zoom", touchmoved)
81 .on("touchend.zoom touchcancel.zoom", touchended)
82 .style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
83 }
84
85 zoom.transform = function(collection, transform, point, event) {
86 var selection = collection.selection ? collection.selection() : collection;
87 selection.property("__zoom", defaultTransform);
88 if (collection !== selection) {
89 schedule(collection, transform, point, event);
90 } else {
91 selection.interrupt().each(function() {
92 gesture(this, arguments)
93 .event(event)
94 .start()
95 .zoom(null, typeof transform === "function" ? transform.apply(this, arguments) : transform)
96 .end();
97 });
98 }
99 };
100
101 zoom.scaleBy = function(selection, k, p, event) {
102 zoom.scaleTo(selection, function() {
103 var k0 = this.__zoom.k,
104 k1 = typeof k === "function" ? k.apply(this, arguments) : k;
105 return k0 * k1;
106 }, p, event);
107 };
108
109 zoom.scaleTo = function(selection, k, p, event) {
110 zoom.transform(selection, function() {
111 var e = extent.apply(this, arguments),
112 t0 = this.__zoom,
113 p0 = p == null ? centroid(e) : typeof p === "function" ? p.apply(this, arguments) : p,
114 p1 = t0.invert(p0),
115 k1 = typeof k === "function" ? k.apply(this, arguments) : k;
116 return constrain(translate(scale(t0, k1), p0, p1), e, translateExtent);
117 }, p, event);
118 };
119
120 zoom.translateBy = function(selection, x, y, event) {
121 zoom.transform(selection, function() {
122 return constrain(this.__zoom.translate(
123 typeof x === "function" ? x.apply(this, arguments) : x,
124 typeof y === "function" ? y.apply(this, arguments) : y
125 ), extent.apply(this, arguments), translateExtent);
126 }, null, event);
127 };
128
129 zoom.translateTo = function(selection, x, y, p, event) {
130 zoom.transform(selection, function() {
131 var e = extent.apply(this, arguments),
132 t = this.__zoom,
133 p0 = p == null ? centroid(e) : typeof p === "function" ? p.apply(this, arguments) : p;
134 return constrain(identity.translate(p0[0], p0[1]).scale(t.k).translate(
135 typeof x === "function" ? -x.apply(this, arguments) : -x,
136 typeof y === "function" ? -y.apply(this, arguments) : -y
137 ), e, translateExtent);
138 }, p, event);
139 };
140
141 function scale(transform, k) {
142 k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], k));
143 return k === transform.k ? transform : new Transform(k, transform.x, transform.y);
144 }
145
146 function translate(transform, p0, p1) {
147 var x = p0[0] - p1[0] * transform.k, y = p0[1] - p1[1] * transform.k;
148 return x === transform.x && y === transform.y ? transform : new Transform(transform.k, x, y);
149 }
150
151 function centroid(extent) {
152 return [(+extent[0][0] + +extent[1][0]) / 2, (+extent[0][1] + +extent[1][1]) / 2];
153 }
154
155 function schedule(transition, transform, point, event) {
156 transition
157 .on("start.zoom", function() { gesture(this, arguments).event(event).start(); })
158 .on("interrupt.zoom end.zoom", function() { gesture(this, arguments).event(event).end(); })
159 .tween("zoom", function() {
160 var that = this,
161 args = arguments,
162 g = gesture(that, args).event(event),
163 e = extent.apply(that, args),
164 p = point == null ? centroid(e) : typeof point === "function" ? point.apply(that, args) : point,
165 w = Math.max(e[1][0] - e[0][0], e[1][1] - e[0][1]),
166 a = that.__zoom,
167 b = typeof transform === "function" ? transform.apply(that, args) : transform,
168 i = interpolate(a.invert(p).concat(w / a.k), b.invert(p).concat(w / b.k));
169 return function(t) {
170 if (t === 1) t = b; // Avoid rounding error on end.
171 else { var l = i(t), k = w / l[2]; t = new Transform(k, p[0] - l[0] * k, p[1] - l[1] * k); }
172 g.zoom(null, t);
173 };
174 });
175 }
176
177 function gesture(that, args, clean) {
178 return (!clean && that.__zooming) || new Gesture(that, args);
179 }
180
181 function Gesture(that, args) {
182 this.that = that;
183 this.args = args;
184 this.active = 0;
185 this.sourceEvent = null;
186 this.extent = extent.apply(that, args);
187 this.taps = 0;
188 }
189
190 Gesture.prototype = {
191 event: function(event) {
192 if (event) this.sourceEvent = event;
193 return this;
194 },
195 start: function() {
196 if (++this.active === 1) {
197 this.that.__zooming = this;
198 this.emit("start");
199 }
200 return this;
201 },
202 zoom: function(key, transform) {
203 if (this.mouse && key !== "mouse") this.mouse[1] = transform.invert(this.mouse[0]);
204 if (this.touch0 && key !== "touch") this.touch0[1] = transform.invert(this.touch0[0]);
205 if (this.touch1 && key !== "touch") this.touch1[1] = transform.invert(this.touch1[0]);
206 this.that.__zoom = transform;
207 this.emit("zoom");
208 return this;
209 },
210 end: function() {
211 if (--this.active === 0) {
212 delete this.that.__zooming;
213 this.emit("end");
214 }
215 return this;
216 },
217 emit: function(type) {
218 var d = select(this.that).datum();
219 listeners.call(
220 type,
221 this.that,
222 new ZoomEvent(type, {
223 sourceEvent: this.sourceEvent,
224 target: zoom,
225 type,
226 transform: this.that.__zoom,
227 dispatch: listeners
228 }),
229 d
230 );
231 }
232 };
233
234 function wheeled(event, ...args) {
235 if (!filter.apply(this, arguments)) return;
236 var g = gesture(this, args).event(event),
237 t = this.__zoom,
238 k = Math.max(scaleExtent[0], Math.min(scaleExtent[1], t.k * Math.pow(2, wheelDelta.apply(this, arguments)))),
239 p = pointer(event);
240
241 // If the mouse is in the same location as before, reuse it.
242 // If there were recent wheel events, reset the wheel idle timeout.
243 if (g.wheel) {
244 if (g.mouse[0][0] !== p[0] || g.mouse[0][1] !== p[1]) {
245 g.mouse[1] = t.invert(g.mouse[0] = p);
246 }
247 clearTimeout(g.wheel);
248 }
249
250 // If this wheel event won’t trigger a transform change, ignore it.
251 else if (t.k === k) return;
252
253 // Otherwise, capture the mouse point and location at the start.
254 else {
255 g.mouse = [p, t.invert(p)];
256 interrupt(this);
257 g.start();
258 }
259
260 noevent(event);
261 g.wheel = setTimeout(wheelidled, wheelDelay);
262 g.zoom("mouse", constrain(translate(scale(t, k), g.mouse[0], g.mouse[1]), g.extent, translateExtent));
263
264 function wheelidled() {
265 g.wheel = null;
266 g.end();
267 }
268 }
269
270 function mousedowned(event, ...args) {
271 if (touchending || !filter.apply(this, arguments)) return;
272 var currentTarget = event.currentTarget,
273 g = gesture(this, args, true).event(event),
274 v = select(event.view).on("mousemove.zoom", mousemoved, true).on("mouseup.zoom", mouseupped, true),
275 p = pointer(event, currentTarget),
276 x0 = event.clientX,
277 y0 = event.clientY;
278
279 dragDisable(event.view);
280 nopropagation(event);
281 g.mouse = [p, this.__zoom.invert(p)];
282 interrupt(this);
283 g.start();
284
285 function mousemoved(event) {
286 noevent(event);
287 if (!g.moved) {
288 var dx = event.clientX - x0, dy = event.clientY - y0;
289 g.moved = dx * dx + dy * dy > clickDistance2;
290 }
291 g.event(event)
292 .zoom("mouse", constrain(translate(g.that.__zoom, g.mouse[0] = pointer(event, currentTarget), g.mouse[1]), g.extent, translateExtent));
293 }
294
295 function mouseupped(event) {
296 v.on("mousemove.zoom mouseup.zoom", null);
297 dragEnable(event.view, g.moved);
298 noevent(event);
299 g.event(event).end();
300 }
301 }
302
303 function dblclicked(event, ...args) {
304 if (!filter.apply(this, arguments)) return;
305 var t0 = this.__zoom,
306 p0 = pointer(event.changedTouches ? event.changedTouches[0] : event, this),
307 p1 = t0.invert(p0),
308 k1 = t0.k * (event.shiftKey ? 0.5 : 2),
309 t1 = constrain(translate(scale(t0, k1), p0, p1), extent.apply(this, args), translateExtent);
310
311 noevent(event);
312 if (duration > 0) select(this).transition().duration(duration).call(schedule, t1, p0, event);
313 else select(this).call(zoom.transform, t1, p0, event);
314 }
315
316 function touchstarted(event, ...args) {
317 if (!filter.apply(this, arguments)) return;
318 var touches = event.touches,
319 n = touches.length,
320 g = gesture(this, args, event.changedTouches.length === n).event(event),
321 started, i, t, p;
322
323 nopropagation(event);
324 for (i = 0; i < n; ++i) {
325 t = touches[i], p = pointer(t, this);
326 p = [p, this.__zoom.invert(p), t.identifier];
327 if (!g.touch0) g.touch0 = p, started = true, g.taps = 1 + !!touchstarting;
328 else if (!g.touch1 && g.touch0[2] !== p[2]) g.touch1 = p, g.taps = 0;
329 }
330
331 if (touchstarting) touchstarting = clearTimeout(touchstarting);
332
333 if (started) {
334 if (g.taps < 2) touchfirst = p[0], touchstarting = setTimeout(function() { touchstarting = null; }, touchDelay);
335 interrupt(this);
336 g.start();
337 }
338 }
339
340 function touchmoved(event, ...args) {
341 if (!this.__zooming) return;
342 var g = gesture(this, args).event(event),
343 touches = event.changedTouches,
344 n = touches.length, i, t, p, l;
345
346 noevent(event);
347 for (i = 0; i < n; ++i) {
348 t = touches[i], p = pointer(t, this);
349 if (g.touch0 && g.touch0[2] === t.identifier) g.touch0[0] = p;
350 else if (g.touch1 && g.touch1[2] === t.identifier) g.touch1[0] = p;
351 }
352 t = g.that.__zoom;
353 if (g.touch1) {
354 var p0 = g.touch0[0], l0 = g.touch0[1],
355 p1 = g.touch1[0], l1 = g.touch1[1],
356 dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp,
357 dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
358 t = scale(t, Math.sqrt(dp / dl));
359 p = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
360 l = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2];
361 }
362 else if (g.touch0) p = g.touch0[0], l = g.touch0[1];
363 else return;
364
365 g.zoom("touch", constrain(translate(t, p, l), g.extent, translateExtent));
366 }
367
368 function touchended(event, ...args) {
369 if (!this.__zooming) return;
370 var g = gesture(this, args).event(event),
371 touches = event.changedTouches,
372 n = touches.length, i, t;
373
374 nopropagation(event);
375 if (touchending) clearTimeout(touchending);
376 touchending = setTimeout(function() { touchending = null; }, touchDelay);
377 for (i = 0; i < n; ++i) {
378 t = touches[i];
379 if (g.touch0 && g.touch0[2] === t.identifier) delete g.touch0;
380 else if (g.touch1 && g.touch1[2] === t.identifier) delete g.touch1;
381 }
382 if (g.touch1 && !g.touch0) g.touch0 = g.touch1, delete g.touch1;
383 if (g.touch0) g.touch0[1] = this.__zoom.invert(g.touch0[0]);
384 else {
385 g.end();
386 // If this was a dbltap, reroute to the (optional) dblclick.zoom handler.
387 if (g.taps === 2) {
388 t = pointer(t, this);
389 if (Math.hypot(touchfirst[0] - t[0], touchfirst[1] - t[1]) < tapDistance) {
390 var p = select(this).on("dblclick.zoom");
391 if (p) p.apply(this, arguments);
392 }
393 }
394 }
395 }
396
397 zoom.wheelDelta = function(_) {
398 return arguments.length ? (wheelDelta = typeof _ === "function" ? _ : constant(+_), zoom) : wheelDelta;
399 };
400
401 zoom.filter = function(_) {
402 return arguments.length ? (filter = typeof _ === "function" ? _ : constant(!!_), zoom) : filter;
403 };
404
405 zoom.touchable = function(_) {
406 return arguments.length ? (touchable = typeof _ === "function" ? _ : constant(!!_), zoom) : touchable;
407 };
408
409 zoom.extent = function(_) {
410 return arguments.length ? (extent = typeof _ === "function" ? _ : constant([[+_[0][0], +_[0][1]], [+_[1][0], +_[1][1]]]), zoom) : extent;
411 };
412
413 zoom.scaleExtent = function(_) {
414 return arguments.length ? (scaleExtent[0] = +_[0], scaleExtent[1] = +_[1], zoom) : [scaleExtent[0], scaleExtent[1]];
415 };
416
417 zoom.translateExtent = function(_) {
418 return arguments.length ? (translateExtent[0][0] = +_[0][0], translateExtent[1][0] = +_[1][0], translateExtent[0][1] = +_[0][1], translateExtent[1][1] = +_[1][1], zoom) : [[translateExtent[0][0], translateExtent[0][1]], [translateExtent[1][0], translateExtent[1][1]]];
419 };
420
421 zoom.constrain = function(_) {
422 return arguments.length ? (constrain = _, zoom) : constrain;
423 };
424
425 zoom.duration = function(_) {
426 return arguments.length ? (duration = +_, zoom) : duration;
427 };
428
429 zoom.interpolate = function(_) {
430 return arguments.length ? (interpolate = _, zoom) : interpolate;
431 };
432
433 zoom.on = function() {
434 var value = listeners.on.apply(listeners, arguments);
435 return value === listeners ? zoom : value;
436 };
437
438 zoom.clickDistance = function(_) {
439 return arguments.length ? (clickDistance2 = (_ = +_) * _, zoom) : Math.sqrt(clickDistance2);
440 };
441
442 zoom.tapDistance = function(_) {
443 return arguments.length ? (tapDistance = +_, zoom) : tapDistance;
444 };
445
446 return zoom;
447}
Note: See TracBrowser for help on using the repository browser.