zoom-element
Version:
[]() [](https://github.com/spyder01/zoom-element/issues) [ • 1.97 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class a extends DOMException{constructor(s="An error occured while creating zoom area."){super(s)}}const o={minZoom:.2,maxZoom:2.5,initialScale:1,zoomScale:.1};class r{constructor(s="zoom-area",t=o){if(this.getScale=()=>this.scale,this.zoom=i=>{const e=this.scale+i;return this.scale=Math.max(this.minZoom,Math.min(this.maxZoom,e)),this.setDivStyle(),this},this.zoomIn=()=>this.zoom(this.zoomScale),this.zoomOut=()=>this.zoom(-this.zoomScale),this.reset=()=>(this.position={x:0,y:0},this.scale=this.config.initialScale,this.setDivStyle(),this),this.handleDrag=i=>{if(console.log("hello"),this.isDragging){const e=i.clientX-this.initialPosition.x,l=i.clientY-this.initialPosition.y,{x:h,y:m}=this.position;this.position={x:h+e,y:m+l},this.initialPosition={x:i.clientX,y:i.clientY}}this.setDivStyle()},this.handleCntrlWheel=i=>{!i.ctrlKey||(this.initialPosition.y+=i.deltaY,this.position.y+=i.deltaY)},this.setDivStyle=()=>{this.zoomArea.style.transform=`scale(${this.scale}) translate(${this.position.x}px, ${this.position.y}px)`,this.zoomArea.style.transformOrigin="center"},this.init=()=>{this.setDivStyle(),this.zoomArea.addEventListener("mousemove",this.handleDrag),this.zoomArea.addEventListener("mouseup",()=>this.isDragging=!1),this.zoomArea.addEventListener("mousedown",i=>{this.isDragging=!0,this.initialPosition={x:i.clientX,y:i.clientY},this.setDivStyle()}),this.zoomArea.addEventListener("wheel",this.handleCntrlWheel)},this.zoomArea=document.getElementById(s),this.scale=t.initialScale?t.initialScale:o.initialScale,this.minZoom=t.minZoom?t.minZoom:o.minZoom,this.maxZoom=t.maxZoom?t.maxZoom:o.maxZoom,this.initialPosition={x:0,y:0},this.position=this.initialPosition,this.isDragging=!1,this.zoomScale=t.zoomScale?t.zoomScale:o.zoomScale,this.config={...o,...t},!this.zoomArea)throw new a;this.init()}}exports.ZoomAreaException=a;exports.default=r;