UNPKG

wind-layer

Version:

a openlayers ol bmap amap maptalks extension to windjs

2 lines (1 loc) 10.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).windLayer=e()}(this,function(){"use strict";function l(u){void 0===u&&(u={}),this.params=u;var p=this;function e(t){t.projection||(t.projection="EPSG:4326"),p.MIN_VELOCITY_INTENSITY=t.minVelocity||0,p.MAX_VELOCITY_INTENSITY=t.maxVelocity||10,p.VELOCITY_SCALE=(t.velocityScale||.005)*(Math.pow(window.devicePixelRatio,1/3)||1),p.MAX_PARTICLE_AGE=t.particleAge||90,p.PARTICLE_LINE_WIDTH=t.lineWidth||1,p.PARTICLE_MULTIPLIER=t.particleMultiplier||1/300,p.PARTICLE_REDUCTION=Math.pow(window.devicePixelRatio,1/3)||1.6,p.FRAME_RATE=t.frameRate||16,p.COLOR_SCALE=t.colorScale||i,p.DEVICEPIXELRATIO=t.devicePixelRatio||1}p.canvas=u.canvas;var i=["rgb(36,104, 180)","rgb(60,157, 194)","rgb(128,205,193 )","rgb(151,218,168 )","rgb(198,231,181)","rgb(238,247,217)","rgb(255,238,159)","rgb(252,217,125)","rgb(255,182,100)","rgb(252,150,75)","rgb(250,112,52)","rgb(245,64,32)","rgb(237,45,28)","rgb(220,24,32)","rgb(180,0,35)"];function n(t,e,i,a,n,o){var r=1-t,s=1-e,h=r*s,c=t*s,l=r*e,u=t*e,d=i[0]*h+a[0]*c+n[0]*l+o[0]*u,p=i[1]*h+a[1]*c+n[1]*l+o[1]*u;return[d,p,Math.sqrt(d*d+p*p)]}function h(t){var e=null,i=null;return t.forEach(function(t){switch(t.header.parameterCategory+","+t.header.parameterNumber){case"1,2":case"2,2":e=t;break;case"1,3":case"2,3":i=t}}),function(t,e){var i=t.data,a=e.data;return{header:t.header,data:function(t){return[i[t],a[t]]},interpolate:n}}(e,i)}function o(a,n,t){function o(t,e){var i=a[Math.round(t)];return i&&i[Math.round(e)]||r}o.release=function(){a=[]},o.randomize=function(t){for(var e,i,a=0;null===o(e=Math.round(Math.floor(Math.random()*n.width)+n.x),i=Math.round(Math.floor(Math.random()*n.height)+n.y))[2]&&a++<30;);return t.x=e,t.y=i,t},t(n,o)}function c(t){return t/180*Math.PI}function l(t){return t/(Math.PI/180)}e(u),window.FRAME_TIME=1e3/p.FRAME_RATE;var f,m,d,w,M,v,g,y,E,x,r=[NaN,NaN,null],s=p.params.data,I=function(t,e){if(!m)return null;var i,a=A(t-w,360)/v,n=(M-e)/g,o=Math.floor(a),r=o+1,s=Math.floor(n),h=s+1;if(i=m[s]){var c=i[o],l=i[r];if(_(c)&&_(l)&&(i=m[h])){var u=i[o],d=i[r];if(_(u)&&_(d))return f.interpolate(a-o,n-s,c,l,u,d)}}return null},_=function(t){return null!=t},A=function(t,e){return t-e*Math.floor(t/e)},C=function(t,e,i,a,n,o){var r=2*Math.PI,s="EPSG:4326"===p.params.projection?5:Math.pow(10,-5.2),h=e<0?s:-s,c=i<0?s:-s,l=L(i,e+h,o),u=L(i+c,e,o),d=Math.cos(i/360*r);return[(l[0]-a)/h/d,(l[1]-n)/h/d,(u[0]-a)/c,(u[1]-n)/c]};function T(t){return Math.log(Math.tan(t/2+Math.PI/4))}function P(a,s){var e,i,h=(e=p.MIN_VELOCITY_INTENSITY,i=p.MAX_VELOCITY_INTENSITY,p.COLOR_SCALE.indexFor=function(t){return Math.max(0,Math.min(p.COLOR_SCALE.length-1,Math.round((t-e)/(i-e)*(p.COLOR_SCALE.length-1))))},p.COLOR_SCALE),c=h.map(function(){return[]}),t=Math.round(a.width*a.height*p.PARTICLE_MULTIPLIER);/android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i.test(navigator.userAgent)&&(t*=p.PARTICLE_REDUCTION);for(var n=[],o=0;o<t;o++)n.push(s.randomize({age:Math.floor(Math.random()*p.MAX_PARTICLE_AGE)+0}));var r=p.canvas.getContext("2d");r.lineWidth=p.PARTICLE_LINE_WIDTH,r.fillStyle="rgba(0, 0, 0, 0.97)",r.globalAlpha=.6;var l=Date.now();!function t(){R=requestAnimationFrame(t);var e=Date.now(),i=e-l;i>FRAME_TIME&&(l=e-i%FRAME_TIME,c.forEach(function(t){t.length=0}),n.forEach(function(t){t.age>p.MAX_PARTICLE_AGE&&(s.randomize(t).age=0);var e=t.x,i=t.y,a=s(e,i),n=a[2];if(null===n)t.age=p.MAX_PARTICLE_AGE;else{var o=e+a[0],r=i+a[1];null!==s(o,r)[2]?(t.xt=o,t.yt=r,c[h.indexFor(n)].push(t)):(t.x=o,t.y=r)}t.age+=1}),r.globalCompositeOperation="destination-in",r.fillRect(a.x,a.y,p.canvas.width,p.canvas.height),r.globalCompositeOperation="lighter",r.globalAlpha=.9,c.forEach(function(t,e){0<t.length&&(r.beginPath(),r.strokeStyle=h[e],t.forEach(function(t){r.moveTo(t.x*p.DEVICEPIXELRATIO,t.y*p.DEVICEPIXELRATIO),r.lineTo(t.xt*p.DEVICEPIXELRATIO,t.yt*p.DEVICEPIXELRATIO),t.x=t.xt,t.y=t.yt}),r.stroke())}),u.onDraw&&u.onDraw())}()}x="EPSG:4326"===p.params.projection?function(t,e,i){var a=i.east-i.west,n=i.south-i.north,o=l(i.north)+e/i.height*l(n);return[l(i.west)+t/i.width*l(a),o]}:function(t,e,i){var a=i.east-i.west,n=i.width/l(a)*360/(2*Math.PI),o=n/2*Math.log((1+Math.sin(i.south))/(1-Math.sin(i.south))),r=(i.height+o-e)/n,s=180/Math.PI*(2*Math.atan(Math.exp(r))-Math.PI/2);return[l(i.west)+t/i.width*l(a),s]};var R,L=function(t,e,i){var a=T(i.south),n=T(i.north),o=i.width/(i.east-i.west),r=i.height/(n-a),s=T(c(t));return[(c(e)-i.west)*o,s=(n-s)*r]},b=function(e,i,a,t){var n={south:c(t[0][1]),north:c(t[1][1]),east:c(t[1][0]),west:c(t[0][0]),width:i,height:a};S(),function(t,e){var i=(f=h(t)).header;w=i.lo1,M=Math.max(i.la2,i.la1),v=i.dx,g=i.dy,y=i.nx,E=i.ny,(d=new Date(i.refTime)).setHours(d.getHours()+i.forecastTime),m=[];for(var a=0,n=360<=Math.floor(y*v),o=0;o<E;o++){for(var r=[],s=0;s<y;s++,a++)r[s]=f.data(a);n&&r.push(r[0]),m[o]=r}e({date:d,interpolate:I})}(s,function(t){!function(v,g,y,i){var E={},t=(y.south-y.north)*(y.west-y.east),I=p.VELOCITY_SCALE*Math.pow(t,.4),_=[],a=g.x;function n(t){for(var e,i,a,n,o,r,s,h,c,l,u,d=[],p=g.y;p<=g.yMax;p+=2){var f=x(t,p,y);if(f){var m=f[0],w=f[1];if(isFinite(m)){var M=v.interpolate(m,w);M&&(e=E,i=m,a=w,n=t,o=p,r=I,h=y,void 0,c=(s=M)[0]*r,l=s[1]*r,u=C(e,i,a,n,o,h),s[0]=u[0]*c+u[2]*l,s[1]=u[1]*c+u[3]*l,M=s,d[p+1]=d[p]=M)}}}_[t+1]=_[t]=d}!function t(){for(var e=Date.now();a<g.width;)if(n(a),a+=2,1e3<Date.now()-e)return void setTimeout(t,25);o(_,g,i)}()}(t,function(t,e,i){var a=t[0],n=t[1],o=Math.round(a[0]),r=Math.max(Math.floor(a[1],0),0);Math.min(Math.ceil(n[0],e),e-1);return{x:o,y:r,xMax:e,yMax:Math.min(Math.ceil(n[1],i),i-1),width:e,height:i}}(e,i,a),n,function(t,e){O.field=e,P(t,e)})})},S=function(){O.field&&O.field.release(),R&&cancelAnimationFrame(R)},O={params:p.params,start:b,stop:S,update:function(t,e,i,a,n){delete p.params.data,p.params.data=t,n&&b(e,i,a,n)},shift:function(t,e){var i=p.canvas,a=i.width,n=i.height,o=i.getContext("2d");if(t<a&&e<n){var r=function(t,e){return Math.max(0,Math.min(t,e))},s=o.getImageData(r(a,-t),r(n,-e),r(a,a-t),r(n,n-e));o.clearRect(0,0,a,n),o.putImageData(s,r(a,t),r(n,e));for(var h=0,c=particles.length;h<c;h++)particles[h].x+=t,particles[h].y+=e}},createField:o,interpolatePoint:I,setData:function(t){s=t},updateParams:function(t){p.params=t,e(p.params)},getParams:function(){return p.params},buildParams:e};return O}window.requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return window.setTimeout(t,1e3/window.FRAME_RATE)},window.cancelAnimationFrame||(window.cancelAnimationFrame=function(t){clearTimeout(t)});var n=function(t){return t/.514},o=function(t){return 3.6*t},a="undefined"==typeof window?{}:window;return a.BMap||(a.BMap={}),a.BMap.Overlay||(a.BMap.Overlay=function(){return function(){}}()),function(i){function t(t,e){void 0===e&&(e={}),i.call(this,e),this.options=e,this.paneName=this.options.paneName||"mapPane",this.context=this.options.context||"2d",this.zIndex=this.options.zIndex||0,this.mixBlendMode=this.options.mixBlendMode||null,this.enableMassClear=this.options.enableMassClear,this._map=e.map,this._lastDrawTime=null,this.canvas=null,this.data=t,this._windy=null,this.show()}return i&&(t.__proto__=i),((t.prototype=Object.create(i&&i.prototype)).constructor=t).prototype.getData=function(){return this.data},t.prototype.setData=function(t){this.data=t,this._map&&this.data&&this._draw()},t.prototype._getExtent=function(){var t=this._map.getSize(),e=this._map.getBounds().getNorthEast(),i=this._map.getBounds().getSouthWest();return[[[0,0],[t.width,t.height]],t.width,t.height,[[i.lng,i.lat],[e.lng,e.lat]]]},t.prototype.appendTo=function(t){if(!t)throw new Error("not map object");t.addOverlay(this)},t.prototype.initialize=function(t){var e=this;this._map=t;var i=this.canvas=document.createElement("canvas");return i.style.cssText="position:absolute; left:0; top:0; z-index: "+this.zIndex+" ;user-select:none;",i.style.mixBlendMode=this.mixBlendMode,this.adjustSize(),t.getPanes()[this.paneName].appendChild(i),t.addEventListener("resize",function(){e.adjustSize(),e._draw()}),this.canvas},t.prototype.adjustSize=function(){var t=this._map.getSize(),e=this.canvas,i=this.devicePixelRatio=a.devicePixelRatio||1;e.width=t.width*i,e.height=t.height*i,"2d"===this.context&&e.getContext(this.context).scale(i,i),e.style.width=t.width+"px",e.style.height=t.height+"px"},t.prototype.draw=function(){var t=this;clearTimeout(t.timeoutID),t.timeoutID=setTimeout(function(){t._draw()},15)},t.prototype._draw=function(){var t=this._map,e=t.getSize(),i=t.getCenter();if(i){var a=t.pointToOverlayPixel(i);this.canvas.style.left=a.x-e.width/2+"px",this.canvas.style.top=a.y-e.height/2+"px",this.dispatchEvent("draw"),this.options.update&&this.options.update.call(this),this.render(this.canvas)}},t.prototype.render=function(t){var e=this._getExtent();if(!this.getData()||!e)return this;if(t&&!this._windy){var i=this.options,a=i.minVelocity,n=i.maxVelocity,o=i.velocityScale,r=i.particleAge,s=i.lineWidth,h=i.particleMultiplier,c=i.colorScale;this._windy=new l({canvas:t,data:this.getData(),onDraw:function(){},minVelocity:a,maxVelocity:n,velocityScale:o,particleAge:r,lineWidth:s,particleMultiplier:h,colorScale:c}),this._windy.start(e[0],e[1],e[2],e[3])}else t&&this._windy&&this._windy.start(e[0],e[1],e[2],e[3]);return this},t.prototype.getContainer=function(){return this.canvas},t.prototype.setZIndex=function(t){this.zIndex=t,this.canvas.style.zIndex=this.zIndex},t.prototype.getZIndex=function(){return this.zIndex},t.prototype.getPointData=function(t){var e=this._windy.interpolatePoint(t[0],t[1]);if(e&&!isNaN(e[0])&&!isNaN(e[1])&&e[2])return{direction:function(t,e,i){i.endsWith("CCW")&&(e=0<e?e=-e:Math.abs(e));var a=Math.sqrt(Math.pow(t,2)+Math.pow(e,2)),n=180*Math.atan2(t/a,e/a)/Math.PI+180;return"bearingCW"!==i&&"meteoCCW"!==i||360<=(n+=180)&&(n-=360),n}(e[0],e[1],this.options.angleConvention||"bearingCCW"),speed:function(t,e,i){var a=Math.sqrt(Math.pow(t,2)+Math.pow(e,2));return"k/h"===i?o(a):"kt"===i?n(a):a}(e[0],e[1],this.options.speedUnit)}},t.prototype.clearWind=function(){this._windy&&this._windy.stop()},t.prototype.updateParams=function(t){if(this.options=Object.assign(this.options,t),this._windy){var e=this.options,i=e.minVelocity,a=e.maxVelocity,n=e.velocityScale,o=e.particleAge,r=e.lineWidth,s=e.particleMultiplier,h=e.colorScale;this._windy&&(this._windy.updateParams({minVelocity:i,maxVelocity:a,velocityScale:n,particleAge:o,lineWidth:r,particleMultiplier:s,colorScale:h}),this._map&&this.canvas&&this.data&&this.render(this.canvas))}return this},t.prototype.getParams=function(){return this._windy&&this._windy.getParams()},t}(a.BMap.Overlay)});