Refactor canvas code
Fix clipping calculation