This repo is companion source code for a blogpost, which in itself is a followup to a blogpost by Daniel Kennett.
This demo project contains a simpler approach to the problem Daniel described, although it still is no walk in the park.
This code is released under an "unlicense" LICENSE.