Skip to content

Commit 7b1a9b4

Browse files
committed
fix zoom on mouse cursor
1 parent 05b9f69 commit 7b1a9b4

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

example/editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function updateMindmap(text) {
4444
}
4545

4646
updateMindmap(text);
47-
viewer.fitView();
47+
// viewer.fitView();
4848
editor.update({ value: text });
4949
editor.textarea.focus();
5050

src/js/parser/text-parser.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ function parse(text) {
77
let currentLevel = 0
88

99
for (let line of lines) {
10+
let content = getContent(line)
11+
if (content.trim() === "") continue
12+
1013
if (root == null) {
1114
root = {
12-
content: getContent(line),
15+
content: content,
1316
children: []
1417
}
1518
levelStack.push({ node: root, level: 0 })
1619
} else {
17-
let content = getContent(line)
20+
1821
let level = getLevel(line)
1922
let node = {
2023
content: content,
@@ -25,8 +28,6 @@ function parse(text) {
2528
break
2629
}
2730

28-
let lastIndex = levelStack.length - 1
29-
3031
while (levelStack[levelStack.length - 1].level > level) {
3132
levelStack.pop()
3233
currentLevel -= 1
@@ -50,7 +51,7 @@ function parse(text) {
5051
}
5152

5253
function getLevel(line) {
53-
line = line.replace(/\t/, " ")
54+
line = line.replace(/\t/g, " ")
5455
let leadingWs = line.match(/^\s*/)
5556
leadingWs = leadingWs[0] || ""
5657
return Math.round(leadingWs.length / 4)

src/js/viewer.js

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,39 @@ class MindmapViewer {
5454
}
5555

5656
zoomBy(percent, x = 0, y = 0) {
57-
let dx = x * (this.scale + percent) - x * this.scale;
58-
let dy = y * (this.scale + percent) - y * this.scale;
59-
this.two.scene.scale = this.scale = this.scale + percent;
60-
61-
this.translateBy(-dx, -dy);
57+
let translation = this.two.scene.translation.clone()
58+
let currentCoord = this.clientToLocalCoord(x, y, this.scale)
59+
let newScale = this.scale + this.scale * percent
60+
let newCoord = this.clientToLocalCoord(x, y, newScale)
61+
62+
let dx = (newCoord.x - currentCoord.x)
63+
let dy = (newCoord.y - currentCoord.y)
64+
let newTranslation = this.localToClientCoord(dx, dy, newScale)
65+
this.two.scene.scale = this.scale = newScale;
66+
67+
// this.translateBy(-dx, -dy);
68+
this.two.scene.translation.copy(newTranslation)
6269
this.two.update();
6370
}
6471

72+
73+
clientToLocalCoord(x, y, scale) {
74+
let translation = this.two.scene.translation.clone()
75+
let localCoord = new Two.Vector(x, y)
76+
localCoord.subSelf(translation)
77+
localCoord.divideScalar(scale)
78+
return localCoord
79+
}
80+
81+
localToClientCoord(x, y, scale) {
82+
let clientCoord = new Two.Vector(x, y)
83+
clientCoord.multiplyScalar(scale)
84+
clientCoord.addSelf(this.two.scene.translation)
85+
return clientCoord
86+
}
87+
88+
89+
6590
centerView() {
6691
let clientRect = this.mindMap.getBoundingBox();
6792
let centerX = (clientRect.left + clientRect.width / 2) * this.scale;
@@ -96,9 +121,47 @@ class MindmapViewer {
96121
e.stopPropagation();
97122
e.preventDefault();
98123

99-
var dy = e.deltaY / 100;
100-
this.zoomBy(dy, e.clientX, e.clientY);
124+
const direction = e.deltaY > 0 ? -1 : 1;
125+
const factor = 0.03;
126+
const zoom = direction * factor
127+
128+
let coord = this.getClientCoord(e)
129+
this.zoomBy(zoom, coord.x, coord.y);
101130
});
131+
132+
stage.addEventListener("click", (e) => {
133+
let coord = this.getClientCoord(e)
134+
this.clientToLocalCoord(coord.x, coord.y, this.scale)
135+
})
136+
}
137+
138+
getClientCoord(mouseEvent) {
139+
var obj = mouseEvent.currentTarget || mouseEvent.target
140+
obj = obj.parentNode;
141+
var objLeft = 0;
142+
var objTop = 0;
143+
var xpos;
144+
var ypos;
145+
146+
while (obj.offsetParent) {
147+
objLeft += obj.offsetLeft;
148+
objTop += obj.offsetTop;
149+
obj = obj.offsetParent;
150+
}
151+
if (mouseEvent) {
152+
//FireFox
153+
xpos = mouseEvent.pageX;
154+
ypos = mouseEvent.pageY;
155+
}
156+
else {
157+
//IE
158+
xpos = window.event.x + document.body.scrollLeft - 2;
159+
ypos = window.event.y + document.body.scrollTop - 2;
160+
}
161+
xpos -= objLeft;
162+
ypos -= objTop;
163+
164+
return { x: xpos, y: ypos }
102165
}
103166

104167
bindMouseDrag() {

0 commit comments

Comments
 (0)