Skip to content

Instantly share code, notes, and snippets.

@skilldrick
Created November 25, 2010 00:00
Show Gist options
  • Save skilldrick/714667 to your computer and use it in GitHub Desktop.
Save skilldrick/714667 to your computer and use it in GitHub Desktop.
No more pesky context.save() and context.restore() - use this handy sandwich function instead!
function saveRestore(ctx, func) {
ctx.save();
func(ctx);
ctx.restore();
}
//Usage
saveRestore(context, function (ctx) {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, width, height);
});
//Alternatively define saveRestore with access to context:
function saveRestore(func) {
MYAPP.ctx.save();
func(MYAPP.ctx);
MYAPP.ctx.restore();
}
//Usage
saveRestore(function (ctx) {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, width, height);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment