@@ -46,6 +46,7 @@ export class ImageViewer{
4646
4747 this . imageElem = null ;
4848 this . imageLoaded = false ;
49+ this . rightClickDown = false ;
4950 }
5051 init ( ) {
5152
@@ -65,25 +66,45 @@ export class ImageViewer{
6566
6667 let touchCount = 0 ;
6768
68- this . viewerDiv . addEventListener ( 'click' , ( ) => {
69+ this . viewerDiv . addEventListener ( 'click' , ( e ) => {
6970 //document.body.removeChild(this.viewerDiv);
7071 this . toggleVisibility ( false ) ;
7172 } ) ;
72- this . viewerDiv . addEventListener ( 'wheel' , ( e ) => {
73- this . zoomInStepped ( e ) ;
74- } ) ;
7573
74+
75+ this . viewerDiv . addEventListener ( 'mousedown' , ( e ) => {
76+ if ( e . button === 2 ) {
77+ this . rightClickDown = true ;
78+ this . zoomPrep ( e ) ;
79+ } else {
80+ this . onMouseDown ( e ) ;
81+ }
82+ } ) ;
83+ this . viewerDiv . addEventListener ( 'mouseup' , ( e ) => {
84+ this . onMouseUp ( e ) ;
85+ this . zoomEnd ( e ) ;
86+ this . rightClickDown = false ;
87+ } ) ;
88+ this . viewerDiv . addEventListener ( 'mousemove' , ( e ) => {
89+ if ( this . rightClickDown ) {
90+ this . zoomScale ( e ) ;
91+ } else {
92+ this . onMouseMove ( e ) ;
93+ }
94+ } ) ;
95+
96+
7697 this . viewerDiv . addEventListener ( 'touchstart' , ( e ) => {
7798 touchCount = 0 ;
7899 if ( e . touches . length > 1 ) {
79- this . zoomPrep ( e ) ;
100+ this . zoomPrepMobile ( e ) ;
80101 e . preventDefault ( ) ;
81102 }
82103 } ) ;
83104 this . viewerDiv . addEventListener ( 'touchmove' , ( e ) => {
84105 touchCount += 1 ;
85106 if ( e . touches . length > 1 ) {
86- this . zoomScale ( e ) ;
107+ this . zoomScaleMobile ( e ) ;
87108 e . preventDefault ( ) ;
88109 }
89110 } ) ;
@@ -92,9 +113,13 @@ export class ImageViewer{
92113 this . toggleVisibility ( false ) ;
93114 }
94115 this . onMouseUp ( e ) ;
95- this . zoomEnd ( e ) ;
116+ this . zoomEndMobile ( e ) ;
96117 e . preventDefault ( ) ;
97118 } ) ;
119+
120+ this . viewerDiv . addEventListener ( 'wheel' , ( e ) => {
121+ this . zoomInStepped ( e ) ;
122+ } ) ;
98123 }
99124
100125 buildImageElement ( ) {
@@ -115,33 +140,44 @@ export class ImageViewer{
115140 } ) ;
116141
117142 this . viewerImg . addEventListener ( 'mousedown' , ( e ) => {
118- this . onMouseDown ( e ) ;
143+ if ( e . button === 2 ) {
144+ this . rightClickDown = true ;
145+ this . zoomPrep ( e ) ;
146+ } else {
147+ this . onMouseDown ( e ) ;
148+ }
119149 } ) ;
120150 this . viewerImg . addEventListener ( 'mouseup' , ( e ) => {
121151 this . onMouseUp ( e ) ;
152+ this . zoomEnd ( e ) ;
153+ this . rightClickDown = false ;
122154 } ) ;
123155 this . viewerImg . addEventListener ( 'mousemove' , ( e ) => {
124- this . onMouseMove ( e ) ;
156+ if ( this . rightClickDown ) {
157+ this . zoomScale ( e ) ;
158+ } else {
159+ this . onMouseMove ( e ) ;
160+ }
125161 } ) ;
126162
127163 this . viewerImg . addEventListener ( 'touchstart' , ( e ) => {
128164 if ( e . touches . length === 1 ) {
129165 this . onMouseDown ( e . touches [ 0 ] ) ;
130166 } else {
131- this . zoomPrep ( e ) ;
167+ this . zoomPrepMobile ( e ) ;
132168 }
133169 e . preventDefault ( ) ;
134170 } ) ;
135171 this . viewerImg . addEventListener ( 'touchend' , ( e ) => {
136172 this . onMouseUp ( e ) ;
137- this . zoomEnd ( e ) ;
173+ this . zoomEndMobile ( e ) ;
138174 e . preventDefault ( ) ;
139175 } ) ;
140176 this . viewerImg . addEventListener ( 'touchmove' , ( e ) => {
141177 if ( e . touches . length === 1 ) {
142178 this . onMouseMove ( e . touches [ 0 ] ) ;
143179 } else if ( e . touches . length === 2 ) {
144- this . zoomScale ( e ) ;
180+ this . zoomScaleMobile ( e ) ;
145181 }
146182 e . preventDefault ( ) ;
147183 } ) ;
@@ -183,7 +219,7 @@ export class ImageViewer{
183219 }
184220
185221 // -- -- --
186-
222+
187223 // Mouse wheel stepped zoom
188224 zoomInStepped ( e ) {
189225 if ( this . imageLoaded ) {
@@ -200,12 +236,59 @@ export class ImageViewer{
200236 }
201237 e . preventDefault ( ) ;
202238 }
239+
240+
241+ // -- -- --
242+
243+ // Zoom helper functions for Mouse
244+
245+ zoomPrep ( e ) {
246+ if ( this . imageLoaded ) {
247+ this . viewerStats . isZooming = true ;
248+ this . viewerStats . zoomStart = this . viewerStats . zoom ;
249+ this . viewerStats . zoomPosA . x = e . clientX ;
250+ this . viewerStats . zoomPosA . y = e . clientY ;
251+ }
252+ e . preventDefault && e . preventDefault ( ) ;
253+ }
254+
255+ zoomScale ( e ) {
256+ if ( this . imageLoaded && this . viewerStats . isZooming ) {
257+ this . checkDebug ( ) ;
258+ let dy = e . clientY - this . viewerStats . zoomPosA . y ;
259+ let dx = e . clientX - this . viewerStats . zoomPosA . x ;
260+ let dist = - dx ; //(dx*dx + dy*dy) ** .5;
261+ //dist *= dy<0 || dx<0 ? -1 : 1;
262+ let scale = 1.0 - ( dist * 0.005 ) ;
263+ this . viewerStats . zoom = Math . min ( Math . max ( this . viewerStats . zoomStart * scale , this . viewerStats . zoomMin ) , this . viewerStats . zoomMax ) ;
264+ this . viewerImg . style . transform = `translate(${ this . viewerStats . offsetX } px, ${ this . viewerStats . offsetY } px) scale(${ this . viewerStats . zoom } )` ;
265+
266+ if ( this . verbose ) {
267+ let debugText = '' ;
268+ debugText += `Zoom: ${ this . viewerStats . zoom . toFixed ( 2 ) } \n` ;
269+ debugText += `Mouse Pos: (${ e . clientX } , ${ e . clientY } )\n` ;
270+ this . log ( debugText ) ;
271+ }
272+ e . preventDefault && e . preventDefault ( ) ;
273+ }
274+ }
275+
276+ zoomEnd ( e ) {
277+ if ( this . viewerStats . isZooming ) {
278+ this . viewerStats . isZooming = false ;
279+ this . viewerStats . zoom = Math . min ( Math . max ( this . viewerStats . zoom , this . viewerStats . zoomMin ) , this . viewerStats . zoomMax ) ;
280+ }
281+ e . preventDefault && e . preventDefault ( ) ;
282+ }
283+
284+ // -- -- --
203285
286+ // Zoom helper functions for Touch
204287
205- zoomPrep ( ) {
288+ zoomPrepMobile ( e ) {
206289 if ( this . viewerStats . isZooming ) return ;
207290 this . viewerStats . isZooming = true ;
208- const touches = event . touches ;
291+ const touches = e . touches ;
209292 if ( touches . length < 2 ) return ;
210293 const dx = touches [ 0 ] . clientX - touches [ 1 ] . clientX ;
211294 const dy = touches [ 0 ] . clientY - touches [ 1 ] . clientY ;
@@ -223,7 +306,7 @@ export class ImageViewer{
223306 e . preventDefault && e . preventDefault ( ) ;
224307 }
225308
226- zoomScale ( e ) {
309+ zoomScaleMobile ( e ) {
227310 if ( e . touches . length < 2 ) return ;
228311
229312 this . checkDebug ( ) ;
@@ -254,7 +337,7 @@ export class ImageViewer{
254337 e . preventDefault && e . preventDefault ( ) ;
255338 }
256339
257- zoomEnd ( e ) {
340+ zoomEndMobile ( e ) {
258341 if ( this . viewerStats . isZooming ) {
259342 this . viewerStats . isZooming = false ;
260343 this . viewerStats . zoom = Math . min ( Math . max ( this . viewerStats . zoom , this . viewerStats . zoomMin ) , this . viewerStats . zoomMax ) ;
0 commit comments