olizilla gets up close and functional with javascript. withCanvas provides a temporary canvas to another function, and always remembers to clean up after itself.
// Provide a function with a temporary canvas, encapsulating the creation and clean up code. | |
function withCanvas(/*canvas => Any*/ use) { | |
var b = document.body; | |
// create canvas | |
var canvas = document.createElement("canvas"); | |
// canvas doesn't draw well unless it's added to the page | |
b.appendChild(canvas); | |
// do the user code | |
var result = use(canvas); | |
// clean up | |
b.removeChild(canvas); | |
// return anything there is to return | |
return result; | |
} | |
var video = document.getElementById('someVideo'); | |
// usage, do something with a temporary canvas | |
var src = withCanvas( function(canvas){ // anonymous function, canvas => String | |
var ctx = canvas.getContext('2d'); | |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height); | |
return ctx.canvas.toDataURL('image/png'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment