@@ -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