@@ -247,6 +247,29 @@ export default class Sketchpad extends Base {
247
247
eventStart : ( coordinate ) => {
248
248
this . zoom ( coordinate , 1 / this . zoomInfo . multiplier ) ;
249
249
}
250
+ } ,
251
+ drag : {
252
+ icon : 'hand-paper-o' ,
253
+ title : 'Drag Zoomed Image' ,
254
+ state : {
255
+ mode : 'drag'
256
+ } ,
257
+ eventStart : ( coordinate ) => {
258
+ this . dragStartPoint = coordinate ;
259
+ } ,
260
+ drag : ( coordinate ) => {
261
+ if ( ! this . dragLastPoint ) {
262
+ this . dragLastPoint = this . dragStartPoint ;
263
+ }
264
+ const offset = {
265
+ x : Math . round ( coordinate . x - this . dragStartPoint . x ) ,
266
+ y : Math . round ( coordinate . y - this . dragStartPoint . y )
267
+ } ;
268
+ if ( offset . x !== 0 || offset . y !== 0 ) {
269
+ this . dragImage ( offset ) ;
270
+ this . dragLastPoint = coordinate ;
271
+ }
272
+ }
250
273
}
251
274
} ;
252
275
}
@@ -687,16 +710,8 @@ export default class Sketchpad extends Base {
687
710
//calculate SVG offset so that coordinate would be center of zoomed image
688
711
this . zoomInfo . viewBox . minX = coordinate . x - this . zoomInfo . viewBox . width / 2 ;
689
712
this . zoomInfo . viewBox . minY = coordinate . y - this . zoomInfo . viewBox . height / 2 ;
690
- //don't let zoom go out of SVG on the left and on the top
691
- this . zoomInfo . viewBox . minX = this . zoomInfo . viewBox . minX < 0 ? 0 : this . zoomInfo . viewBox . minX ;
692
- this . zoomInfo . viewBox . minY = this . zoomInfo . viewBox . minY < 0 ? 0 : this . zoomInfo . viewBox . minY ;
693
- //don't let zoom go out of SVG on the right and on the bottom
694
- const maxOffsetX = this . component . width - this . zoomInfo . viewBox . width ,
695
- maxOffsetY = this . component . height - this . zoomInfo . viewBox . height ;
696
- this . zoomInfo . viewBox . minX = this . zoomInfo . viewBox . minX > ( maxOffsetX ) ? maxOffsetX : this . zoomInfo . viewBox . minX ;
697
- this . zoomInfo . viewBox . minY = this . zoomInfo . viewBox . minY > ( maxOffsetY ) ? maxOffsetY : this . zoomInfo . viewBox . minY ;
698
- //set viewBox so that SVG gets zoomed
699
- this . editSvgElement . setAttribute ( 'viewBox' , `${ this . zoomInfo . viewBox . minX } ${ this . zoomInfo . viewBox . minY } ${ this . zoomInfo . viewBox . width } ${ this . zoomInfo . viewBox . height } ` ) ;
713
+ this . normalizeSvgOffset ( ) ;
714
+ this . updateSvgViewBox ( ) ;
700
715
}
701
716
}
702
717
@@ -710,4 +725,28 @@ export default class Sketchpad extends Base {
710
725
coordinate . y = ( coordinate . y / this . zoomInfo . totalMultiplier ) + this . zoomInfo . viewBox . minY ;
711
726
return coordinate ;
712
727
}
728
+
729
+ dragImage ( offset ) {
730
+ //calculate new offsets for SVG
731
+ this . zoomInfo . viewBox . minX = this . zoomInfo . viewBox . minX - offset . x ;
732
+ this . zoomInfo . viewBox . minY = this . zoomInfo . viewBox . minY - offset . y ;
733
+ this . normalizeSvgOffset ( ) ;
734
+ this . updateSvgViewBox ( ) ;
735
+ }
736
+
737
+ normalizeSvgOffset ( ) {
738
+ //don't let offset go out of SVG on the left and on the top
739
+ this . zoomInfo . viewBox . minX = this . zoomInfo . viewBox . minX < 0 ? 0 : this . zoomInfo . viewBox . minX ;
740
+ this . zoomInfo . viewBox . minY = this . zoomInfo . viewBox . minY < 0 ? 0 : this . zoomInfo . viewBox . minY ;
741
+ //don't let offset go out of SVG on the right and on the bottom
742
+ const maxOffsetX = this . component . width - this . zoomInfo . viewBox . width ,
743
+ maxOffsetY = this . component . height - this . zoomInfo . viewBox . height ;
744
+ this . zoomInfo . viewBox . minX = this . zoomInfo . viewBox . minX > ( maxOffsetX ) ? maxOffsetX : this . zoomInfo . viewBox . minX ;
745
+ this . zoomInfo . viewBox . minY = this . zoomInfo . viewBox . minY > ( maxOffsetY ) ? maxOffsetY : this . zoomInfo . viewBox . minY ;
746
+ }
747
+
748
+ updateSvgViewBox ( ) {
749
+ //set viewBox so that SVG gets zoomed to the proper area according to zoomInfo
750
+ this . editSvgElement . setAttribute ( 'viewBox' , `${ this . zoomInfo . viewBox . minX } ${ this . zoomInfo . viewBox . minY } ${ this . zoomInfo . viewBox . width } ${ this . zoomInfo . viewBox . height } ` ) ;
751
+ }
713
752
}
0 commit comments