JSFiddle - React, Tailwind, and code Playground

JavaScript

//the spiral function accepts an array [width > 0, height > 0], and a //callback function draw(x, y)

function spiral(dim, draw) {
    var i = s = y = 0, x = -1;
    while (dim[s%2]) {
        i = 0;
        while (i++ < dim[s%2]) draw(x+=(1-s)%2, y+=(2-s)%2);
        s = (s+1)%4;
        dim[s%2] -= 1; } }

//example
//the draw function is not interesting … just log to the console
spiral([9,4], function(x, y) { document.write(x + ', ' + y + '<br />') });