refactor canvas generation code