Fix:修复当画布大小改变后,限制思维导图在画布内和滚动条位置计算功能不正确的问题

This commit is contained in:
街角小林 2024-02-22 10:05:51 +08:00
parent dd52873106
commit 3effff95fa
3 changed files with 33 additions and 10 deletions

View File

@ -39,6 +39,10 @@ class MindMap {
// 获取容器尺寸位置信息 // 获取容器尺寸位置信息
this.getElRectInfo() this.getElRectInfo()
// 画布初始大小
this.initWidth = this.width
this.initHeight = this.height
// 添加css // 添加css
this.cssEl = null this.cssEl = null
this.addCss() this.addCss()

View File

@ -128,6 +128,10 @@ class View {
this.translateXY(mx, my) this.translateXY(mx, my)
} }
}) })
this.mindMap.on('resize', () => {
if (!this.checkNeedMindMapInCanvas()) return
this.transform()
})
} }
// 获取当前变换状态数据 // 获取当前变换状态数据
@ -313,20 +317,31 @@ class View {
this.translateXY(newX, newY) this.translateXY(newX, newY)
} }
// 将思维导图限制在画布内 // 判断是否需要将思维导图限制在画布内
limitMindMapInCanvas() { checkNeedMindMapInCanvas() {
const { isLimitMindMapInCanvasWhenHasScrollbar, isLimitMindMapInCanvas } = const { isLimitMindMapInCanvasWhenHasScrollbar, isLimitMindMapInCanvas } =
this.mindMap.opt this.mindMap.opt
// 如果注册了滚动条插件那么使用isLimitMindMapInCanvasWhenHasScrollbar配置 // 如果注册了滚动条插件那么使用isLimitMindMapInCanvasWhenHasScrollbar配置
if (this.mindMap.scrollbar) { if (this.mindMap.scrollbar) {
if (!isLimitMindMapInCanvasWhenHasScrollbar) return return isLimitMindMapInCanvasWhenHasScrollbar
} else { } else {
// 否则使用isLimitMindMapInCanvas配置 // 否则使用isLimitMindMapInCanvas配置
if (!isLimitMindMapInCanvas) return return isLimitMindMapInCanvas
} }
}
// 将思维导图限制在画布内
limitMindMapInCanvas() {
if (!this.checkNeedMindMapInCanvas()) return
let { scale, left, top, right, bottom } = this.getPositionLimit() let { scale, left, top, right, bottom } = this.getPositionLimit()
// 画布宽高改变了,但是思维导图元素变换的中心点依旧是原有位置,所以需要加上中心点变化量
const centerXChange =
((this.mindMap.width - this.mindMap.initWidth) / 2) * scale
const centerYChange =
((this.mindMap.height - this.mindMap.initHeight) / 2) * scale
// 如果缩放值改变了 // 如果缩放值改变了
const scaleRatio = this.scale / scale const scaleRatio = this.scale / scale
left *= scaleRatio left *= scaleRatio
@ -338,10 +353,10 @@ class View {
const centerX = this.mindMap.width / 2 const centerX = this.mindMap.width / 2
const centerY = this.mindMap.height / 2 const centerY = this.mindMap.height / 2
const scaleOffset = this.scale - 1 const scaleOffset = this.scale - 1
left -= scaleOffset * centerX left -= scaleOffset * centerX - centerXChange
right -= scaleOffset * centerX right -= scaleOffset * centerX - centerXChange
top -= scaleOffset * centerY top -= scaleOffset * centerY - centerYChange
bottom -= scaleOffset * centerY bottom -= scaleOffset * centerY - centerYChange
// 判断是否超出边界 // 判断是否超出边界
if (this.x > left) { if (this.x > left) {

View File

@ -40,6 +40,7 @@ class Scrollbar {
this.mindMap.on('mouseup', this.onMouseup) this.mindMap.on('mouseup', this.onMouseup)
this.mindMap.on('node_tree_render_end', this.updateScrollbar) this.mindMap.on('node_tree_render_end', this.updateScrollbar)
this.mindMap.on('view_data_change', this.updateScrollbar) this.mindMap.on('view_data_change', this.updateScrollbar)
this.mindMap.on('resize', this.updateScrollbar)
} }
// 解绑事件 // 解绑事件
@ -48,6 +49,7 @@ class Scrollbar {
this.mindMap.off('mouseup', this.onMouseup) this.mindMap.off('mouseup', this.onMouseup)
this.mindMap.off('node_tree_render_end', this.updateScrollbar) this.mindMap.off('node_tree_render_end', this.updateScrollbar)
this.mindMap.off('view_data_change', this.updateScrollbar) this.mindMap.off('view_data_change', this.updateScrollbar)
this.mindMap.off('resize', this.updateScrollbar)
} }
// 渲染后、数据改变需要更新滚动条 // 渲染后、数据改变需要更新滚动条
@ -202,7 +204,8 @@ class Scrollbar {
yOffset - yOffset -
paddingY * t.scaleY + paddingY * t.scaleY +
paddingY - paddingY -
rootCenterOffset.y * t.scaleY rootCenterOffset.y * t.scaleY +
((this.mindMap.height - this.mindMap.initHeight) / 2) * t.scaleY // 画布宽高改变了,但是思维导图元素变换的中心点依旧是原有位置,所以需要加上中心点变化量
this.mindMap.view.translateYTo(chartTop) this.mindMap.view.translateYTo(chartTop)
this.emitEvent({ this.emitEvent({
horizontal: scrollbarData.horizontal, horizontal: scrollbarData.horizontal,
@ -238,7 +241,8 @@ class Scrollbar {
xOffset - xOffset -
paddingX * t.scaleX + paddingX * t.scaleX +
paddingX - paddingX -
rootCenterOffset.x * t.scaleX rootCenterOffset.x * t.scaleX +
((this.mindMap.width - this.mindMap.initWidth) / 2) * t.scaleX // 画布宽高改变了,但是思维导图元素变换的中心点依旧是原有位置,所以需要加上中心点变化量
this.mindMap.view.translateXTo(chartLeft) this.mindMap.view.translateXTo(chartLeft)
this.emitEvent({ this.emitEvent({
vertical: scrollbarData.vertical, vertical: scrollbarData.vertical,