Merge pull request #152 from F-star/feat/scale-in-wheel-curor

Feat: 支持滚轮情况下,以光标为中心进行缩放
This commit is contained in:
街角小林 2023-06-27 08:47:40 +08:00 committed by GitHub
commit b90d4ddf8a
2 changed files with 15 additions and 16 deletions

View File

@ -13,7 +13,7 @@ export const defaultOpt = {
// 主题配置,会和所选择的主题进行合并 // 主题配置,会和所选择的主题进行合并
themeConfig: {}, themeConfig: {},
// 放大缩小的增量比例 // 放大缩小的增量比例
scaleRatio: 0.1, scaleRatio: 0.2,
// 最多显示几个标签 // 最多显示几个标签
maxTag: 5, maxTag: 5,
// 导出图片时的内边距 // 导出图片时的内边距

View File

@ -73,12 +73,12 @@ class View {
// 鼠标滚轮,向上和向左,都是缩小 // 鼠标滚轮,向上和向左,都是缩小
case CONSTANTS.DIR.UP: case CONSTANTS.DIR.UP:
case CONSTANTS.DIR.LEFT: case CONSTANTS.DIR.LEFT:
this.narrow() this.narrow(e.clientX, e.clientY)
break break
// 鼠标滚轮,向下和向右,都是放大 // 鼠标滚轮,向下和向右,都是放大
case CONSTANTS.DIR.DOWN: case CONSTANTS.DIR.DOWN:
case CONSTANTS.DIR.RIGHT: case CONSTANTS.DIR.RIGHT:
this.enlarge() this.enlarge(e.clientX, e.clientY)
break break
} }
} else { } else {
@ -190,28 +190,27 @@ class View {
} }
// 缩小 // 缩小
narrow(cx = this.mindMap.width / 2, cy = this.mindMap.height / 2) { narrow(cx, cy) {
let scale const scale = Math.max(this.scale - this.mindMap.opt.scaleRatio, 0.1)
if (this.scale - this.mindMap.opt.scaleRatio > 0.1) { this.scaleInCenter(scale, cx, cy)
scale = this.scale - this.mindMap.opt.scaleRatio
} else {
scale = 0.1
}
this.scaleInCenter(cx, cy, scale)
this.transform() this.transform()
this.mindMap.emit('scale', this.scale) this.mindMap.emit('scale', this.scale)
} }
// 放大 // 放大
enlarge(cx = this.mindMap.width / 2, cy = this.mindMap.height / 2) { enlarge(cx, cy) {
const scale = this.scale + this.mindMap.opt.scaleRatio const scale = this.scale + this.mindMap.opt.scaleRatio
this.scaleInCenter(cx, cy, scale) this.scaleInCenter(scale, cx, cy)
this.transform() this.transform()
this.mindMap.emit('scale', this.scale) this.mindMap.emit('scale', this.scale)
} }
// 基于画布中心进行缩放 // 基于指定中心进行缩放cxcy 可不指定,此时会使用画布中心点
scaleInCenter(cx, cy, scale) { scaleInCenter(scale, cx, cy) {
if (cx === undefined || cy === undefined) {
cx = this.mindMap.width / 2
cy = this.mindMap.height / 2
}
const prevScale = this.scale const prevScale = this.scale
const ratio = 1 - scale / prevScale const ratio = 1 - scale / prevScale
const dx = (cx - this.x) * ratio const dx = (cx - this.x) * ratio
@ -224,7 +223,7 @@ class View {
// 设置缩放 // 设置缩放
setScale(scale, cx, cy) { setScale(scale, cx, cy) {
if (cx !== undefined && cy !== undefined) { if (cx !== undefined && cy !== undefined) {
this.scaleInCenter(cx, cy, scale) this.scaleInCenter(scale, cx, cy)
} else { } else {
this.scale = scale this.scale = scale
} }