Using jsxgraph on node.js

33 views
Skip to first unread message

dbrian...@gmail.com

unread,
Jun 9, 2022, 12:28:30 AM6/9/22
to JSXGraph
I'm interested in using JSXGraph in the context of providing graphics for interactive exercises for the PreTeXt project but there is a need to be able to create static versions for output other than HTML. I'd like to be able to use the same code that is used for the HTML version. Suppose I have the javascript code to generate a figure on a webpage and want to generate it in a scripted setting offline with Node.js.

Is there a way to create an svg outside the browser and without a webpage and save it as a file? I see references online that jsxgraph is part of npm (I have installed it), but I can't find documentation to how it might be used. I am new to node/npm. I found a reference saying to use the command:

JXG = require('jsxgraph')

But when I try JXG.JSXGraph(...) it requires the id of a jxgbox div, which doesn't exist in this context, as there isn't even a document defined.

Does anyone have an example of how this might be done?

Thank you in advance,
D. Brian Walton

Alfred Wassermann

unread,
Jun 9, 2022, 6:20:08 AM6/9/22
to JSXGraph
As far as I can judge, what you need is a headless browser on server side. JSXGraph uses this type of browser for unit tests (using the software karma and jasmine).  I'm sure that using e.g. chrome in headless mode, the SVG is generated which then can be exported with JSXGraph's board.renderer.dumpToDataURI(). This will give you a base64 encoded string containing the SVG image.
Hope that helps,
Alfred



Reply all
Reply to author
Forward
0 new messages