Files
ueblueprint/js/UEBlueprintDragScroll.js
barsdeveloper ccaad9b677 selector wip
2021-09-09 18:06:49 +02:00

40 lines
1.6 KiB
JavaScript

import UEBlueprintDrag from "./UEBlueprintDrag.js"
export default class UEBlueprintDragScroll extends UEBlueprintDrag {
constructor(scrolledEntity, options) {
super(scrolledEntity, options)
this.minZoom = options?.minZoom ?? -12
let self = this
this.mouseMoveHandler = function (e) {
let mousePosition = self.snapToGrid(e.clientX, e.clientY)
// How far the mouse has been moved
const dx = mousePosition[0] - self.mousePosition[0]
const dy = mousePosition[1] - self.mousePosition[1]
self.blueprintNode.scrollDelta([-dx, -dy])
// Reassign the position of mouse
self.mousePosition = mousePosition
}
this.mouseWheelHandler = function (e) {
e.preventDefault()
let zoomLevel = self.blueprintNode.getZoom()
zoomLevel -= Math.sign(e.deltaY)
let scaleCorrection = 1 / self.blueprintNode.getScale()
const targetOffset = e.target.getBoundingClientRect()
const currentTargetOffset = e.currentTarget.getBoundingClientRect()
let offset = [
e.offsetX + targetOffset.x * scaleCorrection - currentTargetOffset.x * scaleCorrection,
e.offsetY + targetOffset.y * scaleCorrection - currentTargetOffset.y * scaleCorrection
]
self.blueprintNode.setZoom(zoomLevel, offset)
}
this.blueprintNode.getGridDOMElement().addEventListener('wheel', this.mouseWheelHandler, false)
this.blueprintNode.getGridDOMElement().parentElement.addEventListener('wheel', e => e.preventDefault())
}
}