|
if("undefined"==typeof d3)throw"D3 SVG Overlay for Leaflet requires D3 library loaded first";if("undefined"==typeof L)throw"D3 SVG Overlay for Leaflet requires Leaflet library loaded first";d3.select("head").append("style").attr("type","text/css").text("svg.d3-overlay{pointer-events:none;position:absolute;}svg.d3-overlay>g.origin *{pointer-events:visiblePainted;}"),L.D3SvgOverlay=L.Class.extend({includes:L.Mixin.Events,options:function(t){return t===void 0?this._options:(t.zoomAnimate=t.zoomAnimate===void 0?!0:t.zoomAnimate,t.zoomHide=t.zoomHide===void 0?!t.zoomAnimate:t.zoomHide,t.zoomDraw=t.zoomDraw===void 0?!0:t.zoomDraw,t.jsAnimation=t.jsAnimation||!1,this._options=t)},draw:function(){this._drawCallback(this.selection,this.projection,this.map.getZoom())},initialize:function(t,i){this.options(i||{}),this._drawCallback=t},updSvg:function(){var t=this.map.getSize(),i=this.map.getPixelBounds(),s=this.map.getPixelOrigin();this._svg.attr("width",3*t.x).attr("height",3*t.y).attr("viewBox",[0,0,3*t.x,3*t.y].join(" ")).style("left",i.min.x-s.x-t.x+"px").style("top",i.min.y-s.y-t.y+"px"),this._svg.select("g.origin").attr("transform",["translate(",t.x-(i.min.x-s.x),",",t.y-(i.min.y-s.y),")"].join(""))},_zoomCalc:function(t){var i=t.zoom||this.map._zoom;this._zoomDiff=i-this._zoom,this._scale=Math.pow(2,this._zoomDiff),this._shift=this.map._latLngToNewLayerPoint(this._origin,i,t.center||this.map._initialCenter)},_zoomStart:function(){!this.translateAnim||this.translateAnim.remove(),!this.scaleAnim||this.scaleAnim.remove()},_zoomAnimate:function(){L.Browser.ie||this._options.jsAnimation?(this._gTranslate.transition().duration(250).attr("transform","translate("+this._shift.x+","+this._shift.y+")").ease(d3.ease("cubic-out")),this._gScale.transition().duration(250).attr("transform","scale("+this._scale+")").ease(d3.ease("cubic-out"))):(this.translateAnim=this._svg.append("animateTransform").attr("xlink:href",this._gTranslateXRef).attr("attributeName","transform").attr("attributeType","XML").attr("type","translate").attr("to",this._shift.x+","+this._shift.y).attr("dur","0.25s").attr("calcMode","spline").attr("keyTimes","0; 1").attr("keySplines","0 0 0.25 1").attr("begin","indefinite").attr("fill","freeze"),this.scaleAnim=this._svg.append("animateTransform").attr("xlink:href",this._gScaleXRef).attr("attributeName","transform").attr("attributeType","XML").attr("type","scale").attr("to",this._scale+","+this._scale).attr("dur","0.25s").attr("calcMode","spline").attr("keyTimes","0; 1").attr("keySplines","0 0 0.25 1").attr("begin","indefinite").attr("fill","freeze"),this.translateAnim.node().beginElement(),this.scaleAnim.node().beginElement())},_zoomEnd:function(){this._gTranslate.attr("transform","translate("+this._shift.x+","+this._shift.y+")"),this._gScale.attr("transform","scale("+this._scale+","+this._scale+")"),!this.translateAnim||this.translateAnim.remove(),!this.scaleAnim||this.scaleAnim.remove(),this._options.zoomDraw&&this.draw()},_zoomChange:function(){this._gTranslate.attr("transform","translate("+this._shift.x+","+this._shift.y+")"),this._gScale.attr("transform","scale("+this._scale+","+this._scale+")"),this._options.zoomDraw&&this.draw()},onAdd:function(t){this.map=t,this._zoomAnimated=this._options.zoomAnimate&&t._zoomAnimated;var i=this;this._svg=d3.select(t._panes.overlayPane).append("svg").classed({"d3-overlay":!0,"leaflet-zoom-hide":this._options.zoomHide}),this._gOrigin=this._svg.append("g").classed("origin",!0),this.map.on("moveend",this.updSvg,this),this._origin=this.map.layerPointToLatLng([0,0]),this._zoom=this.map.getZoom(),this._shift=L.point(0,0),this._scale=1,this.projection={latLngToLayerPoint:function(t,s){s=s||i._zoom;var a=i.map.project(L.latLng(t),s),e=i.map.project(i._origin,s);return a._subtract(e)},layerPointToLatLng:function(t,s){s=s||i._zoom;var a=i.map.project(i._origin,s);return i.map.unproject(t.add(a),s)},unitsPerMeter:256*Math.pow(2,i._zoom)/40075017,map:i.map,layer:i},this.projection.latLngToLayerFloatPoint=this.projection.latLngToLayerPoint,this.projection.getZoom=this.map.getZoom.bind(this.map),this.projection.getBounds=this.map.getBounds.bind(this.map);var s="id"+Math.random().toString(26).slice(2,7);this._gTranslateXRef="#"+s,this._gTranslate=this._gOrigin.append("g").classed("translate-anim",!0).attr("transform","translate(0,0)").attr("id",s),s="id"+Math.random().toString(26).slice(2,7),this._gScaleXRef="#"+s,this.selection=this._gScale=this._gTranslate.append("g").classed("scale-anim",!0).attr("transform","scale(1,1)").attr("id",s),this._zoomAnimated?(this.map.on("zoomanim",this._zoomCalc,this),this.map.on("zoomstart",this._zoomStart,this),this.map.on("zoomanim",this._zoomAnimate,this),this.map.on("zoomend",this._zoomEnd,this)):(this.map.on("viewreset",this._zoomCalc,this),this.map.on("viewreset",this._zoomChange,this)),this.updSvg(),this.draw()},onRemove:function(){this._svg.remove(),this.map.off(this._options.updateOn,this.draw,this),this.map.off("moveend",this.updSvg,this),this._zoomAnimated?(this.map.off("zoomanim",this._zoomCalc,this),this.map.off("zoomstart",this._zoomStart,this),this.map.off("zoomanim",this._zoomAnimate,this),this.map.off("zoomend",this._zoomEnd,this)):(this.map.off("viewreset",this._zoomCalc,this),this.map.off("viewreset",this._zoomChange,this))},addTo:function(t){return t.addLayer(this),this}}),L.D3SvgOverlay.version="2.0",L.d3SvgOverlay=function(t,i){return new L.D3SvgOverlay(t,i)}; |