Skip to content

Commit 786f4b2

Browse files
committed
FOR-2073: Implemented Drag mode
1 parent 1705b7e commit 786f4b2

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

src/contrib/sketchpad/sketchpad.js

+49-10
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,29 @@ export default class Sketchpad extends Base {
247247
eventStart: (coordinate) => {
248248
this.zoom(coordinate, 1 / this.zoomInfo.multiplier);
249249
}
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+
}
250273
}
251274
};
252275
}
@@ -687,16 +710,8 @@ export default class Sketchpad extends Base {
687710
//calculate SVG offset so that coordinate would be center of zoomed image
688711
this.zoomInfo.viewBox.minX = coordinate.x - this.zoomInfo.viewBox.width / 2;
689712
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();
700715
}
701716
}
702717

@@ -710,4 +725,28 @@ export default class Sketchpad extends Base {
710725
coordinate.y = (coordinate.y / this.zoomInfo.totalMultiplier) + this.zoomInfo.viewBox.minY;
711726
return coordinate;
712727
}
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+
}
713752
}

0 commit comments

Comments
 (0)