I've been trying to use a canvas to edit video by drawing to a offscreen canvas and then using getImageData to do some work and then putting that onto my onscreen canvas. It works, but even with a small 480x360 video, Chrome memory usage keeps increasing until it crashes (takes about ten minutes on my machine, less with larger video). The issue seems to be better in Firefox, but still suffers from large memory usage.
I realize that each getImageData call takes ~3MB of memory, but even then I feel like there should be a way to get Chrome to use less than 1GB of memory. I've lowered the framerate, which helps but does not solve the issue. Is there anything I can do to ensure that the imageData memory is freed in a more timely fashion?
<!DOCTYPE html>
<meta charset='utf-8'>
<html>
<head>
<title>Video Test</title>
</head>
<body>
<canvas id='display'>
</canvas>
<script type='text/javascript'>
var canvas = document.getElementById('display');
var context = canvas.getContext('2d');
var bCanvas = document.createElement('canvas');
var bContext = bCanvas.getContext('2d');
var video = document.createElement('video');
video.src = 'VIDEO HERE';
video.autoplay = true;
video.loop = true;
var last;
var interval = 35;
video.addEventListener('play', function() {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
bCanvas.width = video.videoWidth;
bCanvas.height = video.videoHeight;
last = performance.now();
window.requestAnimationFrame(draw);
}, false);
function draw(time) {
if(time - last > interval) {
bContext.drawImage(video,0,0,bCanvas.width,bCanvas.height);
var imageData = bContext.getImageData(0,0,bCanvas.width,bCanvas.height);
context.putImageData(imageData,0,0);
last = time;
}
window.requestAnimationFrame(draw);
}
</script>
</body>
</html>