-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from clayzenx/zoom-key
change zoom key
- Loading branch information
Showing
3 changed files
with
120 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
export default { | ||
data: () => ({ | ||
zoomAndPan: { | ||
zoomKey: 'ctrlKey', // Кнопка для зума | ||
panKey: 'shiftKey', | ||
isMove: false, // Признак перемещения схемы | ||
isShiftSens: false, // Признак, что пользователь нажал шифт | ||
cacheViewBox: null, | ||
moveX: 0, | ||
moveY: 0, | ||
zoom: { | ||
value: 1, // Текущий зум | ||
step: 0.1 // Шаг зума | ||
} | ||
} | ||
}), | ||
computed: { | ||
koofScreenX() { | ||
return this.zoomAndPanElement ? this.ZoomAndPanViewBox.width / this.zoomAndPanElement.clientWidth : 1; | ||
}, | ||
// Коэффициент преобразования реальных точек во внутренние по высоте | ||
koofScreenY() { | ||
return this.zoomAndPanElement ? this.ZoomAndPanViewBox.height / this.zoomAndPanElement.clientHeight : 1; | ||
}, | ||
ZoomAndPanViewBox() { | ||
if (!this.zoomAndPanElement) { | ||
return { | ||
x: 0, | ||
y : 0, | ||
width : 0, | ||
height : 0 | ||
}; | ||
} else | ||
return this.zoomAndPan.cacheViewBox ? | ||
this.zoomAndPan.cacheViewBox | ||
// eslint-disable-next-line vue/no-side-effects-in-computed-properties | ||
: this.zoomAndPan.cacheViewBox = this.zoomAndPanElement.viewBox.baseVal; | ||
}, | ||
zoomAndPanElement() { | ||
return this.$refs.zoomAndPan; | ||
} | ||
}, | ||
methods: { | ||
doZoom(value, x, y) { | ||
const kX = x / (this.zoomAndPanElement.clientWidth || x); | ||
const kY = y / (this.zoomAndPanElement.clientHeight || y); | ||
let resizeWidth = value * this.ZoomAndPanViewBox.width; | ||
let resizeHeight = value * this.ZoomAndPanViewBox.height; | ||
this.ZoomAndPanViewBox.x -= resizeWidth * kX; | ||
this.ZoomAndPanViewBox.width += resizeWidth; | ||
this.ZoomAndPanViewBox.y -= resizeHeight * kY; | ||
this.ZoomAndPanViewBox.height += resizeHeight; | ||
this.zoomAndPan.cacheViewBox = null; | ||
}, | ||
zoomAndPanWheelHandler(event) { | ||
if (!event[this.zoomAndPan.zoomKey]) return; | ||
let e = window.event || event; | ||
switch (Math.max(-1, Math.min(1, (e.deltaY || -e.detail)))) { | ||
case 1: | ||
this.doZoom(this.zoomAndPan.zoom.step, event.offsetX, event.offsetY); | ||
break; | ||
case -1: | ||
this.doZoom(-this.zoomAndPan.zoom.step, event.offsetX, event.offsetY); | ||
break; | ||
} | ||
event.stopPropagation(); | ||
}, | ||
zoomAndPanMouseDown(event) { | ||
if (!event[this.zoomAndPan.panKey]) return; | ||
this.zoomAndPan.isMove = true; | ||
this.zoomAndPan.moveX = event.clientX; | ||
this.zoomAndPan.moveY = event.clientY; | ||
}, | ||
zoomAndPanMouseMove(event) { | ||
this.zoomAndPan.isShiftSens = event[this.zoomAndPan.panKey]; | ||
if (!this.zoomAndPan.isMove) return; | ||
this.ZoomAndPanViewBox.x += (this.zoomAndPan.moveX - event.clientX) * (this.koofScreenX || 0); | ||
this.ZoomAndPanViewBox.y += (this.zoomAndPan.moveY - event.clientY) * (this.koofScreenY || 0); | ||
this.zoomAndPan.moveX = event.clientX; | ||
this.zoomAndPan.moveY = event.clientY; | ||
}, | ||
zoomAndPanMouseUp() { | ||
this.zoomAndPan.isMove = false; | ||
} | ||
} | ||
}; |