Moving global function to an Angular service
Adding missing var statements
Moving canvas operation out of loop

No reason to repeatedly re-set the same value again and again inside
the loop.
Adding missing var statements
Re-using pre-allocated array

Re-using the same array reduces the amount of garbage collection
and appears to speed up building the regions, especially in Chrome.