meltingice / CamanJS

Javascript HTML5 (Ca)nvas (Man)ipulation
http://camanjs.com
BSD 3-Clause "New" or "Revised" License
3.56k stars 406 forks source link

Saving callback #97

Open DustinHigginbotham opened 11 years ago

DustinHigginbotham commented 11 years ago

Perhaps I am missing something, but is there a way we can have an onSave style callback? I'm in a node.js environment and this could be very useful for me.

Thanks!

meltingice commented 11 years ago

You're right, there isn't. Marking this as a to-do item.

DustinHigginbotham commented 11 years ago

If I were to implement this myself, should it be registered as an Event or should another parameter be passed into the save function?

I think what makes it interesting is the node and browser support. Would it make sense to have a save callback if you are in the browser?

meltingice commented 11 years ago

It would not make sense to have a callback for the browser version because the way save works is by redirecting the browser to the base64 encoded URL.

That said, the way the save() function works is by delegating to either browserSave or nodeSave depending on the environment. Because of this, I would allow both an optional callback to nodeSave and fire an imageSaved event.

DustinHigginbotham commented 11 years ago

I was working on this feature. I have a callback firing if defined on the save method. Then I added the event type to make use of the whole event system when I realized if I target a single instance like so:

Caman.Event.listen(c, 'imageSaved', function() { /* ... */ });

...Nothing fires. If I run this without the target instance, it works fine. Looking into this more, it looks as if the id set during initialization is null/undefined in the event system (console.log(target.id)). I'm wondering if I am missing something or if you have any insight on what could be happening here. I'm more than willing to fix this, but some guidance would be awesome. :)

jeffreyqua commented 10 years ago

Is there any way to check for a callback once the image is loaded so I can call the caman functions once the caman object is ready?

ie var tempCanvas = document.createElement('canvas'); var c = Caman(tempCanvas, 'path/to/image.jpg'); if (c.isLoaded) { c.reset(); c.resize({width: new_w, height: new_h}); c.render(); }

I need the callback to be external because the crop and resize functions I want to apply are using variables outside the scope of an anonymous function callback.

meltingice commented 10 years ago

There isn't right now, but I could see that being a nice little addition. I'll make a separate issue for it.

raine commented 10 years ago

So, no way to know when the file has actually been saved when working with CamanJS in node?

edit: just read the code and will probably use this.canvas.toBuffer() directly