Now working for non-rectangular canvas.