Skip to content

Commit

Permalink
Move zoomed image
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Mar 8, 2019
1 parent 08bf99d commit f4f3648
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/components/Images.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@

<template>
<img
:class="{zoomed: zoomRatio !== 1}"
:class="{
dragging,
zoomed: zoomRatio !== 1
}"
:draggable="true"
:src="data"
:style="{
height: zoomHeight + 'px',
width: zoomWidth + 'px',
marginTop: shiftY + 'px',
marginLeft: shiftX + 'px'
}"
@drag="dragHandler"
@dragstart="dragStart"
@dragend="dragEnd"
@load="updateImgSize"
@wheel="updateZoom"
@dblclick="resetZoom">
Expand All @@ -51,6 +58,7 @@ export default {
],
data() {
return {
dragging: false,
shiftX: 0,
shiftY: 0,
zoomRatio: 1
Expand Down Expand Up @@ -151,6 +159,36 @@ export default {
this.zoomRatio = 1
this.shiftX = 0
this.shiftY = 0
},

/**
* Dragging handlers
*
* @param {Event} event the event
* @returns {Boolean} false
*/
dragStart(event) {
// cursor hack
event.dataTransfer.effectAllowed = 'move'
this.dragX = event.pageX
this.dragY = event.pageY
this.dragging = true
return false
},
dragEnd() {
this.dragging = false
},
dragHandler({ pageX, pageY }) {
if (this.zoomRatio > 1) {
const moveX = this.shiftX + (pageX - this.dragX)
const moveY = this.shiftY + (pageY - this.dragY)
const growX = this.zoomWidth - this.width
const growY = this.zoomHeight - this.height
this.shiftX = Math.min(Math.max(moveX, -growX / 2), growX / 2)
this.shiftY = Math.min(Math.max(moveY, -growY / 2), growX / 2)
this.dragX = pageX
this.dragY = pageY
}
}
}
}
Expand Down Expand Up @@ -182,6 +220,12 @@ img {
max-height: none;
max-width: none;
z-index: 10000;
cursor: move;
}

&.dragging {
transition: none !important;
cursor: move;
}
}
</style>

0 comments on commit f4f3648

Please sign in to comment.