Loops, break and continued.
The problem was caused when you checked the empty arrays of particles and found that the particle was removed.
Mistakes
The following two statements and blocks caused the problem.
if (particles.length == 0) { explosions.splice(i, 1); return; }
and
if (particles.size < 0) { explosions.splice(ii, 1); return; }
Returns stopped rendering particles, so you sometimes returned before drawing a single particle that was visualized only because the first explosion was empty or the first particle was too small.
Continue and break
You can use the continuation marker in javascript to skip the rest of a, while, do loop
for(i = 0; i < 100; i++){ if(test(i)){ // need to skip this iteration continue; } // more code // more code // continue skips all the code upto the closing } } << continues to here and if i < 100 the loop continues on.
Or you can completely break out of a loop with break
for(i = 0; i < 100; i++){ if(test(i)){ // need to exit the for loop break; } // more code // more code // break skips all the code to the first line after the closing } } << breaks to here and if i remains the value it was when break was encountered
Correction
if (particles.length == 0) { explosions.splice(i, 1); continue; }
and
if (particles.size < 0) { explosions.splice(ii, 1); continue; }
Your fix example
Your corrected code. Befor I found it, I started changing things.
Minor things. requestAnimationFrame transfers time in milliseconds accurate to microseconds.
You incorrectly set and lost frames. I changed the time to use the time of the argument, and then just set to the time when the frame is drawn.
There are other problems, nothing important and no more coding style style. You must capitalize on objects created with the new
function Particle(...
not
function Particle(...
and your random is too complicated
function randInt(min, max = min - (min = 0)) { return Math.floor(Math.random() * (max - min) + min); } or function randInt(min,max){ max = max === undefined ? min - (min = 0) : max; return Math.floor(Math.random() * (max - min) + min); } randInt(100);
And if you use the same name in variables, then a good sign for creating an object
var particlesPerExplosion = 20; var particlesMinSpeed = 3; var particlesMaxSpeed = 6; var particlesMinSize = 1; var particlesMaxSize = 3;
May be
const settings = { particles : { speed : {min : 3, max : 6 }, size : {min : 1 : max : 3 }, explosionCount : 20, }, background : "#000", }
Anyway, your code.
var c = canvas; var ctx = c.getContext('2d');
<!DOCTYPE html> <html> <head> <style> * { margin: 0; padding: 0; overflow: hidden; } </style> </head> <body> <canvas id="canvas"></canvas> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </html>