[Request]Nw.js write image file

Post your work in progress addons and get feedback

Post » Tue Apr 12, 2016 12:32 pm

Okay so I tried many different plugins and solutions already, but nothing really seems to be doing that at the moment
I need to save those images (0 to 100+) on the user disk, to load them back the next time they use the app
Been using base64 loaded from txt files so far, but now I realize this slows down boot times immensely with many pictures...

So the nw.js action "write file" can write text or a base64 string, but I believe it can also save image as binary files?
Anyway that's what those guys seems to be doing? Let me know if someone can make sense of this into a plugin
I need "automatic" save in a custom folder, without user dialog, just like "write file" does, but for actual image data

http://stackoverflow.com/questions/6926 ... ge-to-disk
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Post » Tue Apr 12, 2016 3:11 pm

Does @pode 's filesaver plugin work?
B
109
S
27
G
276
Posts: 4,479
Reputation: 154,418

Post » Tue Apr 12, 2016 5:03 pm

That's one of the plugins I tried yeah, but nope it opens a "save as" dialog on nw.js, so not really manageable for automating 100+
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Post » Wed Apr 13, 2016 9:40 am

The performance dropped might come from 2 reasons.
1. as you mention the image is saved in a text string , not a binary file. (In your given URL, it saved image in base64 string too, not a binary)
2. file saving in official nw.js plugin is synchronous, events will be held until one file saved, then saves next file. An asynchronous file writing action might improve performance a lot, imo.
B
109
S
27
G
276
Posts: 4,479
Reputation: 154,418

Post » Wed Apr 13, 2016 10:26 am

Yeah I can confirm #1, I tried yesterday to load jpg versions of the same text files, and it was much faster
Not to mention the filesizes, as one of the pic was only 100kb in jpg, and goes above 1mb in it's base64 equivalent :p

I'm mostly concerned about loading times, as saving happens once, only each time there's a new entry
While loading happens every time the app launches, for each of the entries
But yeah to be able to load fast I need to save those in jpg first through the code
And could be interesting to see the difference in saving times for synchronous events indeed

Are you sure they're saving in base64 in the link I posted? :(
I can't fully understand the code, but what about the "binaryData", and correct "png" extension there?
Code: Select all
require("fs").writeFile("out.png", binaryData, "binary", function(err)

Ok maybe the first answer looks base64, but how about the second answer "full solution" on that page?
I posted that link coz the docs only have a "utf8" encoding exemple, so it's hard to tell what's possible or not
https://nodejs.org/api/fs.html#fs_fs_wr ... s_callback
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Post » Fri Apr 15, 2016 9:15 am

Okay how about this example? Looks very binary to me

http://stackoverflow.com/questions/7329 ... ng-node-js

Maybe @Ashley would be interested otherwise to add "Write Image File" in the official Nw.js plugin if it's possible?
It could work with the base64 plugin we can use already, or any base64 input (like CanvasSnapshot)
So basically Sprite > Load image from url > Extracct base64 > Write image file
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Post » Thu Apr 21, 2016 6:21 pm

I found this nice plugin to decode base64 inside C2

Combined with the other extract sprite to base 64 would that work?

How about an ExecJS with atob()

I seem to get some kind of "binary" data written, but it says the image is corrupted when I try to open it

Can someone have a look at the NWautosave.capx, where I'm trying all that? with no success whatsoever :mrgreen:
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Post » Fri Apr 22, 2016 4:48 pm

Now, I'm trying to modify @Pode original plugin to extract the sprite directly as a "blob" instead of a base64 string

Luckily the HTMLCanvasElement.toBlob() is supported form Chrome 50+, which just made it into nw.js 14.0 ten days ago!

Would that work to write that image "blob" with nw.js "write file" as a PNG on disk?

But yeah I have no idea about javascript or how to write a plugin, so the modified plugin below doesn't work
How do I return the "blob" out of that child function back into the "ret"?

Code: Select all
   // the example expression
   exps.currentImage = function (ret)   // 'ret' must always be the first parameter - always return the expression's result through it!
   {
      this.canvasSprite.width = this.inst.cur_animation.frames[this.inst.cur_frame].texture_img.width;
      this.canvasSprite.height = this.inst.cur_animation.frames[this.inst.cur_frame].texture_img.height;
      this.ctxSprite.drawImage(this.inst.cur_animation.frames[this.inst.cur_frame].texture_img, 0, 0);
      this.canvasSprite.toBlob(function(blob) {
         ret.set_string(blob);
      });
   };
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Post » Fri Apr 22, 2016 5:07 pm

Perhaps you could zip them first.
Image ImageImage
B
170
S
50
G
179
Posts: 8,378
Reputation: 113,425

Post » Fri Apr 22, 2016 6:24 pm

@newt
The library is different for each user and each image has to be fetched one by one through an API
(it's a movie app with film posters from IMDb)
B
22
S
6
G
8
Posts: 328
Reputation: 6,651

Next

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests