How to convert Canvas animation in HD Avi video file ( 50 )



The purpose of this post is to show how to capture a canvas animation and transform it into avi video. PNG Capture in HD will be 1280 x 720 pixel.

Here is the avi result:

Step 1 - Canvas Capture

When the canvas animation is ready the First step is to locate the canvas DOM element and to create a copy of that canvas content into another canvas element. This works easyly with javascript.

// Create new canvas element
var canvas_draw = $('#canvas-draw-fancy');
canvas_draw.width = 320;
canvas_draw.height = 240;
var canvas_draw = $('canvas')[2].getContext('2d');

// Import the image from the old canvas
canvas_draw.drawImage(renderer.domElement, 0, 0, 320, 240);

// Export the image from the canvas
var img = new Image();
img.src = $("canvas")[0].toDataURL('image/png');
img.width = 320;

// Print screens on same page if necesary for preview

Reference page:

Step 2 - Send image code by Ajax

This image code has to be sended to a php file with ajax.

// set image data to php
var ajax = new XMLHttpRequest();"POST",'makefile.php',false);
ajax.setRequestHeader('Content-Type', 'application/upload');

Rererence page:

Step 3 - Write code in PNG files

Then the php file is writing this data into PNG files. To save all images generated under one second I had to change the time() into microtime() because I generate one image every 40 milisecond in javascript with setInterval() to keep the frame rate at 25-30 frames per second.

// Get the data

// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);

// Need to decode before saving since the data we received is already base64 encoded

// Save file. This example uses a hard coded filename for testing,
$fp = fopen("images/img_".microtime(1).".png", 'w');
fwrite( $fp, $unencodedData);
fclose( $fp );

Reference pages:

Step 4 - Loading all images in Virtualdub and export as avi

For this step is necesary to open all images as sequence in VirtualDub.

Reference page: