A
A
Alex2019-12-08 15:20:37
Node.js
Alex, 2019-12-08 15:20:37

How to enable Canvas support in NodeJS?

There is a GifShot module in npm (from the guys from Yahoo). A post request arrives at the nodejs server with an array of images in base64 in JSON.

var express = require('express');
var gifshot = require('gifshot');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded(({ limit: '100mb', extended: true, parameterLimit: 50000 }));

app.post('/gifshot', urlencodedParser, function(req,res){
    var imagespost = JSON.parse(req.body.images);
    gifshot.createGIF({
        images: imagespost,
        interval: 0.04,
        numFrames: 10,
        frameDuration: 1,
        gifWidth: 300,
        gifHeight: 300,
        sampleInterval: 10,
        numWorkers: 2
    },function(obj) {
        if(!obj.error) {
            res.send(obj.image);
        } else {
            res.send(obj.errorMsg);
        }
    });
    
});

app.listen(3000);

gifshot writes an error Canvas elements are not supported in your browser
I looked at stackoverflow questions, additional modules for gifshot did not seem to be connected. The only difference is that the result in the stackoverflow example was written to a file, but I just output base64.
Who can tell what is the reason and how to fix this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2019-12-08
@abberati

Canvas is a browser API. This is a thing to draw other things in the browser window. Nodejs is not a browser, there is no window and there is no canvas. Don't turn it on at all.
You can, of course, look for emulators for the node or, in extreme cases, raise a virtual browser in the node process. But do you really want this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question