2D drawing on Node screen without having to compile anything

0

Is there a Node module that allows on-screen printing (same bitmap) without having to compile anything?

    
asked by anonymous 05.06.2017 / 22:59

1 answer

1

Try to use node-canvas .

Here's the sample code , see documentation . With some adaptations using http to create the http server and display the 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);
    
06.06.2017 / 14:45