Skip to content

How do I prevent pixelation of the GIF from 2nd frame onwards when serving it from Node Server? #39

@Tribevote

Description

@Tribevote

The first frame seems to load fine, from 2nd frame onwards, there are black pixels. Any idea on how to get rid of them?

Pixelated Screen reference

Here is my code in NodeJS

var GIFEncoder = require('gifencoder');    
const { createCanvas, loadImage, giflib, ImageData } = require('canvas')
const canvas = createCanvas(600, 400)
const ctx = canvas.getContext('2d')
const { parseGIF, decompressFrames } = require('gifuct-js')

app.get('/gif2', async function(req, res) {

 const gifURL = 'https://www.hubspot.com/hubfs/Smiling%20Leo%20Perfect%20GIF.gif'

 const gif = await fetch(gifURL)
 .then(resp => resp.arrayBuffer())
 .then(buff => parseGIF(buff));

 const frames = await decompressFrames(gif,true);

 const encoder = new GIFEncoder(600, 400);
 // stream the results as they are available into myanimated.gif
 encoder.createReadStream().pipe(fs.createWriteStream('myanimated.gif'));
 encoder.start()
 encoder.setDelay(200)

 for (let frameIndex=0; frameIndex < frames.length; frameIndex++) {

     try {

         const frame = frames[frameIndex]

         const imageData = new ImageData(
             frame.patch,
             frame.dims.width,
             frame.dims.height
         );
 
         ctx.putImageData(imageData, 0, 0);
 
         //  Add Text on Canvas     
         let label = req.query.name
         ctx.font = '30px Impact'
         ctx.fillStyle = "white";
         wrapText(ctx, label, 50, 50, 200, 35)

         encoder.addFrame(ctx);
         
     } catch (error) {
         
         console.log("Something went wrong>>>>", error);
     }
    
     
 }

 encoder.finish();

 console.log("Encoding Done>>>>>");

 setTimeout(function() { 

     fs.readFile('myanimated.gif', function(err, data) {

         console.log("Serve it Waiter>>>>>");
 
         res.writeHead(200, {'content-type':'image/gif'});
         res.end(data);
 
     })

 }, 300);


})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions