@@ -239,6 +239,8 @@ class MapboxCircle {
239239 /** @const {Array<Point>} */ this . _handles = undefined ;
240240 /** @const {boolean} */ this . _centerDragActive = false ;
241241 /** @const {boolean} */ this . _radiusDragActive = false ;
242+ /** @const {Object} */ this . _debouncedHandlers = { } ;
243+ /** @const {number} */ this . _updateCount = 0 ;
242244
243245 [ // Bind all event handlers.
244246 '_onZoomEnd' ,
@@ -282,6 +284,36 @@ class MapboxCircle {
282284 return window . navigator . userAgent . indexOf ( 'Chrome' ) === - 1 && window . navigator . userAgent . indexOf ( 'Safari' ) > - 1 ;
283285 }
284286
287+ /**
288+ * Add debounced event handler to map.
289+ * @param {string } event Mapbox GL event name
290+ * @param {Function } handler Event handler
291+ * @private
292+ */
293+ _mapOnDebounced ( event , handler ) {
294+ let ticking = false ;
295+ this . _debouncedHandlers [ handler ] = ( args ) => {
296+ if ( ! ticking ) {
297+ requestAnimationFrame ( ( ) => {
298+ handler ( args ) ;
299+ ticking = false ;
300+ } ) ;
301+ }
302+ ticking = true ;
303+ } ;
304+ this . map . on ( event , this . _debouncedHandlers [ handler ] ) ;
305+ }
306+
307+ /**
308+ * Remove debounced event handler from map.
309+ * @param {string } event Mapbox GL event name
310+ * @param {Function } handler Event handler
311+ * @private
312+ */
313+ _mapOffDebounced ( event , handler ) {
314+ this . map . off ( event , this . _debouncedHandlers [ handler ] ) ;
315+ }
316+
285317 /**
286318 * Re-calculate/update circle polygon and handles.
287319 * @private
@@ -306,9 +338,11 @@ class MapboxCircle {
306338 }
307339
308340 if ( this . options . debugEl ) {
341+ this . _updateCount += 1 ;
309342 this . options . debugEl . innerHTML = ( 'Center: ' + JSON . stringify ( this . getCenter ( ) ) + ' / Radius: ' + radius +
310343 ' / Bounds: ' + JSON . stringify ( this . getBounds ( ) ) + ' / Steps: ' + steps +
311- ' / Zoom: ' + zoom . toFixed ( 2 ) + ' / ID: ' + this . _instanceId ) ;
344+ ' / Zoom: ' + zoom . toFixed ( 2 ) + ' / ID: ' + this . _instanceId +
345+ ' / #: ' + this . _updateCount ) ;
312346 }
313347 }
314348
@@ -468,7 +502,7 @@ class MapboxCircle {
468502 */
469503 _onCenterHandleMouseDown ( ) {
470504 this . _centerDragActive = true ;
471- this . map . on ( 'mousemove' , this . _onCenterHandleMouseMove ) ;
505+ this . _mapOnDebounced ( 'mousemove' , this . _onCenterHandleMouseMove ) ;
472506 this . map . addLayer ( this . _getCenterHandleStrokeLayer ( ) , this . _circleCenterHandleId ) ;
473507 this . _suspendHandleListeners ( 'center' ) ;
474508 this . map . once ( 'mouseup' , this . _onCenterHandleMouseUpOrMapMouseOut ) ;
@@ -507,7 +541,7 @@ class MapboxCircle {
507541
508542 const newCenter = this . center ;
509543 this . _centerDragActive = false ;
510- this . map . off ( 'mousemove' , this . _onCenterHandleMouseMove ) ;
544+ this . _mapOffDebounced ( 'mousemove' , this . _onCenterHandleMouseMove ) ;
511545 switch ( event . type ) {
512546 case 'mouseup' : this . map . off ( 'mouseout' , this . _onCenterHandleMouseUpOrMapMouseOut ) ; break ;
513547 case 'mouseout' : this . map . off ( 'mouseup' , this . _onCenterHandleMouseUpOrMapMouseOut ) ; break ;
@@ -607,7 +641,7 @@ class MapboxCircle {
607641 */
608642 _onRadiusHandlesMouseDown ( event ) {
609643 this . _radiusDragActive = true ;
610- this . map . on ( 'mousemove' , this . _onRadiusHandlesMouseMove ) ;
644+ this . _mapOnDebounced ( 'mousemove' , this . _onRadiusHandlesMouseMove ) ;
611645 this . map . addLayer ( this . _getRadiusHandlesStrokeLayer ( ) , this . _circleRadiusHandlesId ) ;
612646 this . _suspendHandleListeners ( 'radius' ) ;
613647 this . map . once ( 'mouseup' , this . _onRadiusHandlesMouseUpOrMapMouseOut ) ;
@@ -646,7 +680,7 @@ class MapboxCircle {
646680
647681 const newRadius = this . radius ;
648682 this . _radiusDragActive = false ;
649- this . map . off ( 'mousemove' , this . _onRadiusHandlesMouseMove ) ;
683+ this . _mapOffDebounced ( 'mousemove' , this . _onRadiusHandlesMouseMove ) ;
650684 this . map . removeLayer ( this . _circleRadiusHandlesStrokeId ) ;
651685 switch ( event . type ) {
652686 case 'mouseup' : this . map . off ( 'mouseout' , this . _onRadiusHandlesMouseUpOrMapMouseOut ) ; break ;
0 commit comments