cesium-examples/map/2d/build/release/baidu-map-lineGradient.min.js

1 line
7.5 KiB
JavaScript

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.LineGradient=e()}(this,function(){"use strict";function u(t){this.options=t||{},this.paneName=this.options.paneName||"labelPane",this.zIndex=this.options.zIndex||0,this._map=t.map,this._lastDrawTime=null,this.show()}(u.prototype=new BMap.Overlay).initialize=function(t){this._map=t;var e=this.canvas=document.createElement("canvas"),i=this.ctx=this.canvas.getContext("2d");e.style.cssText="position:absolute;left:0;top:0;z-index:"+this.zIndex+";",this.adjustSize(),this.adjustRatio(i),t.getPanes()[this.paneName].appendChild(e);var n=this;return t.addEventListener("resize",function(){n.adjustSize(),n._draw()}),this.canvas},u.prototype.adjustSize=function(){var t=this._map.getSize(),e=this.canvas;e.width=t.width,e.height=t.height,e.style.width=e.width+"px",e.style.height=e.height+"px"},u.prototype.adjustRatio=function(t){var e=t.backingStorePixelRatio||t.webkitBackingStorePixelRatio||t.mozBackingStorePixelRatio||t.msBackingStorePixelRatio||t.oBackingStorePixelRatio||t.backingStorePixelRatio||1,i=(window.devicePixelRatio||1)/e,n=t.canvas.width,o=t.canvas.height;t.canvas.width=n*i,t.canvas.height=o*i,t.canvas.style.width=n+"px",t.canvas.style.height=o+"px",t.scale(i,i)},u.prototype.draw=function(){var t=this;clearTimeout(t.timeoutID),t.timeoutID=setTimeout(function(){t._draw()},15)},u.prototype._draw=function(){var t=this._map,e=t.getSize(),i=t.getCenter();if(i){var n=t.pointToOverlayPixel(i);this.canvas.style.left=n.x-e.width/2+"px",this.canvas.style.top=n.y-e.height/2+"px",this.dispatchEvent("draw"),this.options.update&&this.options.update.call(this)}},u.prototype.getContainer=function(){return this.canvas},u.prototype.show=function(){this.canvas||this._map.addOverlay(this),this.canvas.style.display="block"},u.prototype.hide=function(){this.canvas.style.display="none"},u.prototype.setZIndex=function(t){this.canvas.style.zIndex=t},u.prototype.getZIndex=function(){return this.zIndex};var r={merge:function(e,i){Object.keys(e).forEach(function(t){i[t]=e[t]})},getDistance:function(t,e){return Math.sqrt((t[0]-e[0])*(t[0]-e[0])+(t[1]-e[1])*(t[1]-e[1]))},containStroke:function(t,e,i,n,o,a,s){if(0===o)return!1;var l=o,r=0;if(e+l<s&&n+l<s||s<e-l&&s<n-l||t+l<a&&i+l<a||a<t-l&&a<i-l)return!1;if(t===i)return Math.abs(a-t)<=l/2;var h=(r=(e-n)/(t-i))*a-s+(t*n-i*e)/(t-i);return h*h/(r*r+1)<=l/2*l/2},isPointInRect:function(t,e){var i=e.wn,n=e.es;return t.x>=i.x&&t.x<=n.x&&t.y>=i.y&&t.y<=n.y},isPointInCircle:function(t,e,i){return this.getDistanceNew(t,e)<=i},getDistanceNew:function(t,e){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))}},e="undefined"==typeof window?{}:window,f=e.requestAnimationFrame||e.mozRequestAnimationFrame||e.webkitRequestAnimationFrame||e.msRequestAnimationFrame||function(t){return e.setTimeout(t,1e3/60)},t=function(i,t){var o=this;o.map=i,o.lines=[],o.pixelList=[];var a={lineWidth:1},n=null,s=null,l=!0,r=i.getSize().width,h=i.getSize().height;function p(t){this.name=t.name,this.label=t.label,this.labelColor=t.labelColor,this.path=t.path,this.step=0}p.prototype.getPointList=function(){var e=[],t=this.path;return t&&0<t.length&&t.forEach(function(t){e.push({code:t.code,name:t.name,location:t.location,pixel:i.pointToPixel(t.location),height:t.height,value:t.value,time:t.time,color:t.color})}),e},p.prototype.draw=function(t){for(var e=this.pixelList||this.getPointList(),i=0,n=e.length;i<n-1;i++)t.save(),t.beginPath(),t.lineWidth=a.lineWidth,t.strokeStyle=e[i].color,t.moveTo(e[i].pixel.x,e[i].pixel.y),t.lineTo(e[i+1].pixel.x,e[i+1].pixel.y),t.stroke(),t.closePath(),t.restore();var o=e[e.length-1];t.font="bold 14px Arial",t.textAlign="left",t.textBaseline="middle",t.fillStyle=this.labelColor,t.fillText(this.label,o.pixel.x,o.pixel.y)},p.prototype.drawMoveCircle=function(t){var e=this.pixelList||this.getPointList();t.save(),t.fillStyle="#fff",t.shadowColor="#fff",t.shadowBlur=5,t.beginPath(),t.arc(e[this.step].pixel.x,e[this.step].pixel.y,3,0,2*Math.PI,!0),t.fill(),t.closePath(),t.restore(),this.step+=1,this.step>=e.length&&(this.step=0)},p.prototype.drawArrow=function(t,e){var i=this.pixelList||this.getPointList();this.step>=i.length-1&&(this.step=0),t.beginPath(),t.lineWidth=5,t.strokeStyle="#fff",t.moveTo(i[this.step].pixel.x,i[this.step].pixel.y),t.lineTo(i[this.step+1].pixel.x,i[this.step+1].pixel.y),t.stroke(),t.save(),t.translate(i[this.step+1].pixel.x,i[this.step+1].pixel.y);var n=(i[this.step+1].pixel.x-i[this.step].pixel.x)/(i[this.step+1].pixel.y-i[this.step].pixel.y);n=Math.atan(n),0<=i[this.step+1].pixel.y-i[this.step].pixel.y?t.rotate(-n):t.rotate(Math.PI-n),t.lineTo(-6,-6),t.lineTo(0,6),t.lineTo(6,-6),t.lineTo(0,0),t.fillStyle="#fff",t.fill(),t.restore(),this.step+=1};var e=function(){var t=s.canvas.getContext("2d");if(t)if(l){t.fillStyle="rgba(0,0,0,.2)";var e=t.globalCompositeOperation;t.globalCompositeOperation="destination-in",t.fillRect(0,0,r,h),t.globalCompositeOperation=e;for(var i=o.lines,n=0;n<i.length;n++){i[n].drawArrow(t)}}else t.clearRect(0,0,r,h)},c=function(){if(!(o.lines&&0<o.lines.length)){var t=a.data,n=new d;t.forEach(function(t,e){var i=new p({name:t.name,label:t.label,labelColor:t.labelColor,path:[]});t.data.forEach(function(t,e){i.path.push({code:t.code,name:t.name,location:new BMap.Point(t.Longitude,t.Latitude),height:t.height,value:t.value,time:t.time,color:n.getColor(t.value).color})}),o.lines.push(i)})}},d=function(){var n={width:400,height:15,range:[700,750,800,850,900,950,1e3,1050,1100],gradient:{.1:"#fe0000",.4:"#ffaa01",.7:"#b0e000",1:"#38a702"}},l=document.createElement("canvas"),r=l.getContext("2d");l.width=n.width,l.height=n.height;var t=r.createLinearGradient(0,0,l.width,l.height);for(var e in n.gradient)t.addColorStop(e,n.gradient[e]);r.fillStyle=t,r.fillRect(0,0,l.width,l.height),this.d2Hex=function(t){for(var e=Number(t).toString(16);e.length<2;)e="0"+e;return e.toUpperCase()},this.getRgbColor=function(t){for(var e=r.getImageData(0,0,l.width,l.height).data,i=4*(t.Y*l.width+t.X),n=[],o="#",a={rgb:n,color:o},s=0;s<3;s++)n.push(e[i+s]),o+=this.d2Hex(e[i+s]);return a.color=o,a},this.getColor=function(t){var e=t-n.range[0],i=Math.round(e*l.width/(n.range[n.range.length-1]-n.range[0]));return this.getRgbColor({X:i,Y:1})}};o.init(t,a),n=new u({map:i,update:function(){var e=n.canvas.getContext("2d");e&&(c(),e.clearRect(0,0,r,h),o.pixelList=[],o.lines.forEach(function(t){o.pixelList.push({name:t.name,label:t.label,labelColor:t.labelColor,data:t.getPointList()}),t.draw(e)}))}}),s=new u({map:i,update:e}),i.addEventListener("movestart",function(){l=!1}),i.addEventListener("moveend",function(){l=!0,o.lines=[]}),i.addEventListener("zoomstart",function(){l=!1}),i.addEventListener("zoomend",function(){l=!0,o.lines=[]}),function t(){o.timer=setTimeout(function(){o.animationId=f(t),e()},100)}(),this.clickEvent=this.clickEvent.bind(this),this.bindEvent()};return t.prototype.init=function(t,e){r.merge(t,e),this.options=e},t.prototype.bindEvent=function(t){var e=this.map;this.options.methods&&(this.options.methods.click&&(e.setDefaultCursor("default"),e.addEventListener("click",this.clickEvent)),this.options.methods.mousemove&&(e.setDefaultCursor("default"),e.addEventListener("mousemove",this.clickEvent)))},t.prototype.clickEvent=function(a){var s=this,l=!1,t=s.pixelList;0<t.length&&(t.forEach(function(t,e){for(var i=0;i<t.data.length;i++){var n=t.data[i].pixel;if(null==t.data[i+1])return;t.data[i+1].pixel;var o=a.pixel;if(r.isPointInCircle(o,n,s.options.lineWidth))return s.options.methods.mousemove(a,t.data[i]),void(l=!0)}}),l||(document.getElementById("tooltips").style.visibility="hidden"))},t});