Insert resize function and hook it up to window resize event. Add draw code to clear canvas on each iteration.