简体   繁体   中英

Node.js node canvas show base64 image

I'm currently transfering a base64 image from a client to my server with socket.io . On the server, I want to render that base64 image into my canvas.

The code is:

function newSlide(slideImage){
  socket.broadcast.emit('newSlide', slideImage);
  var img = new Image;
  img.src = slideImage;
  ctx.drawImage(img, 0, 0);
}

With that code, on the client everything is working, I can load the base64 image into a normal canvas, but on my server in server.js (where this code is), it's not working, I'm getting an error saying Image is not defined at the line new Image , when trying without the image-creation and just put in the base64 string, it says image or canvas excepted.

What's the problem with the image creation, why does it not work? And what can I do to make it working?

Image is part of the DOM API and so is not present in the Node runtime.

To do what you want you're going to have to import a module that allows you to emulate some of the DOM APIs.

It should be new Image() , not new Image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM