Skip to content

Instantly share code, notes, and snippets.

@gjohnson
Created March 9, 2013 22:39
Show Gist options
  • Save gjohnson/5126090 to your computer and use it in GitHub Desktop.
Save gjohnson/5126090 to your computer and use it in GitHub Desktop.
multiply video via canvas
<!DOCTYPE html>
<html>
<head>
<title>video - wall</title>
<style>
body {
padding: 0;
margin: 0;
}
#video {
display: none;
}
canvas {
padding: 0;
margin: 0 0 -5px 0;
}
</style>
</head>
<body>
<video id="video" width="1280px" height="720px" />
<script>
// flags
var done = false;
var video = document.getElementById('video');
// create canvas contexts
var ctxs = times(4, function(){
var el = document.createElement('canvas');
el.width = video.width;
el.height = video.height;
document.documentElement.appendChild(el);
return el.getContext('2d');
});
// buffer
video.addEventListener('canplaythrough', onstart, true);
// stop
video.addEventListener('ended', onend, true);
// load
video.preload = 'auto';
video.src = '/earth.mp4';
// draw when buffered
function onstart(){
video.play();
draw(video);
}
// animation frames
function draw(what){
if (done) return;
var pending = ctxs.length;
requestAnimationFrame(function(){
ctxs.forEach(function(ctx, idx){
ctx.drawImage(what, 0, 0);
--pending || draw(what);
});
});
}
// sets the "done" flag
function onend(){
done = true;
}
// calls `fn` `x` times
function times(x, fn){
var ret = [];
while (--x >= 0) ret.push(fn());
return ret;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment