MCPcopy Index your code
hub / github.com/codrops/RainEffect / updateDrops

Function updateDrops

src/raindrops.js:251–370  ·  view source on GitHub ↗
(timeScale)

Source from the content-addressed store, hash-verified

249 this.ctx.drawImage(this.droplets,0,0,this.width,this.height);
250 },
251 updateDrops(timeScale){
252 let newDrops=[];
253
254 this.updateDroplets(timeScale);
255 let rainDrops=this.updateRain(timeScale);
256 newDrops=newDrops.concat(rainDrops);
257
258 this.drops.sort((a,b)=>{
259 let va=(a.y*(this.width/this.scale))+a.x;
260 let vb=(b.y*(this.width/this.scale))+b.x;
261 return va>vb?1:va==vb?0:-1;
262 });
263
264 this.drops.forEach(function(drop,i){
265 if(!drop.killed){
266 // update gravity
267 // (chance of drops "creeping down")
268 if(chance((drop.r-(this.options.minR*this.options.dropFallMultiplier)) * (0.1/this.deltaR) * timeScale)){
269 drop.momentum += random((drop.r/this.options.maxR)*4);
270 }
271 // clean small drops
272 if(this.options.autoShrink && drop.r<=this.options.minR && chance(0.05*timeScale)){
273 drop.shrink+=0.01;
274 }
275 //update shrinkage
276 drop.r -= drop.shrink*timeScale;
277 if(drop.r<=0) drop.killed=true;
278
279 // update trails
280 if(this.options.raining){
281 drop.lastSpawn+=drop.momentum*timeScale*this.options.trailRate;
282 if(drop.lastSpawn>drop.nextSpawn){
283 let trailDrop=this.createDrop({
284 x:drop.x+(random(-drop.r,drop.r)*0.1),
285 y:drop.y-(drop.r*0.01),
286 r:drop.r*random(...this.options.trailScaleRange),
287 spreadY:drop.momentum*0.1,
288 parent:drop,
289 });
290
291 if(trailDrop!=null){
292 newDrops.push(trailDrop);
293
294 drop.r*=Math.pow(0.97,timeScale);
295 drop.lastSpawn=0;
296 drop.nextSpawn=random(this.options.minR,this.options.maxR)-(drop.momentum*2*this.options.trailRate)+(this.options.maxR-drop.r);
297 }
298 }
299 }
300
301 //normalize spread
302 drop.spreadX*=Math.pow(0.4,timeScale);
303 drop.spreadY*=Math.pow(0.7,timeScale);
304
305 //update position
306 let moved=drop.momentum>0;
307 if(moved && !drop.killed){
308 drop.y+=drop.momentum*this.options.globalTimeScale;

Callers

nothing calls this directly

Calls 2

chanceFunction · 0.90
randomFunction · 0.90

Tested by

no test coverage detected