Search code examples
node.jspngdrawing

how to draw a line using pureimage?


I'm new to node.js and this is just a small thing I need as part of a project that I'm writing in a different language.

How would I go about using pureimage to make a line from a start position to an end position and exporting it as a png? I'm interested in using pureimage because to my understanding you can set the line width with a variable

I currently have a working program with pngjs-draw but it's impossible to change line width there.


Solution

  • You can specify the line thickness with the lineWidth property. Then you can draw your line in the canvas 2D context and export your image as a png file with pureimage, like this:

    const fs = require('fs');
    const PImage = require('pureimage');
    
    let img = PImage.make(100, 50);
    let ctx = img.getContext('2d');
    
    ctx.lineWidth = 5;
    
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(100, 50);
    ctx.stroke(); 
    
    PImage.encodePNGToStream(img, fs.createWriteStream('out.png'));