Update storing pan & zoom changes to localStorage
This commit is contained in:
parent
b812884bdf
commit
d36236d0fe
38
src/index.js
38
src/index.js
@ -2,36 +2,31 @@ import { pan, zoom } from 'svg-pan-zoom';
|
||||
import Game from './modules/game.js';
|
||||
|
||||
const PanZoom = new function () {
|
||||
const vb = 'viewBox';
|
||||
let svg;
|
||||
const storageKey = 'pan-zoom';
|
||||
|
||||
function storeViewBoxVal() {
|
||||
localStorage.setItem(vb, svg.getAttribute(vb));
|
||||
function storePanZoomVal(transformMatrix) {
|
||||
localStorage.setItem(storageKey, transformMatrix);
|
||||
}
|
||||
|
||||
function observeViewBoxChanges() {
|
||||
const observer = new MutationObserver(mutations => {
|
||||
if (mutations.find(m => m.target == svg && m.attributeName == vb)) {
|
||||
storeViewBoxVal(svg);
|
||||
}
|
||||
});
|
||||
function observePanZoomChanges(svg) {
|
||||
const observer = new MutationObserver(() => storePanZoomVal(svg.style.transform));
|
||||
|
||||
observer.observe(svg, { attributes: true });
|
||||
observer.observe(svg, { attributeFilter: ['style'] });
|
||||
}
|
||||
|
||||
function restoreViewBoxVal() {
|
||||
const storedVbVal = localStorage.getItem(vb);
|
||||
function restorePanZoomVal(svg) {
|
||||
const storedPanZoomVal = localStorage.getItem(storageKey);
|
||||
|
||||
if (storedVbVal) {
|
||||
svg.setAttributeNS(null, vb, storedVbVal);
|
||||
if (storedPanZoomVal) {
|
||||
svg.style.transform = storedPanZoomVal;
|
||||
}
|
||||
}
|
||||
|
||||
function addEventListeners() {
|
||||
function addEventListeners(svg) {
|
||||
svg.addEventListener('wheel', e => {
|
||||
e.preventDefault();
|
||||
|
||||
svg.setAttributeNS(null, vb, zoom(svg, e));
|
||||
zoom(svg, e);
|
||||
}, { passive: false });
|
||||
|
||||
svg.addEventListener('pointerdown', e => {
|
||||
@ -41,11 +36,10 @@ const PanZoom = new function () {
|
||||
}, { passive: false });
|
||||
}
|
||||
|
||||
this.start = function (el) {
|
||||
svg = el;
|
||||
restoreViewBoxVal();
|
||||
addEventListeners();
|
||||
observeViewBoxChanges();
|
||||
this.start = function (svg) {
|
||||
restorePanZoomVal(svg);
|
||||
addEventListeners(svg);
|
||||
observePanZoomChanges(svg);
|
||||
};
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user