From b262336f083112d76ede22a8e3459097ddfc1933 Mon Sep 17 00:00:00 2001 From: Hao Huang Date: Mon, 26 Jun 2023 05:41:18 +0000 Subject: [PATCH] =?UTF-8?q?Feat:=20=E4=BB=A5=E7=94=BB=E5=B8=83=E4=B8=BA?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E8=BF=9B=E8=A1=8C=E7=BC=A9=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/view/View.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/simple-mind-map/src/core/view/View.js b/simple-mind-map/src/core/view/View.js index 98df7ee0..a3a12d41 100644 --- a/simple-mind-map/src/core/view/View.js +++ b/simple-mind-map/src/core/view/View.js @@ -170,8 +170,8 @@ class View { // 应用变换 transform() { this.mindMap.draw.transform({ + origin: [0, 0], scale: this.scale, - // origin: 'center center', translate: [this.x, this.y] }) this.mindMap.emit('view_data_change', this.getTransformData()) @@ -191,22 +191,36 @@ class View { // 缩小 narrow() { + let scale if (this.scale - this.mindMap.opt.scaleRatio > 0.1) { - this.scale -= this.mindMap.opt.scaleRatio + scale = this.scale - this.mindMap.opt.scaleRatio } else { - this.scale = 0.1 + scale = 0.1 } + this.scaleInCenter(this.mindMap.width / 2, this.mindMap.height / 2, scale) this.transform() this.mindMap.emit('scale', this.scale) } // 放大 enlarge() { - this.scale += this.mindMap.opt.scaleRatio + const scale = this.scale + this.mindMap.opt.scaleRatio + this.scaleInCenter(this.mindMap.width / 2, this.mindMap.height / 2, scale) this.transform() this.mindMap.emit('scale', this.scale) } + // 基于画布中心进行缩放 + scaleInCenter(cx, cy, scale) { + const prevScale = this.scale + const dx = (cx - this.x) * (1 - scale / prevScale) + const dy = (cy - this.y) * (1 - scale / prevScale) + + this.x += dx; + this.y += dy; + this.scale = scale + } + // 设置缩放 setScale(scale) { this.scale = scale