diff --git a/dist/maptalks.markercluster.js b/dist/maptalks.markercluster.js index 3ee860b..42a8d49 100644 --- a/dist/maptalks.markercluster.js +++ b/dist/maptalks.markercluster.js @@ -126,7 +126,9 @@ ClusterLayer.registerJSONType('ClusterLayer'); var defaultTextSymbol = { 'textFaceName': '"microsoft yahei"', - 'textSize': 16 + 'textSize': 16, + 'textDx': 0, + 'textDy': 0 }; var defaultSymbol = { @@ -155,11 +157,16 @@ ClusterLayer.registerRenderer('canvas', function (_maptalks$renderer$Ve) { } _class.prototype.checkResources = function checkResources() { + var symbol = this.layer.options['symbol'] || defaultSymbol; + if (symbol === this._symbolResourceChecked) { + return []; + } var resources = _maptalks$renderer$Ve.prototype.checkResources.apply(this, arguments); - var res = maptalks.Util.getExternalResources(this.layer.options['symbol'] || defaultSymbol, true); + var res = maptalks.Util.getExternalResources(symbol, true); if (res) { resources.push.apply(resources, res); } + this._symbolResourceChecked = symbol; return resources; }; @@ -203,7 +210,7 @@ ClusterLayer.registerRenderer('canvas', function (_maptalks$renderer$Ve) { width = sprite.canvas.width; height = sprite.canvas.height; pt = map._prjToContainerPoint(zoomClusters[p]['center']); - pExt = new maptalks.PointExtent(pt.substract(width, height), pt.add(width, height)); + pExt = new maptalks.PointExtent(pt.subs(width, height), pt.add(width, height)); if (!extent.intersects(pExt)) { continue; } @@ -237,17 +244,17 @@ ClusterLayer.registerRenderer('canvas', function (_maptalks$renderer$Ve) { _class.prototype.onGeometryAdd = function onGeometryAdd() { this._clusterNeedRedraw = true; - this.render(); + _maptalks$renderer$Ve.prototype.onGeometryAdd.apply(this, arguments); }; _class.prototype.onGeometryRemove = function onGeometryRemove() { this._clusterNeedRedraw = true; - this.render(); + _maptalks$renderer$Ve.prototype.onGeometryRemove.apply(this, arguments); }; _class.prototype.onGeometryPositionChange = function onGeometryPositionChange() { this._clusterNeedRedraw = true; - this.render(); + _maptalks$renderer$Ve.prototype.onGeometryPositionChange.apply(this, arguments); }; _class.prototype.onRemove = function onRemove() { @@ -318,11 +325,11 @@ ClusterLayer.registerRenderer('canvas', function (_maptalks$renderer$Ve) { _this4.completeRender(); } else { _this4._drawClusters(clusters, frame.styles.d); - _this4.requestMapToRender(); + _this4.setCanvasUpdated(); } }).play(); this._drawClusters(clusters, 0); - this.requestMapToRender(); + this.setCanvasUpdated(); } else { this._animated = false; this._drawClusters(clusters, 1); @@ -364,7 +371,7 @@ ClusterLayer.registerRenderer('canvas', function (_maptalks$renderer$Ve) { var pt = map._prjToContainerPoint(c['center']); if (c.parent) { var parent = map._prjToContainerPoint(c.parent['center']); - pt = parent.add(pt.substract(parent)._multi(ratio)); + pt = parent.add(pt.sub(parent)._multi(ratio)); } if (matrix) { pt = matrix.applyToPointInstance(pt); @@ -383,13 +390,15 @@ ClusterLayer.registerRenderer('canvas', function (_maptalks$renderer$Ve) { } ctx.globalAlpha = opacity * op; if (sprite) { - var pos = pt.add(sprite.offset)._substract(sprite.canvas.width / 2, sprite.canvas.height / 2); + var pos = pt.add(sprite.offset)._subs(sprite.canvas.width / 2, sprite.canvas.height / 2); ctx.drawImage(sprite.canvas, pos.x, pos.y); } if (this.layer.options['drawClusterText'] && grid['textSize']) { maptalks.Canvas.prepareCanvasFont(ctx, this._textSymbol); - maptalks.Canvas.fillText(ctx, grid['count'], pt.substract(grid['textSize'])); + var dx = this._textSymbol['textDx'] || 0; + var dy = this._textSymbol['textDy'] || 0; + maptalks.Canvas.fillText(ctx, grid['count'], pt.sub(grid['textSize']).add(dx, dy)); } ctx.globalAlpha = opacity; }; diff --git a/dist/maptalks.markercluster.min.js b/dist/maptalks.markercluster.min.js index 2b66c12..05a0b8d 100644 --- a/dist/maptalks.markercluster.min.js +++ b/dist/maptalks.markercluster.min.js @@ -6,4 +6,4 @@ /*! * requires maptalks@^0.23.0 */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("maptalks")):"function"==typeof define&&define.amd?define(["exports","maptalks"],e):e(t.maptalks=t.maptalks||{},t.maptalks)}(this,function(t,e){"use strict";function r(t,e){for(var r=Object.getOwnPropertyNames(e),o=0;oi)return delete this._currentClusters,this._markersToDraw=this.layer._geoList,void t.prototype.draw.apply(this,arguments);this._clusterNeedRedraw&&(this._clearDataCache(),this._computeGrid(),this._clusterNeedRedraw=!1);var n=this._clusterCache[o]?this._clusterCache[o].clusters:null;this._markersToDraw=[];var s=r.getContainerExtent(),a=[],u=void 0,l=void 0,c=void 0,p=void 0,h=void 0,y=void 0;for(var d in n)if(this._currentGrid=n[d],1!==n[d].count)c=this._getSprite(),p=c.canvas.width,h=c.canvas.height,u=r._prjToContainerPoint(n[d].center),l=new e.PointExtent(u.substract(p,h),u.add(p,h)),s.intersects(l)&&(y=e.StringUtil.getFont(this._textSymbol),n[d].textSize||(n[d].textSize=e.StringUtil.stringLength(n[d].count,y).toPoint()._multi(.5)),a.push(n[d]));else{var _=n[d].children[0];_._cluster=n[d],this._markersToDraw.push(_)}this._drawLayer(a)},r.prototype.forEachGeo=function(t,e){this._markersToDraw&&this._markersToDraw.forEach(function(r){e?t.call(e,r):t(r)})},r.prototype.drawOnInteracting=function(){this._currentClusters&&this._drawClusters(this._currentClusters,1),t.prototype.drawOnInteracting.apply(this,arguments)},r.prototype.onGeometryAdd=function(){this._clusterNeedRedraw=!0,this.render()},r.prototype.onGeometryRemove=function(){this._clusterNeedRedraw=!0,this.render()},r.prototype.onGeometryPositionChange=function(){this._clusterNeedRedraw=!0,this.render()},r.prototype.onRemove=function(){this._clearDataCache()},r.prototype.transform=function(t){return this._currentClusters&&this._drawClusters(this._currentClusters,1,t),!0},r.prototype.identify=function(t){var e=this.getMap();if(t=e.coordinateToContainerPoint(t),!this._currentClusters)return null;for(var r=this._currentGrid,o=0;o.5?1:e)})},r.prototype._drawCluster=function(t,r,o){this._currentGrid=r;var i=this.context,n=this._getSprite(),s=i.globalAlpha;if(s*o!==0){if(i.globalAlpha=s*o,n){var a=t.add(n.offset)._substract(n.canvas.width/2,n.canvas.height/2);i.drawImage(n.canvas,a.x,a.y)}this.layer.options.drawClusterText&&r.textSize&&(e.Canvas.prepareCanvasFont(i,this._textSymbol),e.Canvas.fillText(i,r.count,t.substract(r.textSize))),i.globalAlpha=s}},r.prototype._getSprite=function(){this._spriteCache||(this._spriteCache={});var t=e.Util.getSymbolStamp(this._symbol);return this._spriteCache[t]||(this._spriteCache[t]=new e.Marker([0,0],{symbol:this._symbol})._getSprite(this.resources,this.getMap().CanvasClass)),this._spriteCache[t]},r.prototype._initGridSystem=function(){var t=[],e=void 0,r=void 0;this.layer.forEach(function(o){r=o._getPrjCoordinates(),e=e?e._combine(o._getPrjExtent()):o._getPrjExtent(),t.push({x:r.x,y:r.y,id:o._getInternalId(),geometry:o})}),this._markerExtent=e,this._markerPoints=t},r.prototype._computeGrid=function(){var t=this.getMap(),e=t.getZoom();this._markerExtent||this._initGridSystem(),this._clusterCache||(this._clusterCache={});var r=t._getResolution(t.getMinZoom())>t._getResolution(t.getMaxZoom())?e-1:e+1;this._clusterCache[r]&&this._clusterCache[r].length===this.layer.getCount()&&(this._clusterCache[e]=this._clusterCache[r]),this._clusterCache[e]||(this._clusterCache[e]=this._computeZoomGrid(e))},r.prototype._computeZoomGrid=function(t){if(!this._markerExtent)return null;var r=this.getMap(),o=r._getResolution(t)*this.layer.options.maxClusterRadius,i=r._getResolution(t-1)?r._getResolution(t-1)*this.layer.options.maxClusterRadius:null,n=this._clusterCache[t-1];!n&&t-1>=r.getMinZoom()&&(this._clusterCache[t-1]=n=this._computeZoomGrid(t-1));for(var s=this._markerPoints,a={},u=this._markerExtent.getMin(),l=void 0,c=void 0,p=void 0,h=void 0,y=void 0,d=void 0,_=0,m=s.length;_e.to?"in":"out",this._stopAnim(),t.prototype.onZoomStart.call(this,e)},r.prototype.onZoomEnd=function(){this._animated=!0,this._computeGrid(),t.prototype.onZoomEnd.apply(this,arguments)},r.prototype._clearDataCache=function(){this._stopAnim(),delete this._markerExtent,delete this._markerPoints,delete this._clusterCache},r}(e.renderer.VectorLayerCanvasRenderer)),t.ClusterLayer=a,Object.defineProperty(t,"__esModule",{value:!0}),"undefined"!=typeof console&&console.log("maptalks.markercluster v0.4.1, requires maptalks@^0.23.0.")}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("maptalks")):"function"==typeof define&&define.amd?define(["exports","maptalks"],e):e(t.maptalks=t.maptalks||{},t.maptalks)}(this,function(t,e){"use strict";function r(t,e){for(var r=Object.getOwnPropertyNames(e),o=0;oi)return delete this._currentClusters,this._markersToDraw=this.layer._geoList,void t.prototype.draw.apply(this,arguments);this._clusterNeedRedraw&&(this._clearDataCache(),this._computeGrid(),this._clusterNeedRedraw=!1);var n=this._clusterCache[o]?this._clusterCache[o].clusters:null;this._markersToDraw=[];var s=r.getContainerExtent(),a=[],p=void 0,l=void 0,u=void 0,c=void 0,h=void 0,y=void 0;for(var d in n)if(this._currentGrid=n[d],1!==n[d].count)u=this._getSprite(),c=u.canvas.width,h=u.canvas.height,p=r._prjToContainerPoint(n[d].center),l=new e.PointExtent(p.subs(c,h),p.add(c,h)),s.intersects(l)&&(y=e.StringUtil.getFont(this._textSymbol),n[d].textSize||(n[d].textSize=e.StringUtil.stringLength(n[d].count,y).toPoint()._multi(.5)),a.push(n[d]));else{var _=n[d].children[0];_._cluster=n[d],this._markersToDraw.push(_)}this._drawLayer(a)},r.prototype.forEachGeo=function(t,e){this._markersToDraw&&this._markersToDraw.forEach(function(r){e?t.call(e,r):t(r)})},r.prototype.drawOnInteracting=function(){this._currentClusters&&this._drawClusters(this._currentClusters,1),t.prototype.drawOnInteracting.apply(this,arguments)},r.prototype.onGeometryAdd=function(){this._clusterNeedRedraw=!0,t.prototype.onGeometryAdd.apply(this,arguments)},r.prototype.onGeometryRemove=function(){this._clusterNeedRedraw=!0,t.prototype.onGeometryRemove.apply(this,arguments)},r.prototype.onGeometryPositionChange=function(){this._clusterNeedRedraw=!0,t.prototype.onGeometryPositionChange.apply(this,arguments)},r.prototype.onRemove=function(){this._clearDataCache()},r.prototype.transform=function(t){return this._currentClusters&&this._drawClusters(this._currentClusters,1,t),!0},r.prototype.identify=function(t){var e=this.getMap();if(t=e.coordinateToContainerPoint(t),!this._currentClusters)return null;for(var r=this._currentGrid,o=0;o.5?1:e)})},r.prototype._drawCluster=function(t,r,o){this._currentGrid=r;var i=this.context,n=this._getSprite(),s=i.globalAlpha;if(s*o!==0){if(i.globalAlpha=s*o,n){var a=t.add(n.offset)._subs(n.canvas.width/2,n.canvas.height/2);i.drawImage(n.canvas,a.x,a.y)}if(this.layer.options.drawClusterText&&r.textSize){e.Canvas.prepareCanvasFont(i,this._textSymbol);var p=this._textSymbol.textDx||0,l=this._textSymbol.textDy||0;e.Canvas.fillText(i,r.count,t.sub(r.textSize).add(p,l))}i.globalAlpha=s}},r.prototype._getSprite=function(){this._spriteCache||(this._spriteCache={});var t=e.Util.getSymbolStamp(this._symbol);return this._spriteCache[t]||(this._spriteCache[t]=new e.Marker([0,0],{symbol:this._symbol})._getSprite(this.resources,this.getMap().CanvasClass)),this._spriteCache[t]},r.prototype._initGridSystem=function(){var t=[],e=void 0,r=void 0;this.layer.forEach(function(o){r=o._getPrjCoordinates(),e=e?e._combine(o._getPrjExtent()):o._getPrjExtent(),t.push({x:r.x,y:r.y,id:o._getInternalId(),geometry:o})}),this._markerExtent=e,this._markerPoints=t},r.prototype._computeGrid=function(){var t=this.getMap(),e=t.getZoom();this._markerExtent||this._initGridSystem(),this._clusterCache||(this._clusterCache={});var r=t._getResolution(t.getMinZoom())>t._getResolution(t.getMaxZoom())?e-1:e+1;this._clusterCache[r]&&this._clusterCache[r].length===this.layer.getCount()&&(this._clusterCache[e]=this._clusterCache[r]),this._clusterCache[e]||(this._clusterCache[e]=this._computeZoomGrid(e))},r.prototype._computeZoomGrid=function(t){if(!this._markerExtent)return null;var r=this.getMap(),o=r._getResolution(t)*this.layer.options.maxClusterRadius,i=r._getResolution(t-1)?r._getResolution(t-1)*this.layer.options.maxClusterRadius:null,n=this._clusterCache[t-1];!n&&t-1>=r.getMinZoom()&&(this._clusterCache[t-1]=n=this._computeZoomGrid(t-1));for(var s=this._markerPoints,a={},p=this._markerExtent.getMin(),l=void 0,u=void 0,c=void 0,h=void 0,y=void 0,d=void 0,_=0,m=s.length;_e.to?"in":"out",this._stopAnim(),t.prototype.onZoomStart.call(this,e)},r.prototype.onZoomEnd=function(){this._animated=!0,this._computeGrid(),t.prototype.onZoomEnd.apply(this,arguments)},r.prototype._clearDataCache=function(){this._stopAnim(),delete this._markerExtent,delete this._markerPoints,delete this._clusterCache},r}(e.renderer.VectorLayerCanvasRenderer)),t.ClusterLayer=a,Object.defineProperty(t,"__esModule",{value:!0}),"undefined"!=typeof console&&console.log("maptalks.markercluster v0.4.1, requires maptalks@^0.23.0.")}); \ No newline at end of file diff --git a/index.js b/index.js index 0406b4b..b5b8dca 100644 --- a/index.js +++ b/index.js @@ -94,7 +94,9 @@ ClusterLayer.registerJSONType('ClusterLayer'); const defaultTextSymbol = { 'textFaceName' : '"microsoft yahei"', - 'textSize' : 16 + 'textSize' : 16, + 'textDx' : 0, + 'textDy' : 0 }; const defaultSymbol = { @@ -118,11 +120,16 @@ ClusterLayer.registerRenderer('canvas', class extends maptalks.renderer.VectorLa } checkResources() { + const symbol = this.layer.options['symbol'] || defaultSymbol; + if (symbol === this._symbolResourceChecked) { + return []; + } const resources = super.checkResources.apply(this, arguments); - const res = maptalks.Util.getExternalResources(this.layer.options['symbol'] || defaultSymbol, true); + const res = maptalks.Util.getExternalResources(symbol, true); if (res) { resources.push.apply(resources, res); } + this._symbolResourceChecked = symbol; return resources; } @@ -161,7 +168,7 @@ ClusterLayer.registerRenderer('canvas', class extends maptalks.renderer.VectorLa width = sprite.canvas.width; height = sprite.canvas.height; pt = map._prjToContainerPoint(zoomClusters[p]['center']); - pExt = new maptalks.PointExtent(pt.substract(width, height), pt.add(width, height)); + pExt = new maptalks.PointExtent(pt.subs(width, height), pt.add(width, height)); if (!extent.intersects(pExt)) { continue; } @@ -195,17 +202,17 @@ ClusterLayer.registerRenderer('canvas', class extends maptalks.renderer.VectorLa onGeometryAdd() { this._clusterNeedRedraw = true; - this.render(); + super.onGeometryAdd.apply(this, arguments); } onGeometryRemove() { this._clusterNeedRedraw = true; - this.render(); + super.onGeometryRemove.apply(this, arguments); } onGeometryPositionChange() { this._clusterNeedRedraw = true; - this.render(); + super.onGeometryPositionChange.apply(this, arguments); } onRemove() { @@ -273,13 +280,13 @@ ClusterLayer.registerRenderer('canvas', class extends maptalks.renderer.VectorLa this.completeRender(); } else { this._drawClusters(clusters, frame.styles.d); - this.requestMapToRender(); + this.setCanvasUpdated(); } } ) .play(); this._drawClusters(clusters, 0); - this.requestMapToRender(); + this.setCanvasUpdated(); } else { this._animated = false; this._drawClusters(clusters, 1); @@ -319,7 +326,7 @@ ClusterLayer.registerRenderer('canvas', class extends maptalks.renderer.VectorLa let pt = map._prjToContainerPoint(c['center']); if (c.parent) { const parent = map._prjToContainerPoint(c.parent['center']); - pt = parent.add(pt.substract(parent)._multi(ratio)); + pt = parent.add(pt.sub(parent)._multi(ratio)); } if (matrix) { pt = matrix.applyToPointInstance(pt); @@ -339,13 +346,15 @@ ClusterLayer.registerRenderer('canvas', class extends maptalks.renderer.VectorLa } ctx.globalAlpha = opacity * op; if (sprite) { - const pos = pt.add(sprite.offset)._substract(sprite.canvas.width / 2, sprite.canvas.height / 2); + const pos = pt.add(sprite.offset)._subs(sprite.canvas.width / 2, sprite.canvas.height / 2); ctx.drawImage(sprite.canvas, pos.x, pos.y); } if (this.layer.options['drawClusterText'] && grid['textSize']) { maptalks.Canvas.prepareCanvasFont(ctx, this._textSymbol); - maptalks.Canvas.fillText(ctx, grid['count'], pt.substract(grid['textSize'])); + const dx = this._textSymbol['textDx'] || 0; + const dy = this._textSymbol['textDy'] || 0; + maptalks.Canvas.fillText(ctx, grid['count'], pt.sub(grid['textSize']).add(dx, dy)); } ctx.globalAlpha = opacity; }