@@ -115,15 +115,19 @@ window.Cl = window.Cl || {};
115115 event . preventDefault ( ) ;
116116 this . isDragging = true ;
117117
118- const clientX = event . type === 'touchstart' ? event . touches [ 0 ] . clientX : event . clientX ;
119- const clientY = event . type === 'touchstart' ? event . touches [ 0 ] . clientY : event . clientY ;
118+ const touch = event . type === 'touchstart' ? event . touches [ 0 ] : event ;
120119
121- this . dragStartX = clientX ;
122- this . dragStartY = clientY ;
120+ // Get container bounds
121+ const containerRect = this . container . getBoundingClientRect ( ) ;
122+
123+ // Calculate mouse position relative to container
124+ this . dragStartX = touch . clientX - containerRect . left ;
125+ this . dragStartY = touch . clientY - containerRect . top ;
123126
124- const rect = this . circle . getBoundingClientRect ( ) ;
125- this . circleStartX = rect . left ;
126- this . circleStartY = rect . top ;
127+ // Get current circle position (center)
128+ const circleRect = this . circle . getBoundingClientRect ( ) ;
129+ this . circleStartX = circleRect . left - containerRect . left + circleRect . width ;
130+ this . circleStartY = circleRect . top - containerRect . top + circleRect . height ;
127131
128132 document . addEventListener ( 'mousemove' , this . _onMouseMove ) ;
129133 document . addEventListener ( 'mouseup' , this . _onMouseUp ) ;
@@ -136,33 +140,36 @@ window.Cl = window.Cl || {};
136140
137141 event . preventDefault ( ) ;
138142
139- const clientX = event . type === 'touchmove' ? event . touches [ 0 ] . clientX : event . clientX ;
140- const clientY = event . type === 'touchmove' ? event . touches [ 0 ] . clientY : event . clientY ;
141-
142- const deltaX = clientX - this . dragStartX ;
143- const deltaY = clientY - this . dragStartY ;
143+ const touch = event . type === 'touchmove' ? event . touches [ 0 ] : event ;
144144
145+ // Get container bounds
145146 const containerRect = this . container . getBoundingClientRect ( ) ;
146- const circleRect = this . circle . getBoundingClientRect ( ) ;
147147
148- let newX = this . circleStartX - containerRect . left + deltaX ;
149- let newY = this . circleStartY - containerRect . top + deltaY ;
148+ // Calculate current mouse position relative to container
149+ const currentX = touch . clientX - containerRect . left ;
150+ const currentY = touch . clientY - containerRect . top ;
150151
151- // Constrain to container bounds
152- const minX = 0 ;
153- const minY = 0 ;
154- const maxX = containerRect . width - circleRect . width ;
155- const maxY = containerRect . height - circleRect . height ;
152+ // Calculate how much the mouse moved
153+ const deltaX = currentX - this . dragStartX ;
154+ const deltaY = currentY - this . dragStartY ;
156155
157- newX = Math . max ( minX , Math . min ( maxX , newX ) ) ;
158- newY = Math . max ( minY , Math . min ( maxY , newY ) ) ;
156+ // Calculate new center position
157+ let centerX = this . circleStartX + deltaX ;
158+ let centerY = this . circleStartY + deltaY ;
159159
160- this . circle . style . left = `${ newX } px` ;
161- this . circle . style . top = `${ newY } px` ;
160+ // Get circle dimensions
161+ const circleRect = this . circle . getBoundingClientRect ( ) ;
162+ const halfCircle = circleRect . width / 2 ;
162163
163- // Update location value (center of circle)
164- const centerX = newX + circleRect . width / 2 ;
165- const centerY = newY + circleRect . height / 2 ;
164+ // Constrain center to container bounds
165+ centerX = Math . max ( halfCircle , Math . min ( containerRect . width + halfCircle , centerX ) ) ;
166+ centerY = Math . max ( halfCircle , Math . min ( containerRect . height + halfCircle , centerY ) ) ;
167+
168+ // Position circle by its top-left corner (center - radius)
169+ this . circle . style . left = `${ centerX - halfCircle } px` ;
170+ this . circle . style . top = `${ centerY - halfCircle } px` ;
171+
172+ // Update location value with center coordinates
166173 this . _updateLocationValue ( centerX , centerY ) ;
167174 }
168175
@@ -237,4 +244,11 @@ window.Cl = window.Cl || {};
237244
238245 window . Cl . FocalPoint = FocalPoint ;
239246 window . Cl . FocalPointConstructor = FocalPointConstructor ;
247+
248+ // Export for ES6 module usage
249+ if ( typeof module !== 'undefined' && module . exports ) {
250+ module . exports = FocalPoint ;
251+ }
240252} ) ( ) ;
253+
254+ export default window . Cl . FocalPoint ;
0 commit comments