Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas Image src needs += #413

Closed
maxstudener opened this issue May 12, 2014 · 5 comments
Closed

Canvas Image src needs += #413

maxstudener opened this issue May 12, 2014 · 5 comments

Comments

@maxstudener
Copy link

I'd like to download an image from external source and on req.on('data', function(chunk)) id like to take the stream chunks of image from external source to canvas then on req.on('end') id like to then ctx.drawImage

@maxstudener
Copy link
Author

current code that works, has image in buffer from http download then assigning to img.src im assuming this will suck up all of memory, memory is faster than I/O but would like to put chunks directly right in img.src instead of in buffer

router.get('/', function(req, res) {

  var http = require('http');
  var http_options = {
    host: 'th01.deviantart.net',
    port: 80,
    path: '/fs71/200H/f/2012/208/0/2/meme_ay_si_png_by_mfsyrcm-d58vmvz.png'
  };

  http.get(http_options, function(resp) {
    console.log('downloading');

    var bufs = [];

    var Canvas = require('canvas');
    var Image = Canvas.Image;

    resp.on('data', function(chunk) {
        console.log(chunk.length)
        bufs.push(chunk);
     })

    resp.on('end', function() {
       console.log('done')

       var buf = Buffer.concat(bufs);


        img = new Image;

        img.onload = function(){
          var w = img.width
            , h = img.height
            , canvas = new Canvas(w, h)
            , ctx = canvas.getContext('2d');


          ctx.drawImage(img, 0, 0, w, h, 0, 0, w, h);

          canvas.toBuffer(function(err, canvas_buf){
            res.writeHead(200, { 'Content-Type': 'image/png', 'Content-Length': canvas_buf.length });
            res.end(canvas_buf);
          });
        }

        img.src = buf;


    });// on external image call end

  });// http get

}); // index route

@shawnbot
Copy link

+1 on this. It'd be really nice to have an async way to stream bytes into an Image.

@TooTallNate
Copy link
Contributor

The real way to do this would be to have img.src support a Readable stream instance.

@shawnbot
Copy link

👍

@LinusU
Copy link
Collaborator

LinusU commented Feb 15, 2016

What @TooTallNate said, allowing src += _ would just move the buffer from you program to canvas, it wouldn't make a difference...

I don't actually think that there would be any added memory benefit of reading the file chunk by chunk, since that would require you to either allocate the entire raw image on the first chunk, or do excessive copying of data...

I'm closing this for now, if anyone has more information or thoughts on how this should be implemented, please feel free to reopen...

@LinusU LinusU closed this as completed Feb 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants