isolate the keepInViewport method
add tests for keepInViewport