wind-layer
Version:
a openlayers ol bmap amap maptalks extension to windjs
2 lines (1 loc) • 11.5 kB
JavaScript
!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(l){void 0===l&&(l={}),this.params=l;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=l.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,r){var o=1-t,s=1-e,h=o*s,c=t*s,u=o*e,l=t*e,d=i[0]*h+a[0]*c+n[0]*u+r[0]*l,p=i[1]*h+a[1]*c+n[1]*u+r[1]*l;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 r(a,n,t){function r(t,e){var i=a[Math.round(t)];return i&&i[Math.round(e)]||o}r.release=function(){a=[]},r.randomize=function(t){for(var e,i,a=0;null===r(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,r)}function c(t){return t/180*Math.PI}function u(t){return t/(Math.PI/180)}e(l),window.FRAME_TIME=1e3/p.FRAME_RATE;var f,g,d,M,v,m,y,E,w,A,o=[NaN,NaN,null],s=p.params.data,_=function(t,e){if(!g)return null;var i,a=C(t-M,360)/m,n=(v-e)/y,r=Math.floor(a),o=r+1,s=Math.floor(n),h=s+1;if(i=g[s]){var c=i[r],u=i[o];if(I(c)&&I(u)&&(i=g[h])){var l=i[r],d=i[o];if(I(l)&&I(d))return f.interpolate(a-r,n-s,c,u,l,d)}}return null},I=function(t){return null!=t},C=function(t,e){return t-e*Math.floor(t/e)},x=function(t,e,i,a,n,r){var o=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,u=R(i,e+h,r),l=R(i+c,e,r),d=Math.cos(i/360*o);return[(u[0]-a)/h/d,(u[1]-n)/h/d,(l[0]-a)/c,(l[1]-n)/c]};function P(t){return Math.log(Math.tan(t/2+Math.PI/4))}function L(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=[],r=0;r<t;r++)n.push(s.randomize({age:Math.floor(Math.random()*p.MAX_PARTICLE_AGE)+0}));var o=p.canvas.getContext("2d");o.lineWidth=p.PARTICLE_LINE_WIDTH,o.fillStyle="rgba(0, 0, 0, 0.97)",o.globalAlpha=.6;var u=Date.now();!function t(){T=requestAnimationFrame(t);var e=Date.now(),i=e-u;i>FRAME_TIME&&(u=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 r=e+a[0],o=i+a[1];null!==s(r,o)[2]?(t.xt=r,t.yt=o,c[h.indexFor(n)].push(t)):(t.x=r,t.y=o)}t.age+=1}),o.globalCompositeOperation="destination-in",o.fillRect(a.x,a.y,p.canvas.width,p.canvas.height),o.globalCompositeOperation="lighter",o.globalAlpha=.9,c.forEach(function(t,e){0<t.length&&(o.beginPath(),o.strokeStyle=h[e],t.forEach(function(t){o.moveTo(t.x*p.DEVICEPIXELRATIO,t.y*p.DEVICEPIXELRATIO),o.lineTo(t.xt*p.DEVICEPIXELRATIO,t.yt*p.DEVICEPIXELRATIO),t.x=t.xt,t.y=t.yt}),o.stroke())}),l.onDraw&&l.onDraw())}()}A="EPSG:4326"===p.params.projection?function(t,e,i){var a=i.east-i.west,n=i.south-i.north,r=u(i.north)+e/i.height*u(n);return[u(i.west)+t/i.width*u(a),r]}:function(t,e,i){var a=i.east-i.west,n=i.width/u(a)*360/(2*Math.PI),r=n/2*Math.log((1+Math.sin(i.south))/(1-Math.sin(i.south))),o=(i.height+r-e)/n,s=180/Math.PI*(2*Math.atan(Math.exp(o))-Math.PI/2);return[u(i.west)+t/i.width*u(a),s]};var T,R=function(t,e,i){var a=P(i.south),n=P(i.north),r=i.width/(i.east-i.west),o=i.height/(n-a),s=P(c(t));return[(c(e)-i.west)*r,s=(n-s)*o]},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;M=i.lo1,v=Math.max(i.la2,i.la1),m=i.dx,y=i.dy,E=i.nx,w=i.ny,(d=new Date(i.refTime)).setHours(d.getHours()+i.forecastTime),g=[];for(var a=0,n=360<=Math.floor(E*m),r=0;r<w;r++){for(var o=[],s=0;s<E;s++,a++)o[s]=f.data(a);n&&o.push(o[0]),g[r]=o}e({date:d,interpolate:_})}(s,function(t){!function(m,y,E,i){var w={},t=(E.south-E.north)*(E.west-E.east),_=p.VELOCITY_SCALE*Math.pow(t,.4),I=[],a=y.x;function n(t){for(var e,i,a,n,r,o,s,h,c,u,l,d=[],p=y.y;p<=y.yMax;p+=2){var f=A(t,p,E);if(f){var g=f[0],M=f[1];if(isFinite(g)){var v=m.interpolate(g,M);v&&(e=w,i=g,a=M,n=t,r=p,o=_,h=E,void 0,c=(s=v)[0]*o,u=s[1]*o,l=x(e,i,a,n,r,h),s[0]=l[0]*c+l[2]*u,s[1]=l[1]*c+l[3]*u,v=s,d[p+1]=d[p]=v)}}}I[t+1]=I[t]=d}!function t(){for(var e=Date.now();a<y.width;)if(n(a),a+=2,1e3<Date.now()-e)return void setTimeout(t,25);r(I,y,i)}()}(t,function(t,e,i){var a=t[0],n=t[1],r=Math.round(a[0]),o=Math.max(Math.floor(a[1],0),0);Math.min(Math.ceil(n[0],e),e-1);return{x:r,y:o,xMax:e,yMax:Math.min(Math.ceil(n[1],i),i-1),width:e,height:i}}(e,i,a),n,function(t,e){W.field=e,L(t,e)})})},S=function(){W.field&&W.field.release(),T&&cancelAnimationFrame(T)},W={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,r=i.getContext("2d");if(t<a&&e<n){var o=function(t,e){return Math.max(0,Math.min(t,e))},s=r.getImageData(o(a,-t),o(n,-e),o(a,a-t),o(n,n-e));r.clearRect(0,0,a,n),r.putImageData(s,o(a,t),o(n,e));for(var h=0,c=particles.length;h<c;h++)particles[h].x+=t,particles[h].y+=e}},createField:r,interpolatePoint:_,setData:function(t){s=t},updateParams:function(t){p.params=t,e(p.params)},getParams:function(){return p.params},buildParams:e};return W}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},r=function(t){return 3.6*t},o=("undefined"==typeof window?{}:window).ol||{};return o.layer||(o.layer={}),o.layer.Image||(o.layer.Image=function(){return function(){}}()),function(i){function t(t,e){void 0===e&&(e={}),i.call(this,e),this._canvas=null,this.data=t,this.$Windy=null,this.isClear=!1,this.options=e,this.setSource(new o.source.ImageCanvas({logo:e.logo,state:e.state,attributions:e.attributions,resolutions:e.resolutions,canvasFunction:this.canvasFunction.bind(this),ratio:e.hasOwnProperty("ratio")?e.ratio:1})),this.on("precompose",this.redraw,this)}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){var e=this.getMap();if(!e)return this;if(this.data=t,this.isClear=!1,!this.$Windy&&this._canvas)this.render(this._canvas),e.renderSync();else if(this.$Windy&&this._canvas){this._cloneLayer&&(e.addLayer(this._cloneLayer),delete this._cloneLayer);var i=this._getExtent();this.$Windy.update(this.getData(),i[0],i[1],i[2],i[3])}else console.warn("please create new instance");return this},t.prototype.render=function(t){var e=this._getExtent();if(this.isClear||!this.getData()||!e)return this;if(t&&!this.$Windy){var i=this.options,a=i.minVelocity,n=i.maxVelocity,r=i.velocityScale,o=i.particleAge,s=i.lineWidth,h=i.particleMultiplier,c=i.colorScale;this.$Windy=new l({canvas:t,projection:this._getProjectionCode(),data:this.getData(),devicePixelRatio:this.options.devicePixelRatio,minVelocity:a,maxVelocity:n,velocityScale:r,particleAge:o,lineWidth:s,particleMultiplier:h,colorScale:c}),this.$Windy.start(e[0],e[1],e[2],e[3])}else if(t&&this.$Windy){var u=this._getExtent();this.$Windy.start(u[0],u[1],u[2],u[3])}return this},t.prototype.redraw=function(){if(!this.isClear){var t=this.options.extent||this._getMapExtent();this.setExtent(t)}},t.prototype.canvasFunction=function(t,e,i,a,n){return this._canvas?(this._canvas.width=a[0],this._canvas.height=a[1]):this._canvas=function(t,e,i){if("undefined"==typeof document)return new i(t,e);var a=document.createElement("canvas");return a.width=t,a.height=e,a}(a[0],a[1]),e<=this.get("maxResolution")&&this.render(this._canvas),this._canvas},t.prototype._getExtent=function(){var t=this._getMapSize(),e=this._getMapExtent();if(t&&e){var i=this._getProjectionCode(),a=o.proj.transformExtent(e,i,"EPSG:4326");return[[[0,0],[t[0],t[1]]],t[0],t[1],[[a[0],a[1]],[a[2],a[3]]]]}return!1},t.prototype._getMapExtent=function(){if(this.getMap()){var t=this._getMapSize(),e=this.getMap().getView();return e&&e.calculateExtent(t)}},t.prototype._getMapSize=function(){if(this.getMap())return this.getMap().getSize()},t.prototype.appendTo=function(t){if(!(t&&t instanceof o.Map))throw new Error("not map object");this.set("originMap",t),this.getSource().projection_=this._getProjectionCode(),t.addLayer(this)},t.prototype.getPointData=function(t){if(!this.$Windy)return null;var e=this.$Windy.interpolatePoint(t[0],t[1]);return e&&!isNaN(e[0])&&!isNaN(e[1])&&e[2]?{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?r(a):"kt"===i?n(a):a}(e[0],e[1],this.options.speedUnit)}:void 0},t.prototype.clearWind=function(){var t=this.getMap();t&&(this.$Windy&&this.$Windy.stop(),this.isClear=!0,this._cloneLayer=this,t.removeLayer(this),this.changed(),this.getMap().renderSync())},t.prototype.removeLayer=function(){var t=this.getMap();t&&(this.$Windy&&this.$Windy.stop(),this.un("precompose",this.redraw,this),t.removeLayer(this),delete this._canvas,delete this.$Windy,delete this._cloneLayer)},t.prototype.setMap=function(t){this.set("originMap",t)},t.prototype.getMap=function(){return this.get("originMap")},t.prototype._getProjectionCode=function(){var t=this.getMap();return t?t.getView()&&t.getView().getProjection().getCode():"EPSG:3857"},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,r=e.particleAge,o=e.lineWidth,s=e.particleMultiplier,h=e.colorScale;this.$Windy&&(this.$Windy.updateParams({minVelocity:i,maxVelocity:a,velocityScale:n,particleAge:r,lineWidth:o,particleMultiplier:s,colorScale:h,devicePixelRatio:this.options.devicePixelRatio}),this.getMap()&&this._canvas&&this.data&&this.render(this._canvas))}return this},t.prototype.getParams=function(){return this.$Windy&&this.$Windy.getParams()},t}(o.layer.Image)});