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

Running in Node, but Caman tries to access document object. #181

Open vinicius0026 opened 9 years ago

vinicius0026 commented 9 years ago

I'm trying to work with layers in Caman, with overlay images in each layer, but I'm having some issues.

My current attempt is like shown below:

var Caman = require('caman').Caman;

Caman('./background.png', function () {

    this.newLayer(function () {
        this.setBlendingMode('normal');
        this.overlayImage('./overlay.png');
    });

    this.render(function () {
        this.save('./result.png');
    });
});

But when I execute this code, I get the following error:

img = document.createElement('img');
      ^

ReferenceError: document is not defined
    at Renderer.Caman.Renderer.Renderer.loadOverlay (/.../camanjs/node_modules/caman/dist/caman.full.js:2036:13)
    at Renderer.Caman.Renderer.Renderer.processNext (/.../camanjs/node_modules/caman/dist/caman.full.js:1855:23)
    at Renderer.processNext (/.../camanjs/node_modules/caman/dist/caman.full.js:7:61)
    at Renderer.Caman.Renderer.Renderer.processNext (/.../camanjs/node_modules/caman/dist/caman.full.js:1849:23)
    at Renderer.processNext (/.../camanjs/node_modules/caman/dist/caman.full.js:7:61)
    at Renderer.Caman.Renderer.Renderer.execute (/.../camanjs/node_modules/caman/dist/caman.full.js:1866:19)
    at Caman.render (/.../camanjs/node_modules/caman/dist/caman.full.js:597:28)
    at Caman.callback (/.../camanjs/index.js:12:10)
    at Caman.finishInit (/.../camanjs/node_modules/caman/dist/caman.full.js:484:21)
    at Caman.nodeFileReady (/.../camanjs/node_modules/caman/dist/caman.full.js:372:19)

If I run the very simple example for node in Caman homepage (only setting the brightness), it works.

What am I missing?

jocooler commented 8 years ago

I had noticed some differences between the latest version of Caman and build used by the site. I suggest you try pulling the compiled version from the site or a CDN and give that a try.

As for solving the issue, the offending coffeescript certain does use a created image to work. (source/core/renderer.coffee:206)

 # Loads an image onto the current canvas
   loadOverlay: (layer, src) ->
     img = document.createElement 'img'
     img.onload = =>
       ...
kaustubhn commented 8 years ago

I ran the above code getting the same error, do we have any solution?

jordancalder commented 8 years ago

This is the same issue I am dealing with. Am I missing something here?

pwrstudio commented 8 years ago

Exact same issue. Here did anyone figure this out?