Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  59] [ 4]  / answers: 1 / hits: 16199  / 14 Years ago, mon, january 24, 2011, 12:00:00

I need to take HTML5 canvas output as video or swf png sequence.



I found the following link on stackoverflow for capturing images.

Capture HTML Canvas as gif/jpg/png/pdf?



But can anyone suggest if we want the output to be video or swf of png sequence?



EDIT:



Ok now I understood how to capture the canvas data to store on server, I tried it and it is working fine if I use only shapes, rectangle or some other graphic, but not if I draw external images on canvas element.
Can anyone tell me how to capture canvas data completely whether we use graphic or external images for drawing on canvas?



I used the following code:



var cnv = document.getElementById(myCanvas);
var ctx = cnv.getContext(2d);

if(ctx)
{
var img = new Image();

ctx.fillStyle = rgba(255,0,0,0.5);
ctx.fillRect(0,0,300,300);
ctx.fill();

img.onload = function()
{
ctx.drawImage(img, 0,0);
}

img.src = my external image path;

var data = cnv.toDataURL(image/png);
}


after taking the canvas data into my data variable I created a new canvas and draw the captured data on that, the red rectangle drawn on the second canvas but that external image doesn't.



Thanks in advance.


More From » jquery

 Answers
62

I would suggest:




  1. Use setInterval to capture the contents of your Canvas as a PNG data URL.



    function PNGSequence( canvas ){
    this.canvas = canvas;
    this.sequence = [];
    };
    PNGSequence.prototype.capture = function( fps ){
    var cap = this;
    this.sequence.length=0;
    this.timer = setInterval(function(){
    cap.sequence.push( cap.canvas.toDataURL() );
    },1000/fps);
    };
    PNGSequence.prototype.stop = function(){
    if (this.timer) clearInterval(this.timer);
    delete this.timer;
    return this.sequence;
    };

    var myCanvas = document.getElementById('my-canvas-id');
    var recorder = new PNGSequence( myCanvas );
    recorder.capture(15);

    // Record 5 seconds
    setTimeout(function(){
    var thePNGDataURLs = recorder.stop();
    }, 5000 );

  2. Send all these PNG DataURLs to your server. It'll be a very large pile of data.


  3. Using whatever server-side language you like (PHP, Ruby, Python) strip the headers from the data URLs so that you are left with just the base64 encoded PNGs


  4. Using whatever server-side language you like, convert the base64 data to binary and write out temporary files.


  5. Using whatever 3rd party library you like on the server, convert the sequence of PNG files to a video.




Edit: Regarding your comment of external images, you cannot create a data URL from a canvas that is not origin-clean. As soon as you use drawImage() with an external image, your canvas is tainted. From that link:




All canvas elements must start with their origin-clean set to true.
The flag must be set to false if any of the following actions occur:



[...]



The element's 2D context's drawImage() method is called with an HTMLImageElement or an HTMLVideoElement whose origin is not the same as that of the Document object that owns the canvas element.



[...]



Whenever the toDataURL() method of a canvas element whose origin-clean flag is set to false is called, the method must raise a SECURITY_ERR exception.



Whenever the getImageData() method of the 2D context of a canvas element whose origin-clean flag is set to false is called with otherwise correct arguments, the method must raise a SECURITY_ERR exception.



[#94082] Friday, January 21, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
acaciac

Total Points: 317
Total Questions: 117
Total Answers: 128

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
;