Skip to content

Instantly share code, notes, and snippets.

@fi-tomek-augustyn
Created October 29, 2012 09:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fi-tomek-augustyn/3972674 to your computer and use it in GitHub Desktop.
Save fi-tomek-augustyn/3972674 to your computer and use it in GitHub Desktop.
Transparent canvas fader
/**
* Fade transparent canvas using 'destination-out' mode.
* To avoid visible trails call
* fadeCanvas(canvas, 0.4, 'source-over');
*
* @param {element} canvas Reference to a Canvas DOM element
* @param {number} alpha Alpha percentage of how much to fade
* @param {string} mode Fading mode
*/
function fadeCanvas(canvas, alpha, mode) {
mode = mode || 'destination-out';
ctx = canvas.getContext('2d');
ctx.globalCompositeOperation = mode;
ctx.fillStyle = "rgba(255, 255, 255, " + alpha + ")";
ctx.beginPath();
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fill();
ctx.globalCompositeOperation = 'source-over';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment