概述:本文基于《Openlayer4中风场的实现》一文,使用leaflet实现流动风场的效果。
废话不多说,下面贴上代码:
1、wind算法及扩展:
1 /** 2 * @author 3 * @Date 4 * 1.计算矩形4个角的canvas坐标x、y,初始化该区域所有网格点,间距可根据map index设置 5 * 2.将已有的站点经纬度转换为canvas坐标 6 * 3.插值法计算出每个网格点的风向、风速 7 * 4.在该网格区域随机生成width*8个点,重复运动 8 */ 9 10 var Windy = function (options) { 11 var MAX_PARTICLE_AGE = 100; //粒子最大运动次数 12 var FRAME_RATE = 20; //重绘帧数 13 var PARTICLE_MULTIPLIER = 8; 14 15 var canvas = options.canvas; 16 var width = canvas.width; 17 var height = canvas.height; 18 var ctx = canvas.getContext('2d'); 19 ctx.lineWidth = .8; 20 ctx.fillStyle = 'rgba(0,0,0,.97)'; 21 // ctx.strokeStyle = 'rgba(38,173,133,0.8)'; 22 23 var buildBounds = function (extent, callback) { 24 var upperLeft = extent[0]; 25 var lowerRight = extent[1]; 26 var bounds = { 27 x: upperLeft[0], 28 y: upperLeft[1], 29 xmax: lowerRight[0], 30 ymax: lowerRight[1], 31 width: lowerRight[0] - upperLeft[0], 32 height: lowerRight[1] - upperLeft[1] 33 }; 34 callback(bounds); 35 } 36 37 var createField = function (columns, bounds, callback) { 38 function vector(x, y) { 39 var column = columns[Math.floor(x)]; 40 return column && column[Math.floor(y)]; 41 } 42 43 vector.release = function () { 44 columns = []; 45 } 46 47 vector.randomize = function (o) { 48 var x = Math.floor(Math.floor(Math.random() * bounds.width) + bounds.x); 49 var y = Math.floor(Math.floor(Math.random() * bounds.height) + bounds.y); 50 o.x = x; 51 o.y = y; 52 return o; 53 } 54 callback(bounds, vector); 55 }; 56 57 var interpolateGrid = function (bounds, stationPoints, callback) { 58 var columns = []; 59 var x = bounds.x; 60 61 function interpolateColumn(x) { 62 var column = []; 63 for (var y = bounds.y; y < bounds.ymax; y += 2