I don't know about square drawing, but the problem here seems to be in variable passing to function.
Those variables you have defined at the beginning does not get changed when passing furher into functions
You can check how it works here:
function swap(a, b) {
var tmp = a;
a = b;
b = tmp; //assign tmp to b
}
var x = 1, y = 2;
swap(x, y);
alert("x is " + x + " y is " + y); // "x is 1 y is 2"
And your example on FIDDLE
it does draw a line and topMinX is incremented