(Taken from https://github.com/open-craft/jsinput-vectordraw/pull/24/commits/33c1d0cf53806b5fa67b290293af2f8e5b53a1e9)