Fixed bug 1828 and added unit test

Rebased this one now and closed the previous pull request at: https://github.com/processing-js/processing-js/pull/38