This question is about using the p5.js Editor tool.
In the index.html file there are statements in the head section to link to p5.js e.g. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
Now I want to link to a js file on my website like this <script src="http://lagers.org.uk/libs/_files/math/complex.js"></script>
but it doesn’t work. The editor doesn’t give any error/warning message about the file it simply states
[poly.js, line 229] "Complex" is not defined in the current scope. If you have defined it in your code, you should check its scope, spelling, and letter-casing (JavaScript is case-sensitive).
The Complex class is defined in this file.
This is the sketch. I have tried it in Firefox and Safari with the same result.
I’ve removed the http: part and then it loaded successfully: <script src=//lagers.org.uk/libs/_files/math/complex.js></script>
We shouldn’t load files using protocol http: while our page is https:!
I tried this with "" around the source file also with and without the // and unfortunately this didn’t solve my problem. The error message is "Complex is not defined"
It could still be that a protocol / security issue but I would expect the editor to report it as a problem
Imo you need to make your class available in the current/global context when loading from remote.
Put that to the bottom of your remote complex.js window.Complex = Complex;
and try again …
Ok! In principle it should work like it is as the code is a simple JS code which should automatically assigned to the global context. Maybe open web developer console and look what that say …
Unfortunately I can’t test now on my mobile. Will have a look tomorrow if the issue is persistent…
When I 1st edited out http: from “Complex.js” link it just worked for me.
But strangely I can’t get it working again!
Anyways, I don’t like https://editor.p5js.org at all; and don’t even have an account there.
It’s always seemed to me it does too many things behind-the-scene!
I don’t use the p5.js editor or any web editor for development, that includes OpenPprocessing. I use Visual Studio Code with TypeScript for anything Javascript and Eclipse for anything Java.
I have used OpenProcessing since 2009 to share my efforts with others. In the beginning I posted Java Applets but now I post p5.js and/or Javascript sketches. I am considering using the p5.js Editor as a secondary outlet for my work.
p5.js editor seems to parse the index.html file and processes all the script tags in fileReducer.js and I suspect that it is ignoring request script requests that are not local.
OpenProcessing, Glitch and many others are meant to host ready-to-deploy code.
While editor.p5js.org is meant for creating code online besides hosting them.
IMO, that’s not the best usage for it. Other sites are much better for sharing.
Try out Glitch.com or GitHub.com.
Thanks for your time investigating this issue, unfortunately I am unable to test your fix but since you have got it to work I will mark it as a solution