CamanJS icon indicating copy to clipboard operation
CamanJS copied to clipboard

NodeJS Canvas is not a constructor

Open safoine27 opened this issue 7 years ago • 11 comments

I tried running the example of NodeJS but it kept giving me this error

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

Caman("user-icon.jpg", function () {
  this.brightness(40);
  this.render(function () {
    this.save("./output.png");
  });
});

and it gave me this error

_this.canvas = new Canvas(img.width, img.height); ^

TypeError: Canvas is not a constructor

safoine27 avatar Nov 06 '18 09:11 safoine27

i got same error and idk how to solve it

misterh01 avatar Dec 24 '18 23:12 misterh01

+1

NiteshSaxena avatar Feb 28 '19 09:02 NiteshSaxena

@safoine27 @Badvillain01 @NiteshSaxena replace in caman.full.js line no: 371 this.canvas = new Canvas.Canvas(this.imageWidth(), this.imageHeight());

bhavyasanchaniya avatar Mar 04 '19 10:03 bhavyasanchaniya

@bhavyasanchaniya...How?..installed it through npm

misterh01 avatar Apr 25 '19 23:04 misterh01

@Badvillain01 How did you install Camanjs through npm? I have been trying to do the same with no success even though I have also installed all the addition dependency mentioned(node-gyp & Installing GTK 2).

furyOO7 avatar Jul 03 '19 09:07 furyOO7

Hi folks! I'm thrilled to say that I have solved this error. I was lucky enough to have a working version from a while ago, and my Caman stuff was breaking while I was trying to modernize my script, so I compared the difference. The difference was actually the canvas package that Caman relies upon. If you don't specify the version in your package.json, Caman will go and download the latest (2.5.x at time of this message), however this version does not work with Caman. As far as I can tell, you need to choose a 1.x version of the canvas package.

So, make sure you add "canvas" as a dependency in your package.json, and restrict the version to 1.6.x. So, for example:

"dependencies" : {
      "canvas": "~1.6.13",
      "caman": "^4.1.2"
}

... and you should be good to go!!

NoiseEee avatar Jul 26 '19 20:07 NoiseEee

... and you should be good to go!!

Yes you will get warnings spit to screen about deprecations in the old Canvas package, but I can confirm all this works on Node 8.x.x

NoiseEee avatar Jul 26 '19 20:07 NoiseEee

@safoine27 @Badvillain01 @NiteshSaxena replace in caman.full.js line no: 371 this.canvas = new Canvas.Canvas(this.imageWidth(), this.imageHeight());

Works but this is not suitable solution. I tried to choose a 1.x version of the canvas package but that not solved the issue

inkquery avatar Aug 12 '19 10:08 inkquery

You can use my package where i fixed the problem by changing the line: this.canvas = new Canvas.Canvas(this.imageWidth(), this.imageHeight());

Just replace the original caman with this line: "caman": "git+https://gitlab.com/modernframe/caman.git"

slimtiexxx avatar Oct 11 '19 21:10 slimtiexxx

Hi ,

function readBarCodeSync(){
var filepath =  './barcode-128-image_2.png';
var buf = fs.readFileSync(filepath);
//var canvas = new Canvas(640, 480);
 var canvas =new Canvas.Canvas(this.imageWidth(), this.imageHeight());


var Image = Canvas.Image;
var ctx = canvas.getContext('2d');

fs.readFileSync(filepath, function (err, squid) {
    if (err) throw err;
    let img = new Image;
    img.src = squid;
    img.onload = function () {
    ctx.drawImage(img, 0, 0, 640, 480);
    }
});




let imgData = ctx.getImageData(0, 0, 640, 480);
const code = javascriptBarcodeReader(imgData /* ImageData */ , { 
    barcode: 'code-128', // 'code-128'
    type: 'interleaved', //standard/interleaved optional type
});

console.log(code); // output as null
}

I am getting error - TypeError: this.imageWidth is not a function at readBarCodeSync (D:\telegram_finesse_app_v1-master\server\barcodescan.server.js:91:36) at Layer.handle [as handle_request] (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\layer.js:95:5) at next (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\layer.js:95:5) at D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\index.js:281:22 at Function.process_params (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\index.js:335:12) at next (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\index.js:275:10) at Function.handle (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\index.js:174:3) at router (D:\telegram_finesse_app_v1-master\node_modules\express\lib\router\index.js:47:12)

suchetaswabhav avatar Dec 30 '21 16:12 suchetaswabhav

My package.json - "caman": "git+https://gitlab.com/modernframe/caman.git", "canvas": "^2.8.0",

suchetaswabhav avatar Dec 30 '21 16:12 suchetaswabhav