Port background-image from JS

This is close, but I'm lost porting the math for the background image pattern matrix from JS.
@ansis or @kkaefer, can you help?