1 line
2.5 KiB
JavaScript
1 line
2.5 KiB
JavaScript
var Windy=function(d){var b=100;var p=20;var o=8;var c=d.canvas;var r=d.map;var j=d.data;var l=c.width;var k=c.height;var m=c.getContext("2d");m.lineWidth=0.8;m.fillStyle="rgba(0,0,0,.97)";var a=function(t,w){var s=t[0];var v=t[1];var u={x:s[0],y:s[1],xmax:v[0],ymax:v[1],width:v[0]-s[0],height:v[1]-s[1]};w(u)};var i=function(t,u,v){function s(w,A){var z=t[Math.floor(w)];return z&&z[Math.floor(A)]}s.release=function(){t=[]};s.randomize=function(z){var w=Math.floor(Math.floor(Math.random()*u.width)+u.x);var A=Math.floor(Math.floor(Math.random()*u.height)+u.y);z.x=w;z.y=A;return z};v(u,s)};var q=function(w,A,z){var v=[];var s=w.x;function y(B){var D=[];for(var E=w.y;E<w.ymax;E+=2){var C=u(B,E);D[E+1]=D[E]=C}v[B+1]=v[B]=D}function u(B,H){var G=0,F=0,E=0,D=0,C={};A.forEach(function(x){G+=x.angle*1/((H-x.y)*(H-x.y)+(B-x.x)*(B-x.x));F+=1/((H-x.y)*(H-x.y)+(B-x.x)*(B-x.x));E+=x.speed*1/((H-x.y)*(H-x.y)+(B-x.x)*(B-x.x));D+=1/((H-x.y)*(H-x.y)+(B-x.x)*(B-x.x));if(F!=0){C.angle=G/F}if(D!=0){C.speed=E/D}});return C}(function t(){var x=Date.now();while(s<w.xmax){y(s);s+=2;if((Date.now()-x)>1000){setTimeout(t,25);return}}i(v,w,z)})()};var f=function(x,t){var z=Math.round(x.width*o);var w=[];for(var u=0;u<z;u++){w.push(t.randomize({age:Math.floor(Math.random()*b)}))}function s(){w.forEach(function(C,A){if(C.age>b){C=t.randomize({age:0});w.splice(A,1,C)}var G=C.x;var E=C.y;var I=t(G,E);if(I){var H=G-I.speed*Math.sin(Math.PI/180*(180-I.angle));var B=E-I.speed*Math.cos(Math.PI/180*(180-I.angle));var F=t(H,B);if(F){C.xe=H;C.ye=B}else{var D=t.randomize({age:Math.floor(Math.random()*b)});w.splice(A,1,D)}}else{C.age=b}C.age+=1})}function v(){var A=m.globalCompositeOperation;m.globalCompositeOperation="destination-in";m.fillRect(0,0,l,k);m.globalCompositeOperation=A;m.beginPath();w.forEach(function(C,B){m.moveTo(C.x,C.y);m.lineTo(C.xe,C.ye);C.x=C.xe;C.y=C.ye});m.stroke()}(function y(){try{g.timer=setTimeout(function(){requestAnimationFrame(y);s();v()},1000/p)}catch(A){console.error(A)}})()};var e=function(s,t){n();a(s,function(u){q(u,t,function(w,v){g.vector=v;f(w,v)})})};var n=function(){m.clearRect(0,0,l,k);if(g.vector){g.vector.release()}if(g.timer){clearTimeout(g.timer)}};var h=function(s){m.lineWidth=s.size;m.strokeStyle=s.color};var g={options:d,start:e,stop:n,change:h};return g};window.requestAnimationFrame=(function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(a,1000/20)}})(); |