-
-
Notifications
You must be signed in to change notification settings - Fork 457
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
[Question] How can we load an SVG file in a headless environment? #668
Comments
Hmmm, great question. You are correct in how Two.js creates a virtual DOM with native browser DOM elements. It uses this to iterate through all the children and parse what would be text as nodes. This is how it expects that: // src/utils/root.js
let root;
if (typeof window !== 'undefined') {
root = window;
} else if (typeof global !== 'undefined') {
root = global;
} else if (typeof self !== 'undefined') {
root = self;
}
// src/utils/dom.js
dom.temp = (root.document ? root.document.createElement('div') : {}); Is there a way that |
Thanks for pointing me in the right direction! I managed to make it work using the following code. global.window = new JSDOM().window;
global.document = window.document; Note that it also needs a global // src/effects/texture.js
if (root.document) {
anchor = document.createElement('a');
} Also, it seems JSDOM has to be loaded and configured before Two.js is loaded to make it work. Additionally, JSDOM advises against this kind of setup. This is from their docs:
I feel that there's still work to be done to make Two.js fully headless-mode compatible. Maybe we can add a warning with a link to JSDOM in the docs for now? What do you think? And do you think it would ever be feasible to make Two.js fully headless without the need for tools like JSDOM? I'm fairly new to Two.js, but I feel it should be possible. But I'm probably missing a really challenging part here ;) |
Thanks for posting your answer. This is super helpful for anyone that is using the current version of Two.js in a headless environment. There are three functions that really rely on the DOM in some capacity that I haven't moved away from using because it's (at the time of writing) it was difficult to do. They are as follows:
So, we'd have to address these three functions (doable!) to make it fully headless. |
Great. I'd agree that this is challenging but doable.
|
Thanks this is really helpful.
I'll create new issues for numbers 2, 3. |
I think we can close this issue now that we have the actionable issues created, right? |
Describe your question
How can I load an SVG file in a headless environment? It seems like Two.js expects the DOM to be present as it parses the loaded SVG using
innerHTML
and thenchildren
. I've tried to make it work usingjsdom
, but that didn't help. Is it possible to make it work?Your code (either pasted here, or a link to a hosted example)
Screenshots
The error:
Environment (please select one):
Additional context
Probably not relevant, but we're using Node 16.
The text was updated successfully, but these errors were encountered: