Web Dev Solutions

Catalin Mititiuc

From bbf65197db8da4f360e3d460d4eb74d947525622 Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Mon, 22 Apr 2024 20:58:17 -0700 Subject: Extract panzoom logic into separate module --- src/index.js | 46 ++-------------------------------------------- 1 file changed, 2 insertions(+), 44 deletions(-) (limited to 'src/index.js') diff --git a/src/index.js b/src/index.js index 63925b1..89893b6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,48 +1,6 @@ -import { pan, zoom } from 'svg-pan-zoom'; +import PanZoom from './modules/panzoom.js'; import Game from './modules/game.js'; -const PanZoom = new function () { - const storageKey = 'pan-zoom'; - - function storePanZoomVal(transformMatrix) { - localStorage.setItem(storageKey, transformMatrix); - } - - function observePanZoomChanges(svg) { - const observer = new MutationObserver(() => storePanZoomVal(svg.style.transform)); - - observer.observe(svg, { attributeFilter: ['style'] }); - } - - function restorePanZoomVal(svg) { - const storedPanZoomVal = localStorage.getItem(storageKey); - - if (storedPanZoomVal) { - svg.style.transform = storedPanZoomVal; - } - } - - function addEventListeners(svg) { - svg.addEventListener('wheel', e => { - e.preventDefault(); - - zoom(svg, e); - }, { passive: false }); - - svg.addEventListener('pointerdown', e => { - e.preventDefault(); - - pan(svg, e); - }, { passive: false }); - } - - this.start = function (svg) { - restorePanZoomVal(svg); - addEventListeners(svg); - observePanZoomChanges(svg); - }; -}; - const RecordSheet = new function () { this.unSelect = function () { let selected = this.getSelected(); @@ -88,7 +46,7 @@ window.addEventListener('load', () => { const svgns = "http://www.w3.org/2000/svg", recordSheetVisibility = document.querySelector('#content input[type="checkbox"].visible'); - PanZoom.start(svg); + new PanZoom(svg); const distanceOutput = document.getElementById('status'); -- cgit v1.2.3