This is what I was doing for a similar process with canvas. Maybe there is something here that will help: