Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save canvas animation as gif or webm?

Tags:

i have written this function to capture each frame for the GIF but the output is very laggy and crashes when the data increases. Any suggestions ?

Code :

    function createGifFromPng(list, framerate, fileName, gifScale) {             gifshot.createGIF({                 'images': list,                 'gifWidth': wWidth * gifScale,                 'gifHeight': wHeight * gifScale,                 'interval': 1 / framerate,             }, function(obj) {                 if (!obj.error) {                     var image = obj.image;                     var a = document.createElement('a');                     document.body.append(a);                     a.download = fileName;                     a.href = image;                     a.click();                     a.remove();                 }             });         } /////////////////////////////////////////////////////////////////////////  function getGifFromCanvas(renderer, sprite, fileName, gifScale, framesCount, framerate) {             var listImgs = [];             var saving = false;             var interval = setInterval(function() {                 renderer.extract.canvas(sprite).toBlob(function(b) {                     if (listImgs.length >= framesCount) {                         clearInterval(interval);                         if (!saving) {                         createGifFromPng(listImgs, framerate, fileName,gifScale);                             saving = true;                         }                     }                     else {                         listImgs.push(URL.createObjectURL(b));                     }                 }, 'image/gif');             }, 1000 / framerate);         } 
like image 570
Hashir Salam Avatar asked Jun 04 '18 13:06

Hashir Salam


People also ask

How can I save my animation as a GIF?

Choose File > Export > Export image or File > Export > Export Animated GIF. Click a tab at the top of the Export image or Export Animated GIF dialog box to select a display option: Optimized or 2‑Up.

How do I save a canvas video?

Locate the video in your Canvas course. Right-click anywhere on the video and select “Save Video As” from the menu that opens. Choose a location to save on your computer and click Save. NOTES: You can rename the video file as the one that is generated is not meaningful.

Does canvas support gifs?

Embedding Images Displaying images is a good first step as you learn to embed media in Canvas. The image insertion tools allow you to add still images and animated gifs.

Can canvas be used to make animations?

You can create animations with HTML5 by combining HTML, CSS, and JavaScript (JS), with which you can build shapes. Also, you can control animations and edit images, video, and audio by means of JS or CSS elements, all of which you then add to a drawing board, which you set up with the <canvas> element.


2 Answers

You can also use https://github.com/spite/ccapture.js/ to capture to gif or video.

like image 26
SMUsamaShah Avatar answered Oct 06 '22 07:10

SMUsamaShah


In modern browsers you can use a conjunction of the MediaRecorder API and the HTMLCanvasElement.captureStream method.

The MediaRecorder API will be able to encode a MediaStream in a video or audio media file on the fly, resulting in far less memory needed than when you grab still images.

const ctx = canvas.getContext('2d');  var x = 0;  anim();  startRecording();    function startRecording() {    const chunks = []; // here we will store our recorded media chunks (Blobs)    const stream = canvas.captureStream(); // grab our canvas MediaStream    const rec = new MediaRecorder(stream); // init the recorder    // every time the recorder has new data, we will store it in our array    rec.ondataavailable = e => chunks.push(e.data);    // only when the recorder stops, we construct a complete Blob from all the chunks    rec.onstop = e => exportVid(new Blob(chunks, {type: 'video/webm'}));        rec.start();    setTimeout(()=>rec.stop(), 3000); // stop recording in 3s  }    function exportVid(blob) {    const vid = document.createElement('video');    vid.src = URL.createObjectURL(blob);    vid.controls = true;    document.body.appendChild(vid);    const a = document.createElement('a');    a.download = 'myvid.webm';    a.href = vid.src;    a.textContent = 'download the video';    document.body.appendChild(a);  }    function anim(){    x = (x + 1) % canvas.width;    ctx.fillStyle = 'white';    ctx.fillRect(0,0,canvas.width,canvas.height);    ctx.fillStyle = 'black';    ctx.fillRect(x - 20, 0, 40, 40);    requestAnimationFrame(anim);  }
<canvas id="canvas"></canvas>
like image 167
Kaiido Avatar answered Oct 06 '22 08:10

Kaiido