Render Distance for Sprites

I was wondering if there is something for rendering sprites that’s only close to a player or rendering sprites that are in the canvas frame.
I know I can use the dist(), but I’m using a loop to go through an array. The loop renders the sprite this way, therefore I can’t really go out of scope.

What I mean by looping through an array:

function npcBehavior() {
    for (var n = npcs.length-1; n > -1; n--) {
        var nn = npcs[n];
        drawSprite(nn);
    }
}

Hello @pluto,

I have implemented some functionality of this on a similar topic Help debug student program this takes into account of the sprites being centered and being rendered properly

By using the in built camera system you can check for specific sprites should be rendered on screen before having to draw them

The check i had made for this project is subpar and likely will be needed to be modified for your use case