meltingice / CamanJS

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

caman.reloadCanvasData() Uncaught TypeError: undefined is not a function #154

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi,

I came across your package recently and really enjoyed it until today when I started integrating CamanJS with my project at work and stumbled into this issue.

TL;DR

...
 img.onload = ->
    Caman @, ->
...

Scenario

I'm using CamanJS in browser, where I

I initialize Caman with a new Image() object in a _.each files after user chooses the images:

_.each files, (file) ->
  ...
  img = new Image()
  img.onload = ->
    Caman @, ->
      @resize height: 300
      @render ->
        upload @canvas.toDataUrl
...

Problem

The problem happens when I need to upload multiple images. All the images are the same as the first one uploaded. If I upload them separately, they are still the same, that is, unless I reload the page every single time.

Current Status

Now I know that before Caman @, ->, the image is correct every time, but in the callback of Caman initializer (after Caman @, ->), the image stays unchanged every time.

I tried reset(), reloadCanvasData(), revert(true/false), and etc. But they all throw the error Uncaught TypeError: undefined is not a function. revert() actually change the picture to an unrecognizable image.

Thanks

Sorry for the long form. I added a tl;dr section at the beginning. I'd love to get some insights from you, and solve this issue as soon as I can.

ghost commented 10 years ago

OK. I just solved the issue

caman = null

_.each files, (file) ->
  ...
  img = new Image()
  img.onload = ->
    if caman?
      caman.loadImage @, ->
        @resize height: 300
        @render ->
          upload @canvas.toDataUrl
    else
      caman = Caman @, ->
        @resize height: 300
        @render ->
          upload @canvas.toDataUrl
...

Just found & tried this loadImage method, and it works!

Still would love to hear your insights if there is a better solution.

Thanks.