user2005317
user2005317

Reputation: 21

Is there a way to make svg.js work with node.js

Did someone of you try to make svg.js work with node.js? I tried to use the jsdom module to render svg but jsdom but SVG.supported returns false. Is there a way to make this library work with node.js?

Thanks in advance!

EDIT: Here is my code, I want to make that on Node.js and then probably render the SVG in a pdf or as a png:

var draw = SVG('drawing').size(600, 600) 
var image = draw.image('inclusions.png') 
image.size(400, 150) 
var rect = draw.rect(400, 150).attr({ fill: 'orange' }) 
for (i = 0; i < 10; i++) { 
    var point = draw.circle(5) 
    var xpos = Math.floor((Math.random() * 400) + 1); 
    var ypos = Math.floor((Math.random() * 150) + 1); 
    point.x(xpos) 
    point.y(ypos) 
    point.fill('black') 
} 
image.front()

Upvotes: 2

Views: 5622

Answers (2)

erhanasikoglu
erhanasikoglu

Reputation: 1735

Here is the working example usage of svg.js inside nodejs project, svgdom is the suggested library from svg.js official website

const window = require('svgdom');
const SVG = require('svg.js')(window);
const document = window.document;

function generateSVGTextLines(width, height, lineList, tAsset) {
  var draw = SVG(document.documentElement).size(width, height);
  draw.clear();

  draw.text(function (add) {
    if (lineList) {
        for (var i = 0; i < lineList.length; i++) {
            add.tspan(lineList[i].text).attr("x", "50%").newLine();
        }
    }
  }).font({
    family: tAsset.fontFamily,
    size: tAsset.fontHeight,
    leading: '1.2em',
    anchor: "middle"
   }).move(width / 2, 0);

   return draw.svg();
}

Upvotes: 3

TheHans255
TheHans255

Reputation: 2233

This link might be helpful: http://techslides.com/save-svg-as-an-image

This documents a client side solution that causes the requisite SVG to be drawn on the end user's browser, rather than on the server, but it provides the end result you want by putting the SVG into an image tag and allowing the user to download it. If keeping the SVG drawing logic secret is a problem, you could use a similar principle by sicing PhantomJS on the generator page and sending the user the image it downloads.

Upvotes: 0

Related Questions