Created
March 21, 2013 19:47
-
-
Save positlabs/5216101 to your computer and use it in GitHub Desktop.
A simple canvas vignette
This file contains hidden or 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
Vignette = function (canvas) { | |
var alpha = .7, | |
context, | |
visible, | |
data; | |
(function initView() { | |
context = canvas.getContext("2d"); | |
show(); | |
})(); | |
function draw() { | |
var w = canvas.width = window.innerWidth; | |
var h = canvas.height = window.innerHeight; | |
context.clearRect(0, 0, w, h); | |
context.rect(0, 0, w, h); | |
// create radial gradient | |
var outerRadius = w * .5; | |
var innerRadius = w * .2; | |
var grd = context.createRadialGradient(w / 2, h / 2, innerRadius, w / 2, h / 2, outerRadius); | |
// light blue | |
grd.addColorStop(0, 'rgba(0,0,0,0)'); | |
// dark blue | |
grd.addColorStop(1, 'rgba(0,0,0,' + alpha + ')'); | |
context.fillStyle = grd; | |
context.fill(); | |
} | |
this.show = function() { | |
if (!visible) { | |
visible = true; | |
canvas.style.display = "block"; | |
window.addEventListener("resize", draw); | |
draw(); | |
} | |
} | |
this.hide = function() { | |
if (!visible) { | |
visible = false; | |
canvas.style.display = "none"; | |
window.removeEventListener("resize", draw); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment