2D drawing on the screen in Node without having to compile anything

Asked

Viewed 55 times

0

Is there any Node module that allows printing on the screen (same bitmap), without having to compile anything?

  • 1

    I found this, it might help you: https://www.npmjs.com/package/canvas

1 answer

1

Try to use the node-canvas.

Here is the code example, see the documentation. With some adaptations using http to create http server and show image:

var http = require('http'), fs = require('fs'), 
Canvas = require('canvas');
var port = 3000;

http.createServer(function (req, res) {
    fs.readFile(__dirname + '/image.jpg', function(err, data) {
        if (err) throw err;
        var img = new Canvas.Image; // Create a new Image
        img.src = data;

        // Inicialize uma nova tela com as mesmas dimensões
        // e use um contexto de desenho 2D para isso.
        var canvas = new Canvas(img.width, img.height);
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, img.width / 4, img.height / 4);

        //crie os htmls para mostrar a img
        res.write('<html><body>');
        res.write('<img src="' + canvas.toDataURL() + '" />');
        res.write('</body></html>');
        res.end();
    });

}).listen(port, "localhost");
console.log('Acesse localhost ' + port);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.