Ported code to canvas.