Skip to content

How to tell Canvas.pngStream() that the stream has ended? #351

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

Closed
giacecco opened this issue Oct 30, 2013 · 16 comments
Closed

How to tell Canvas.pngStream() that the stream has ended? #351

giacecco opened this issue Oct 30, 2013 · 16 comments

Comments

@giacecco
Copy link

Sorry if this is a stupid question, but I can't find a solution. If I write code like the one you see below:

    var canvas = new Canvas(CARD_SIZE_X, CARD_SIZE_Y),
        out = fs.createWriteStream(__dirname + '/text' + page + '.png'),
        stream = canvas.pngStream(),
        ctx = canvas.getContext('2d');
    stream.on('data', function(chunk) { out.write(chunk); });
    stream.on('end', function() { console.log('saved png'); });

... the 'end' event of the stream is triggered when the nodejs script exits.

What method of stream or perhaps canvas should I call if I need the stream to end before that, e.g. if I want to use the file that I have just created within the same script?

Moreover, if I create many streams from different canvas, when execution ends only the last of the .png files I have created has content, while all other files have zero length.

Thanks,

Giacecco

@giacecco
Copy link
Author

I setup a gist https://gist.github.com/giacecco/7234364 that simplifies my problem and makes evident what I am not understanding. Please help!

@mitar
Copy link
Contributor

mitar commented Sep 20, 2014

The PNG stream is underdefined. Maybe try to wrap it in a similar manner: #232 (comment)

@LinusU
Copy link
Collaborator

LinusU commented Feb 15, 2016

Closing this since it's an old thread, please report back if you still have problems...

@LinusU LinusU closed this as completed Feb 15, 2016
@Etsitpab
Copy link

I still have the same problem with jpeg streams. The end event is only triggered when the script ends. This makes it consumming a lot of memory.

@LinusU
Copy link
Collaborator

LinusU commented Feb 29, 2016

@Etsitpab That does sound like a bug, but I don't think it's the same since JPEG- and PNG-streams are handled very differently. Could you open up a new bug describing your issue?

@Etsitpab
Copy link

Etsitpab commented Mar 7, 2016

@LinusU sorry for the delay, here is the shortest script I could write to reproduce the problem I observed :

#!/usr/bin/env node

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

(function () {
    "use strict";
    var out, stream;
    out = fs.createWriteStream("test1.jpg");
    stream = new Canvas(500, 500).syncJPEGStream();
    stream.on('data', function (chunk) {
        console.log("Write chunk for", "test1.jpg")
        out.write(chunk);
    });
    stream.on('end', function () {
        console.log("End stream for", "test1.jpg")
    });
    out = fs.createWriteStream("test2.jpg");
    stream = new Canvas(500, 500).syncJPEGStream();
    stream.on('data', function (chunk) {
        console.log("Write chunk for", "test2.jpg")
        out.write(chunk);
    });
    stream.on('end', function () {
        console.log("End stream for", "test2.jpg")
    });
})();

And here, the corresponding output:

Write chunk for test1.jpg
Write chunk for test2.jpg
Write chunk for test2.jpg
End stream for test2.jpg
Write chunk for test1.jpg
End stream for test1.jpg

Where I was expecting something like that:

Write chunk for test1.jpg
Write chunk for test1.jpg
End stream for test1.jpg
Write chunk for test2.jpg
Write chunk for test2.jpg
End stream for test2.jpg

I'm very interested to know whether you are able to reproduce this behavioror have any hint on what i could do wrong. Thank you for your time and for the great work you're doing, this module is really cool !

@mitar
Copy link
Contributor

mitar commented Mar 7, 2016

@Etsitpab, please open a new ticket and put all this information there.

@LinusU
Copy link
Collaborator

LinusU commented Mar 7, 2016

Output for me:

Write chunk for test1.jpg
Write chunk for test1.jpg
End stream for test1.jpg
Write chunk for test2.jpg
Write chunk for test2.jpg
End stream for test2.jpg

@Etsitpab
Copy link

Etsitpab commented Mar 7, 2016

Well, I don't know why it doesn't work. Looks like we are stuck.
I'm using Debian sid by the way but I have another laptop under Ubuntu. I
will try as soon as possible if I also have this problem with it.

Thank you very much.
Baptiste

2016-03-07 13:19 GMT+01:00 Linus Unnebäck [email protected]:

Output for me:

Write chunk for test1.jpg
Write chunk for test1.jpg
End stream for test1.jpg
Write chunk for test2.jpg
Write chunk for test2.jpg
End stream for test2.jpg


Reply to this email directly or view it on GitHub
#351 (comment)
.

@LinusU
Copy link
Collaborator

LinusU commented Mar 7, 2016

I'm using OS X without pango installed...

@Etsitpab
Copy link

Etsitpab commented Mar 7, 2016

Ok, do you think that I could be related or that I should try something
like uninstalling pango ?

2016-03-07 13:33 GMT+01:00 Linus Unnebäck [email protected]:

I'm using OS X without pango installed...


Reply to this email directly or view it on GitHub
#351 (comment)
.

@LinusU
Copy link
Collaborator

LinusU commented Mar 7, 2016

Hmm, I don't think that it's related, but you never know :)

@Etsitpab
Copy link

Etsitpab commented Mar 7, 2016

I tried to uninstall it, but I doesn't make any difference :-(

2016-03-07 13:35 GMT+01:00 Linus Unnebäck [email protected]:

Hmm, I don't think that it's related, but you never know :)


Reply to this email directly or view it on GitHub
#351 (comment)
.

@LinusU
Copy link
Collaborator

LinusU commented Mar 7, 2016

Did you run node-gyp rebuild afterwards?

@Etsitpab
Copy link

Etsitpab commented Mar 7, 2016

No, I have to get back to work, I will try it tonight. thank you very much
for your help !
Cheers,
Baptiste

2016-03-07 13:41 GMT+01:00 Linus Unnebäck [email protected]:

Did you run node-gyp rebuild afterwards?


Reply to this email directly or view it on GitHub
#351 (comment)
.

@LinusU
Copy link
Collaborator

LinusU commented Mar 7, 2016

No problem 👍

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