Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeJS Canvas is not a constructor #232

Open
safoine27 opened this issue Nov 6, 2018 · 11 comments
Open

NodeJS Canvas is not a constructor #232

safoine27 opened this issue Nov 6, 2018 · 11 comments

Comments

@safoine27
Copy link

safoine27 commented Nov 6, 2018

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

@misterh01
Copy link

i got same error and idk how to solve it

@NiteshSaxena
Copy link

+1

@bhavyasanchaniya
Copy link

bhavyasanchaniya commented Mar 4, 2019

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

@misterh01
Copy link

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

@furyOO7
Copy link

furyOO7 commented Jul 3, 2019

@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).

@NoiseEee
Copy link

NoiseEee commented Jul 26, 2019

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
Copy link

... 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

@inkquery
Copy link

@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

@slimtiexxx
Copy link

slimtiexxx commented Oct 11, 2019

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"

@suchetaswabhav
Copy link

suchetaswabhav commented Dec 30, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants