Created
October 29, 2012 09:51
-
-
Save fi-tomek-augustyn/3972674 to your computer and use it in GitHub Desktop.
Transparent canvas fader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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