From the code you’ve provided, it looks like the problem stems from your if statement that checks for a blank image:
if (pixels[3] == 0) {
// blank image - retry
return test (dx, dy, w, h);
}
If the condition is met, test() will get called, so if a blank image is produced multiple times, you could end up with the function being called more than you anticipated.
To see if this is where the problem lies, insert a console.log statement so you can see how often this condition evaluates to true, thus calling test().
Something like this would work:
if (pixels[3] == 0) {
// blank image - retry
console.log('blank image... calling test()');
return test (dx, dy, w, h);
}
Then you could see how often test() is being called as a result of that if check.
solved Console log outputs one too many times on function