added buffer and canvas state save and restore