From 4cf66adc18dae4636c5df99b219e5acd87a5c502 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Wed, 11 Oct 2023 15:09:47 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=AF=BC=E5=87=BA=E5=92=8C=E9=80=82=E5=BA=94?= =?UTF-8?q?=E7=94=BB=E5=B8=83=E6=93=8D=E4=BD=9C=E6=97=B6=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=8E=B7=E5=8F=96=E5=AE=B9=E5=99=A8=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E5=B0=BA=E5=AF=B8=E4=BD=8D=E7=BD=AE=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/index.js | 23 +++++++++++++---------- simple-mind-map/src/core/view/View.js | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/simple-mind-map/index.js b/simple-mind-map/index.js index c4074dd2..e20ef950 100644 --- a/simple-mind-map/index.js +++ b/simple-mind-map/index.js @@ -35,13 +35,9 @@ class MindMap { // 容器元素 this.el = this.opt.el if (!this.el) throw new Error('缺少容器元素el') - this.elRect = this.el.getBoundingClientRect() - - // 画布宽高 - this.width = this.elRect.width - this.height = this.elRect.height - if (this.width <= 0 || this.height <= 0) - throw new Error('容器元素el的宽高不能为0') + + // 获取容器尺寸位置信息 + this.getElRectInfo() // 添加css this.cssEl = null @@ -181,11 +177,18 @@ class MindMap { }) } - // 容器尺寸变化,调整尺寸 - resize() { + // 获取或更新容器尺寸位置信息 + getElRectInfo() { this.elRect = this.el.getBoundingClientRect() this.width = this.elRect.width this.height = this.elRect.height + if (this.width <= 0 || this.height <= 0) + throw new Error('容器元素el的宽高不能为0') + } + + // 容器尺寸变化,调整尺寸 + resize() { + this.getElRectInfo() this.svg.size(this.width, this.height) } @@ -385,7 +388,7 @@ class MindMap { const origWidth = svg.width() const origHeight = svg.height() const origTransform = draw.transform() - const elRect = this.el.getBoundingClientRect() + const elRect = this.elRect // 去除放大缩小的变换效果 draw.scale(1 / origTransform.scaleX, 1 / origTransform.scaleY) // 获取变换后的位置尺寸信息,其实是getBoundingClientRect方法的包装方法 diff --git a/simple-mind-map/src/core/view/View.js b/simple-mind-map/src/core/view/View.js index ac235615..c318da0d 100644 --- a/simple-mind-map/src/core/view/View.js +++ b/simple-mind-map/src/core/view/View.js @@ -267,7 +267,7 @@ class View { let drawHeight = rect.height / origTransform.scaleY let drawRatio = drawWidth / drawHeight let { width: elWidth, height: elHeight } = - this.mindMap.el.getBoundingClientRect() + this.mindMap.elRect elWidth = elWidth - fitPadding * 2 elHeight = elHeight - fitPadding * 2 let elRatio = elWidth / elHeight