-
-
Notifications
You must be signed in to change notification settings - Fork 616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use rough.js in node.js to save an svg file? #169
Comments
(I figured it out, reposting my SO answer here in case it's of interest. I'm happy for this issue to be closed or deleted.) The solution I found uses // file generate-rough-svg.js
const { DOMImplementation, XMLSerializer } = require('xmldom');
const xmlSerializer = new XMLSerializer();
const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html', null);
const rough = require('roughjs');
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const rc = rough.svg(svg);
var data = [[40, 20], [140, 30], [100, 130]]
svg.appendChild(rc.polygon(data))
let xml = xmlSerializer.serializeToString(svg);
console.log(xml) Now I can run
to generate the svg file. |
My solution is a simple mock const simpleDomMock = {
ownerDocument: {
createElementNS: (ns, tagName) => {
const children = []
const attrs = {}
return {
tagName,
attrs,
setAttribute: (key, value) => (attrs[key] = value),
appendChild: node => children.push(node),
children,
}
},
},
} Usage: const rough = require('roughjs/bundled/rough.cjs.js').svg(domMock)
const rect = rough.rectangle(0, 0, 1, 1) rect is now of the structure { tagName: 'g',
attrs: {},
setAttribute: [Function: setAttribute],
appendChild: [Function: appendChild],
children:
[ { tagName: 'path',
attrs: [Object],
setAttribute: [Function: setAttribute],
appendChild: [Function: appendChild],
children: [] } ] } So now you can use what every you want to serialize this to the desired format. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(Disclaimer: I also asked this on stackoverflow here)
The html code below is a minimal example to create an svg element displaying a simple rough.js polygon generated from an array of data. I want to use the js code in a nodejs program that generates the same svg and saves it to a file. What would be the simplest way of achieving this?
The text was updated successfully, but these errors were encountered: