Last active
April 9, 2024 06:35
-
-
Save tamas-p/a8237edcf8f82266c0f7b6701048185a to your computer and use it in GitHub Desktop.
Minimal element (img) viewer with zoom, pan, enlarge capability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* MIT License | |
Copyright (c) 2024 Tamas Palagyi | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE.*/ | |
/** | |
* Minimal element (img) viewer with zoom, pan, enlarge capability. | |
* | |
* Usage: import this file: <script src="viewit.js"></script> | |
* Construct ViewIt at the load of an element: | |
* | |
* <img src="your_image.svg" onload="new ViewIt(this)" /> | |
* | |
* You can pan (click and move mouse cursor), zoom (rotate mouse wheel) and | |
* enlarge/back (double click) an element. | |
*/ | |
class ViewIt { | |
#transX = 0; | |
#transY = 0; | |
#prevX; | |
#prevY; | |
#scale = 1; | |
#change = 0.1; | |
#maxScale = 10; | |
#minScale = 0.1; | |
#isMouseDown = false; | |
constructor(element) { | |
this.#register(element); | |
} | |
onMouseDown(event) { | |
this.#isMouseDown = true; | |
this.#savePos(event); | |
} | |
onMouseUp(event) { | |
this.#isMouseDown = false; | |
} | |
onMouseLeave(event) { | |
this.onMouseUp(event); | |
} | |
onMouseMove(event) { | |
if (this.#isMouseDown) { | |
// Calculate scaling caused by ancestor elements. | |
const r = event.target.getBoundingClientRect(); | |
const xScaleAnc = event.target.clientWidth / r.width; | |
const yScaleAnc = event.target.clientHeight / r.height; | |
this.#transX += this.#scale * xScaleAnc * (event.pageX - this.#prevX); | |
this.#transY += this.#scale * yScaleAnc * (event.pageY - this.#prevY); | |
this.#paintIt(event); | |
this.#savePos(event); | |
} | |
} | |
onWheel(event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
// Change scaling. | |
const oldScale = this.#scale; | |
this.#scale = | |
event.deltaY < 0 ? oldScale + this.#change : oldScale - this.#change; | |
if (this.#scale > this.#maxScale) this.#scale = this.#maxScale; | |
if (this.#scale < this.#minScale) this.#scale = this.#minScale; | |
const ratio = this.#scale / oldScale; | |
// Adjust position to fix where pointer is. | |
const r = event.target.getBoundingClientRect(); | |
const ox = r.left + r.width / 2; // Get x of origin. | |
const oy = r.top + r.height / 2; // Get y of origin. | |
const a1 = event.x - ox; // Get x component of mouse pos from origin. | |
const b1 = event.y - oy; // Get y component of mouse pos from origin. | |
const da = ratio * a1 - a1; // Delta caused by scale change at x. | |
const db = ratio * b1 - b1; // Delta caused by scale change at y. | |
// Calculate scaling caused by ancestor elements. | |
const xScaleAnc = (event.target.clientWidth * oldScale) / r.width; | |
const yScaleAnc = (event.target.clientHeight * oldScale) / r.height; | |
// Apply adjustments impacted by ancestor scale to current translation. | |
this.#transX -= xScaleAnc * da; | |
this.#transY -= yScaleAnc * db; | |
this.#paintIt(event); | |
} | |
#saveBody; | |
#newBody; | |
#wrapper; | |
#scrollTop; | |
#scrollLeft; | |
dblClick(event) { | |
this.#transX = 0; | |
this.#transY = 0; | |
this.#scale = 1; | |
event.target.style.transform = | |
"translate(" + | |
this.#transX + | |
"px, " + | |
this.#transY + | |
"px) scale(" + | |
this.#scale + | |
") translateZ(0)"; | |
if (this.#saveBody == null) { | |
this.#scrollTop = document.body.scrollTop; | |
this.#scrollLeft = document.body.scrollLeft; | |
this.#saveBody = document.body; | |
const newWrapper = document.createElement("div"); | |
newWrapper.style.maxHeight = "100%"; | |
newWrapper.style.maxWidth = "100%"; | |
newWrapper.style.height = "auto"; | |
newWrapper.style.width = "auto"; | |
newWrapper.style.overflow = "hidden"; | |
newWrapper.appendChild(event.target); | |
this.#newBody = document.createElement("body"); | |
this.#newBody.style.margin = "0"; | |
this.#newBody.appendChild(newWrapper); | |
document.body.parentNode.replaceChild(this.#newBody, document.body); | |
} else { | |
this.#wrapper.appendChild(event.target); | |
document.body.parentNode.replaceChild(this.#saveBody, this.#newBody); | |
document.body.scrollTop = this.#scrollTop; | |
document.body.scrollLeft = this.#scrollLeft; | |
this.#saveBody = null; | |
} | |
} | |
#savePos(event) { | |
this.#prevX = event.pageX; | |
this.#prevY = event.pageY; | |
} | |
#paintIt(event) { | |
const element = event.target; | |
element.style.transform = | |
"translate(" + | |
this.#transX + | |
"px, " + | |
this.#transY + | |
"px) scale(" + | |
this.#scale + | |
") translateZ(0)"; | |
} | |
#register(element) { | |
this.#injectParentDiv(element); | |
element.onmousedown = this.onMouseDown.bind(this); | |
element.onmouseup = this.onMouseUp.bind(this); | |
element.onmousemove = this.onMouseMove.bind(this); | |
element.onmouseleave = this.onMouseLeave.bind(this); | |
element.onwheel = this.onWheel.bind(this); | |
element.addEventListener("dblclick", this.dblClick.bind(this)); | |
element.style.width = "100%"; | |
element.draggable = false; | |
element.parentNode.style.overflow = "hidden"; | |
} | |
#injectParentDiv(element) { | |
const parent = element.parentNode; | |
const wrapper = document.createElement("div"); | |
wrapper.style.cssText = element.style.cssText; | |
parent.replaceChild(wrapper, element); | |
wrapper.appendChild(element); | |
this.#wrapper = wrapper; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment