I created a class wrapped in an ES6 module that solves exactly this.
It's 103 lines, no dependencies, and fairly nicely structured and documented, meant to be easy to modify/reuse.
Handles all 8 possible orientations, and is Promise-based.
Here you go, hope this still helps someone: https://gist.github.com/vdavid/3f9b66b60f52204317a4cc0e77097913