For any non-trivial editor you will need an object model that abstracts away the low level drawing functions and manages state.
I think fabric.js is worth checking as its source code is well organized and documented. It's not a small project though and the latest version was rewritten in TypeScript: https://github.com/fabricjs/fabric.js/tree/master