Skip to content

Commit 42541df

Browse files
committed
Merge branch 'release/1.6.1'
2 parents e835061 + fe5524d commit 42541df

File tree

4 files changed

+104
-39
lines changed

4 files changed

+104
-39
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ Returns **{sw: {lat: [number](https://developer.mozilla.org/en-US/docs/Web/JavaS
186186

187187
## Changelog
188188

189+
### v. 1.6.1
190+
191+
- Improved move animation ([#55](https://github.com/mblomdahl/mapbox-gl-circle/issues/55))
192+
189193
### v. 1.6.0
190194

191195
- Add optional `before` argument to _MapboxCircle.addTo_

lib/main.js

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,19 @@ class MapboxCircle {
6868
}
6969

7070
/**
71-
* @return {string} Unique circle handles source ID.
71+
* @return {string} Unique circle center handle source ID.
7272
* @private
7373
*/
74-
get _circleHandlesSourceId() {
75-
return 'circle-handles-source-' + this._instanceId;
74+
get _circleCenterHandleSourceId() {
75+
return 'circle-center-handle-source-' + this._instanceId;
76+
}
77+
78+
/**
79+
* @return {string} Unique radius handles source ID.
80+
* @private
81+
*/
82+
get _circleRadiusHandlesSourceId() {
83+
return 'circle-radius-handles-source-' + this._instanceId;
7684
}
7785

7886
/**
@@ -92,11 +100,19 @@ class MapboxCircle {
92100
}
93101

94102
/**
95-
* @return {string} Unique ID for edit handles stroke.
103+
* @return {string} Unique ID for center handle stroke.
96104
* @private
97105
*/
98-
get _circleHandlesStrokeId() {
99-
return 'circle-handles-stroke-' + this._instanceId;
106+
get _circleCenterHandleStrokeId() {
107+
return 'circle-center-handle-stroke-' + this._instanceId;
108+
}
109+
110+
/**
111+
* @return {string} Unique ID for radius handles stroke.
112+
* @private
113+
*/
114+
get _circleRadiusHandlesStrokeId() {
115+
return 'circle-radius-handles-stroke-' + this._instanceId;
100116
}
101117

102118
/**
@@ -306,17 +322,25 @@ class MapboxCircle {
306322
}
307323

308324
/**
309-
* Return GeoJSON for edit handles and stroke.
325+
* Return GeoJSON for center handle and stroke.
310326
* @private
311327
* @return {FeatureCollection}
312328
*/
313-
_getHandlesGeoJSON() {
314-
const features = [...this._handles, turfHelpers.point(this.center, {'type': 'center'})];
315-
if (!(this._centerDragActive && this.radius < 10000)) {
316-
features.push(this._circle);
329+
_getCenterHandleGeoJSON() {
330+
if (this._centerDragActive && this.radius < 10000) {
331+
return turfHelpers.featureCollection([turfHelpers.point(this.center)]);
332+
} else {
333+
return turfHelpers.featureCollection([turfHelpers.point(this.center), this._circle]);
317334
}
335+
}
318336

319-
return turfHelpers.featureCollection(features);
337+
/**
338+
* Return GeoJSON for radius handles and stroke.
339+
* @private
340+
* @return {FeatureCollection}
341+
*/
342+
_getRadiusHandlesGeoJSON() {
343+
return turfHelpers.featureCollection([...this._handles, this._circle]);
320344
}
321345

322346
/**
@@ -329,7 +353,12 @@ class MapboxCircle {
329353
}
330354

331355
if (this.options.editable) {
332-
this._map.getSource(this._circleHandlesSourceId).setData(this._getHandlesGeoJSON());
356+
if (!this._radiusDragActive) {
357+
this._map.getSource(this._circleCenterHandleSourceId).setData(this._getCenterHandleGeoJSON());
358+
}
359+
if (!this._centerDragActive) {
360+
this._map.getSource(this._circleRadiusHandlesSourceId).setData(this._getRadiusHandlesGeoJSON());
361+
}
333362
}
334363
}
335364

@@ -440,7 +469,7 @@ class MapboxCircle {
440469
_onCenterHandleMouseDown() {
441470
this._centerDragActive = true;
442471
this.map.on('mousemove', this._onCenterHandleMouseMove);
443-
this.map.addLayer(this._getCircleHandlesStrokeLayer(), this._circleCenterHandleId);
472+
this.map.addLayer(this._getCenterHandleStrokeLayer(), this._circleCenterHandleId);
444473
this._suspendHandleListeners('center');
445474
this.map.once('mouseup', this._onCenterHandleMouseUpOrMapMouseOut);
446475
this.map.once('mouseout', this._onCenterHandleMouseUpOrMapMouseOut); // Deactivate drag if mouse leaves canvas.
@@ -471,7 +500,7 @@ class MapboxCircle {
471500
case 'mouseout': this.map.off('mouseup', this._onCenterHandleMouseUpOrMapMouseOut); break;
472501
}
473502
this._resumeHandleListeners('center');
474-
this.map.removeLayer(this._circleHandlesStrokeId);
503+
this.map.removeLayer(this._circleCenterHandleStrokeId);
475504
this._resetHandles(this._circleCenterHandleId);
476505
if (newCenter[0] !== this._lastCenterLngLat[0] || newCenter[1] !== this._lastCenterLngLat[1]) {
477506
this.center = newCenter;
@@ -566,7 +595,7 @@ class MapboxCircle {
566595
_onRadiusHandlesMouseDown(event) {
567596
this._radiusDragActive = true;
568597
this.map.on('mousemove', this._onRadiusHandlesMouseMove);
569-
this.map.addLayer(this._getCircleHandlesStrokeLayer(), this._circleRadiusHandlesId);
598+
this.map.addLayer(this._getRadiusHandlesStrokeLayer(), this._circleRadiusHandlesId);
570599
this._suspendHandleListeners('radius');
571600
this.map.once('mouseup', this._onRadiusHandlesMouseUpOrMapMouseOut);
572601
this.map.once('mouseout', this._onRadiusHandlesMouseUpOrMapMouseOut); // Deactivate drag if mouse leaves canvas.
@@ -592,7 +621,7 @@ class MapboxCircle {
592621
const newRadius = this.radius;
593622
this._radiusDragActive = false;
594623
this.map.off('mousemove', this._onRadiusHandlesMouseMove);
595-
this.map.removeLayer(this._circleHandlesStrokeId);
624+
this.map.removeLayer(this._circleRadiusHandlesStrokeId);
596625
switch (event.type) {
597626
case 'mouseup': this.map.off('mouseout', this._onRadiusHandlesMouseUpOrMapMouseOut); break;
598627
case 'mouseout': this.map.off('mouseup', this._onRadiusHandlesMouseUpOrMapMouseOut); break;
@@ -869,13 +898,25 @@ class MapboxCircle {
869898
}
870899

871900
/**
872-
* @return {Object} GeoJSON map source for edit handles.
901+
* @return {Object} GeoJSON map source for center handle.
873902
* @private
874903
*/
875-
_getHandlesMapSource() {
904+
_getCenterHandleMapSource() {
876905
return {
877906
type: 'geojson',
878-
data: this._getHandlesGeoJSON(),
907+
data: this._getCenterHandleGeoJSON(),
908+
buffer: 1
909+
};
910+
}
911+
912+
/**
913+
* @return {Object} GeoJSON map source for radius handles.
914+
* @private
915+
*/
916+
_getRadiusHandlesMapSource() {
917+
return {
918+
type: 'geojson',
919+
data: this._getRadiusHandlesGeoJSON(),
879920
buffer: 1
880921
};
881922
}
@@ -916,44 +957,62 @@ class MapboxCircle {
916957
}
917958

918959
/**
919-
* @return {Object} Style layer for the edit handles' stroke.
960+
* @return {Object} Style layer for the center handle's stroke.
920961
* @private
921962
*/
922-
_getCircleHandlesStrokeLayer() {
963+
_getCenterHandleStrokeLayer() {
923964
if (this._centerDragActive && this.radius < 10000) {
924965
// Inspired by node_modules/mapbox-gl/src/ui/control/scale_control.js:getDistance
925966
const y = this.map._container.clientHeight / 2;
926967
const x = this.map._container.clientWidth;
927968
const horizontalPixelsPerMeter = x / turfDistance(
928969
this.map.unproject([0, y]).toArray(), this.map.unproject([x, y]).toArray(), 'meters');
929970
return {
930-
id: this._circleHandlesStrokeId,
971+
id: this._circleCenterHandleStrokeId,
931972
type: 'circle',
932-
source: this._circleHandlesSourceId,
973+
source: this._circleCenterHandleSourceId,
933974
paint: {
934975
'circle-radius': horizontalPixelsPerMeter * this.radius,
935976
'circle-opacity': 0,
936977
'circle-stroke-color': this.options.strokeColor,
937-
'circle-stroke-opacity': this.options.strokeOpacity,
978+
'circle-stroke-opacity': this.options.strokeOpacity * .5,
938979
'circle-stroke-width': this.options.strokeWeight
939980
},
940-
filter: ['all', ['==', '$type', 'Point'], ['==', 'type', 'center']]
981+
filter: ['==', '$type', 'Point']
941982
};
942983
} else {
943984
return {
944-
id: this._circleHandlesStrokeId,
985+
id: this._circleCenterHandleStrokeId,
945986
type: 'line',
946-
source: this._circleHandlesSourceId,
987+
source: this._circleCenterHandleSourceId,
947988
paint: {
948989
'line-color': this.options.strokeColor,
949990
'line-width': this.options.strokeWeight,
950-
'line-opacity': this.options.strokeOpacity
991+
'line-opacity': this.options.strokeOpacity * 0.5
951992
},
952993
filter: ['==', '$type', 'Polygon']
953994
};
954995
}
955996
}
956997

998+
/**
999+
* @return {Object} Style layer for the radius handles' stroke.
1000+
* @private
1001+
*/
1002+
_getRadiusHandlesStrokeLayer() {
1003+
return {
1004+
id: this._circleRadiusHandlesStrokeId,
1005+
type: 'line',
1006+
source: this._circleRadiusHandlesSourceId,
1007+
paint: {
1008+
'line-color': this.options.strokeColor,
1009+
'line-width': this.options.strokeWeight,
1010+
'line-opacity': this.options.strokeOpacity * 0.5
1011+
},
1012+
filter: ['==', '$type', 'Polygon']
1013+
};
1014+
}
1015+
9571016
/**
9581017
* @return {Object} Default paint style for edit handles.
9591018
* @private
@@ -969,30 +1028,30 @@ class MapboxCircle {
9691028
}
9701029

9711030
/**
972-
* @return {Object} Style layer for the circle's center edit handle.
1031+
* @return {Object} Style layer for the circle's center handle.
9731032
* @private
9741033
*/
9751034
_getCircleCenterHandleLayer() {
9761035
return {
9771036
id: this._circleCenterHandleId,
9781037
type: 'circle',
979-
source: this._circleHandlesSourceId,
1038+
source: this._circleCenterHandleSourceId,
9801039
paint: this._getEditHandleDefaultPaintOptions(),
981-
filter: ['all', ['==', '$type', 'Point'], ['==', 'type', 'center']]
1040+
filter: ['==', '$type', 'Point']
9821041
};
9831042
}
9841043

9851044
/**
986-
* @return {Object} Style layer for the circle's radius edit handles.
1045+
* @return {Object} Style layer for the circle's radius handles.
9871046
* @private
9881047
*/
9891048
_getCircleRadiusHandlesLayer() {
9901049
return {
9911050
id: this._circleRadiusHandlesId,
9921051
type: 'circle',
993-
source: this._circleHandlesSourceId,
1052+
source: this._circleRadiusHandlesSourceId,
9941053
paint: this._getEditHandleDefaultPaintOptions(),
995-
filter: ['all', ['==', '$type', 'Point'], ['!=', 'type', 'center']]
1054+
filter: ['==', '$type', 'Point']
9961055
};
9971056
}
9981057

@@ -1057,7 +1116,8 @@ class MapboxCircle {
10571116
map.on('zoomend', this._onZoomEnd);
10581117

10591118
if (this.options.editable) {
1060-
map.addSource(this._circleHandlesSourceId, this._getHandlesMapSource());
1119+
map.addSource(this._circleCenterHandleSourceId, this._getCenterHandleMapSource());
1120+
map.addSource(this._circleRadiusHandlesSourceId, this._getRadiusHandlesMapSource());
10611121

10621122
map.addLayer(this._getCircleCenterHandleLayer());
10631123
this._bindCenterHandleListeners(map);
@@ -1112,7 +1172,8 @@ class MapboxCircle {
11121172
this._unbindCenterHandleListeners();
11131173
this.map.removeLayer(this._circleCenterHandleId);
11141174

1115-
this.map.removeSource(this._circleHandlesSourceId);
1175+
this.map.removeSource(this._circleRadiusHandlesSourceId);
1176+
this.map.removeSource(this._circleCenterHandleSourceId);
11161177
}
11171178

11181179
this.map.off('zoomend', this._onZoomEnd);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mapbox-gl-circle",
3-
"version": "1.6.0",
3+
"version": "1.6.1",
44
"author": "Smith Micro Software, Inc.",
55
"license": "ISC",
66
"description": "A google.maps.Circle replacement for Mapbox GL JS API",

0 commit comments

Comments
 (0)