Skip to content
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

Does svg.js support node.js? #352

Closed
y0ke opened this issue Jun 17, 2015 · 20 comments
Closed

Does svg.js support node.js? #352

y0ke opened this issue Jun 17, 2015 · 20 comments

Comments

@y0ke
Copy link

y0ke commented Jun 17, 2015

hi all.
Does svg.js support node.js?
Sorry If I missed the part of the document which states about it.
Thanks in advance.

@Fuzzyma
Copy link
Member

Fuzzyma commented Jun 17, 2015

What do you want to do exactly?

@y0ke
Copy link
Author

y0ke commented Jun 18, 2015

I'm planning to build a APIlike server that returns SVG data as plain text.
So it doesn't require any animation/event handler things but just creating SVGs.so far I found phantom.js on the server side do the trick though I don't want to event render SVGs if I can and work with Basic node.js

@Fuzzyma
Copy link
Member

Fuzzyma commented Jun 18, 2015

As long as you dont use functions which relates on methods made available by the browser (e.g. bbox) it should work. But I am not sure with that. You have to try that yourself

@leoweigand
Copy link

svg.js will not work inside node.js as it assumes the presence of a document. It is intended to be used for client-side manipulation.
Take a look at this article: http://www.smashingmagazine.com/2014/05/26/love-generating-svg-javascript-move-to-server/#jsdom.
I linked the section, where the author explains how to use jsdom to simulate the DOM inside nodejs, in theory this should be possible to do with svg.js, too.

Anyway, this is not something svg.js should take care of, so this is ready to be closed.

@Fuzzyma Fuzzyma closed this as completed Jul 1, 2015
@derhuerst
Copy link

I think there's just one small part missing.

If svg.js allowed me to pass the "global scope object", it would easily be possible. jsdom gives me a window object with a document property, but I can't pass it into the svg.js factory method.

AFAIK, everything else works.

@derhuerst
Copy link

I'd like svg.js to either respect environments without a global document object or provide a way to pass it in, e.g. to the factory method.

@Fuzzyma Fuzzyma reopened this Sep 9, 2015
@Fuzzyma
Copy link
Member

Fuzzyma commented Sep 14, 2015

Any idea on how to accomplish this?

@derhuerst
Copy link

No sry, I haven't gone through the code of svg.js.

jQuery allows you to pass a window object as of 2.1.0. Maybe this can help.

@Fuzzyma
Copy link
Member

Fuzzyma commented Sep 15, 2015

Actually I cannot see the line where you can really pass the window objcect. It just checks for the window object to be present and passes window or this. The UMD-Wrapper of svg.js does the same. this is passed to the factory which is just the global variable at this point. We just have to move the global-element into the lib where document is used

@derhuerst
Copy link

Look at the lines 25-27. If the global scope has no document property, an anonymous function is returned, which you can pass a window object (which is supposed to then have a document proprty).

@Fuzzyma
Copy link
Member

Fuzzyma commented Oct 11, 2015

Ok - I think I understand how it works. Please test this for me:
Replace the UMD-Wrapper at the start with this one:

(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(factory);
  } else if (typeof exports === 'object') {
    module.exports = root.document ? factory(root, root.document) : function(w){ return factory(w, w.document) };
  } else {
    root.SVG = factory(root, root.document);
  }
}(typeof window !== "undefined" ? window : this, function(window, document) {

You now can require your module via require('svg.js')(yourWindow). If that works I will push that to the master (even if I have to disable my gulp umd wrapper because it cant do this kind of stuff)

@Fuzzyma
Copy link
Member

Fuzzyma commented Oct 14, 2015

@derhuerst @y0ke @lweigand you all were that curios about that. What happend? :D

@derhuerst
Copy link

@Fuzzyma

My short-lasting motivation about SVG is over. I will have to wait until it comes back. :P

Furthermore, my contribution to this discussion was not because I urgently need to use SVG.js in Node, but because i like isomorphic NPM modules in general.

@fibo
Copy link
Contributor

fibo commented Oct 24, 2015

First of all, thank you againg for open sourcing and developing svg.js, it is the best svg lib you can find. Thank you also for the hints in this thread.

I am using svg.js with Node for my flow-view project, where I am using SVG.js file for server side, and window.SVG.js file for the browserified version.

I tryed the solution proposed, see fibo/flow-view@f1e5c1b where I used my svg.js custom build after modifying dist/svg.js with code found in #352 (comment).

I only tryed with a trivial test, but, it works and I hope this solution will be merged (please let me know).
This would be a big win for me, since I had to use a fake (empty) SVG object to let me pass a trivial test. Now I could add more tests running server side, making my project better.

It also open a big door to server side svg export to png or to svg file.

A note about plugins: in the browserified version I am using require('svg.js.pluginfoo') and it works, so in this sense svg.js support Node too, at least when browserified. I am not sure if it is just a trick that I found or it is implicitly known by most of developers, since I did not found any documentation about how to use plugins from npm.

@Fuzzyma
Copy link
Member

Fuzzyma commented Oct 24, 2015

Glad to hear, that this works.
Maybe I find a gulp plugin which helps including this properly

@fibo
Copy link
Contributor

fibo commented Oct 25, 2015

I am not a gulp expert (I currently use bare npm scripts) but I think you could even write your own task, so it would not be difficult if you cannot find a plugin that do specifically that.

By the way, I am also testing it on client side and it still works too.

@Fuzzyma
Copy link
Member

Fuzzyma commented Oct 25, 2015

I am curious if bbox works on serverside. Can anyone try that for me? (@fibo maybe?)

@Fuzzyma
Copy link
Member

Fuzzyma commented Mar 19, 2017

@y0ke @leoweigand @derhuerst @fibo we just released the new project svgdom which enables svg.js to run on node. Its nothing big but give it a try and report back :)

@leoweigand
Copy link

@Fuzzyma thanks for the heads-up! I'll make sure to look into it when I am tinkering with svg again ;)

@fibo
Copy link
Contributor

fibo commented Mar 19, 2017

check out also svgx which renders SVG server side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants