Finding remainder of stepping by multiple beyond a limit
Math.ceil(scrollOffset/n)*n - scrollOffset


gives you the difference between scrollOffset and the next multiple of n larger or equal to scrollOffset. This is equivalent to:

var firstTickX = (scrollOffset % n) ? n - (scrollOffset % n) : 0;