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