Snap.svg icon indicating copy to clipboard operation
Snap.svg copied to clipboard

Using SnapSVG inside of NodeJS

Open addijhaq opened this issue 5 years ago • 1 comments

Found a way to do this using JSDOM, however all examples are using outdated code

const jsdom = require('jsdom');
const xmlserializer = require('xmlserializer');

jsdom.env('', ['node_modules/snapsvg/dist/snap.svg.js'], (error, window) => {
    if (error) throw error;

    const paper = window.Snap(100, 100);

    const rect = paper.rect(20, 20, 60, 60);
    rect.attr({fill: 'red'});

    const svg = xmlserializer.serializeToString(paper.node);
    window.close();

    console.log(svg);
});

This no longer works.

Any suggestions on how to get SnapSVG running inside of Node.js with JSDOM without using webpack?

addijhaq avatar Sep 17 '20 15:09 addijhaq

I know this is old question, but since I got to here, I might as well write a solution. For me this worked like a charm:

const { JSDOM } = require('jsdom'); const dom = new JSDOM(); global.window = dom.window;

If I require Snap after this. I can use anywhere in code: Snap.path.toCubic function.

aklacar1 avatar Nov 08 '24 09:11 aklacar1