Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/pan.js')
-rw-r--r--src/modules/pan.js55
1 files changed, 41 insertions, 14 deletions
diff --git a/src/modules/pan.js b/src/modules/pan.js
index 6f2cacf..4fcb416 100644
--- a/src/modules/pan.js
+++ b/src/modules/pan.js
@@ -2,6 +2,10 @@ import getComputedTransformMatrix from './utils.js';
const minDistanceThreshold = 5;
+function mainButtonPressed(e) {
+ return e.button === 0;
+}
+
function distanceBetween({ x: x1, y: y1 }, { x: x2, y: y2 }) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}
@@ -20,15 +24,26 @@ function getTranslateMatrix(startPt, movePt) {
return translateMatrix.translate(movePt.x - startPt.x, movePt.y - startPt.y);
}
-export default function (svg, el, e) {
- e.preventDefault();
+export function manualPan(gb, counter) {
+ const mtx = getComputedTransformMatrix(gb);
+ const inverseScreenCTM = gb.getScreenCTM().inverse();
- const mtx = getComputedTransformMatrix(el),
- inverseScreenCTM = el.getScreenCTM().inverse();
+ const counterRect = counter.getBoundingClientRect();
+ const mapRect = document.querySelector('object').getBoundingClientRect();
- let startPt = new DOMPoint(e.clientX, e.clientY),
- movePt = new DOMPoint(),
- isPanning = false;
+ let startPt = new DOMPoint(counterRect.x + counterRect.width / 2, counterRect.y + counterRect.height / 2);
+ let movePt = new DOMPoint(mapRect.width / 2, mapRect.height / 2);
+
+ startPt = startPt.matrixTransform(inverseScreenCTM);
+ movePt = movePt.matrixTransform(inverseScreenCTM);
+
+ gb.style.transition = 'transform 0.5s';
+ gb.style.transform = mtx.multiply(getTranslateMatrix(startPt, movePt));
+ gb.addEventListener('transitionend', () => gb.style.transition = '', { once: true });
+}
+
+export default function (el) {
+ let mtx, inverseScreenCTM, startPt, movePt, isPanning;
function pointerMove(e) {
movePt.x = e.clientX;
@@ -36,7 +51,6 @@ export default function (svg, el, e) {
if (!isPanning && minDistanceThresholdIsMet(startPt, movePt)) {
isPanning = true;
- e.target.setPointerCapture(e.pointerId);
startPt.x = e.clientX;
startPt.y = e.clientY;
@@ -54,11 +68,24 @@ export default function (svg, el, e) {
}
}
- svg.addEventListener('pointermove', pointerMove);
+ return function(e) {
+ if (!mainButtonPressed(e)) return;
+ e.preventDefault();
+ e.target.setPointerCapture(e.pointerId);
+
+ isPanning = false;
+ mtx = getComputedTransformMatrix(el);
+ inverseScreenCTM = el.getScreenCTM().inverse();
- svg.addEventListener(
- 'pointerup',
- () => svg.removeEventListener('pointermove', pointerMove),
- { once: true }
- );
+ startPt = new DOMPoint(e.clientX, e.clientY);
+ movePt = new DOMPoint();
+
+ this.addEventListener('pointermove', pointerMove);
+
+ this.addEventListener(
+ 'pointerup',
+ () => this.removeEventListener('pointermove', pointerMove),
+ { once: true }
+ );
+ }
}