Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw an image in node.js

I'd like to create an application using node.js which creates an image. In the image I'd like to programmatically draw circles, lines or any function f(x) (well I could draw that function by adding points at some coordinates). I'd like to know which node.js modules I should use, or if there is something created for this.

In other words I need to draw a given mathematical functions and export it to an image file.

Thanks.

like image 911
Juanillo Avatar asked Nov 03 '12 19:11

Juanillo


People also ask

How do I insert an image in node JS?

post('/upload', (req, res) => { // We'll handle the image upload here }); To handle file uploads we need the express-fileupload middleware, this will read the multipart/form-data and add a files property to the request object.

Does NodeJS have canvas?

node-canvas is a Cairo-backed Canvas implementation for Node. js.

Is Node JS good for image processing?

With image processing, your application can resize and compress all the user-uploaded images, which can significantly improve your application performance and save your server disk space. Node. js has an ecosystem of libraries you can use to process images, such as sharp, jimp, and gm module.


1 Answers

Have a look at node-canvas which is a canvas implementation for Node.js

Source code example:

var Canvas = require('canvas')
  , canvas = new Canvas(200,200)
  , ctx = canvas.getContext('2d');

ctx.font = '30px Impact';
ctx.rotate(.1);
ctx.fillText("Awesome!", 50, 100);

var te = ctx.measureText('Awesome!');
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.beginPath();
ctx.lineTo(50, 102);
ctx.lineTo(50 + te.width, 102);
ctx.stroke();

console.log('<img src="' + canvas.toDataURL() + '" />');
like image 102
Werner Kvalem Vesterås Avatar answered Oct 09 '22 03:10

Werner Kvalem Vesterås