Can Snap.svg be used only on the server side using Node.js? (Not via browser)

I need to create and process some SVGs only with some server-side code (e.g. with cron jobs), but I am wondering if Snap.svg can be used in this scenario where it is not included in the web page.

Will it work without Snap.svg in the browser?

+6
source share
1 answer

You can use jsdom to simulate a browser environment and initially run Snap.svg in Node.js.

Example:

const jsdom = require('jsdom'); const xmlserializer = require('xmlserializer'); jsdom.env('', [require.resolve('snapsvg')], (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); }); 

Print

 <svg height="100" version="1.1" width="100" xmlns="http://www.w3.org/2000/svg"><desc>Created with Snap</desc><defs/><rect x="20" y="20" width="60" height="60" style="" fill="#ff0000"/></svg> 
+4
source

All Articles